参考资料

  1. 如何添加按钮的过渡动画效果?
  2. 颜色属性详细说明以及案例
  3. 动态伪类选择器详细说明以及案例
  4. display: flex/inline-flex示例
  5. 如何优化Flexbox布局的性能?
  6. css让div居中的几种方法
  7. css div内容居中
  8. CSS 阴影效果属性
  1. 水平居中:

div {
  margin: 0 auto;
  width: 200px;
}
  1. 水平垂直居中(flex布局):

.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
  1. 水平垂直居中(绝对定位):

div {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
  1. 水平垂直居中(grid布局):

.container {
  display: grid;
  place-items: center;
  height: 100vh;
}

完整示例(flex方案):

<!DOCTYPE html>
<html>
<head>
<style>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  border: 1px solid #ccc;
}
.box {
  width: 200px;
  height: 100px;
  background: #f0f0f0;
  text-align: center;
  line-height: 100px;
}
</style>
</head>
<body>
<div class="container">
  <div class="box">居中内容</div>
</div>
</body>
</html>

声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。