参考资料

  1. 如何调试Flex布局问题?
  2. css文字阴影属性
  3. Flex布局在动画制作中的应用?
  4. css怎么使div居中
  5. flex-direction: row/column示例
  6. CSS 阴影效果设置
  7. Flex布局与Grid布局的对比?
  8. 如何用添加产品图片和描述?

css如何居中一个div

  1. 水平居中
    方法1: margin auto

div {
  width: 200px;
  margin: 0 auto;
}

方法2: flexbox

.parent {
  display: flex;
  justify-content: center;
}
  1. 垂直居中
    方法1: flexbox

.parent {
  display: flex;
  align-items: center;
  height: 300px;
}

方法2: transform

div {
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}
  1. 水平垂直居中
    方法1: flexbox

.parent {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 300px;
}

方法2: absolute + transform

.parent {
  position: relative;
  height: 300px;
}
.child {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

完整示例:

<div class="parent">
  <div class="child">居中内容</div>
</div>

<style>
.parent {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 300px;
  border: 1px solid #ccc;
}
.child {
  width: 200px;
  padding: 20px;
  background: #f0f0f0;
}
</style>