参考资料

  1. CSS弹性盒子详细说明以及案例
  2. CSS有哪些常用的选择器?
  3. 子选择器详细说明以及案例
  4. css中div居中详细说明
  5. css阴影效果属性有哪些,
  6. 动态伪类选择器详细说明以及案例
  7. css3阴影效果
  8. css阴影颜色怎么设置

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>