2.居中方式总结

居中方式总结

经典真题

  • 怎么让一个 div 水平垂直居中

盒子居中

首先题目问到了如何进行居中,那么居中肯定分 2 个方向,一个是水平方向,一个是垂直方向。

水平方向居中

水平方向居中很简单,有 2 种常见的方式:

  1. 设置盒子 margin : 0 auto :这种居中方式的原理在于设置 margin-left 为 auto 时,margin-left 就会被设置为能有多大就设置多大,所以盒子会跑到最右边,而设置 margin-right 为 auto 时,同理盒子就会跑到最左边。所以,当我们设置左右的 margin 都是 auto 的时候,盒子就跑到了中间,从而形成了水平居中。

  2. 第二种常见的方式就是通过 display : flex 设置盒子的外层盒子是一个弹性盒,然后通过 justify-content : center 使得内部的盒子居中。

垂直方向居中

关于盒子的垂直方向居中,方法就比较多了,这里介绍几种:

  1. 通过 verticle-align:middle 实现垂直居中

通过 vertical-align:middle 实现垂直居中是最常使用的方法,但是有一点需要格外注意,vertical 生效的前提是元素的 display:inline-block 。并且在使用 vertical-align:middle 的时候需要一个兄弟元素做参照物,让它垂直于兄弟元素的中心点。vertical-align 对齐的方法是寻找兄弟元素中最高的元素作为参考。

代码示例如下:

html 复制代码
<div class="container">
  <div class="item"></div>
  <div class="brotherBox"></div>
</div>
css 复制代码
.container{
  width: 500px;
  height: 300px;
  background-color: pink;
  text-align: center;
}
.item{
  width: 100px;
  height: 100px;
  background-color: skyblue;
  vertical-align: middle;
  margin: 0 auto;
  display: inline-block;
}
.brotherBox{
  height: 100%;
  /* width: 2px; */
  background: red;
  display: inline-block;
  vertical-align: middle;
}
  1. 通过伪元素 :before 实现垂直居中

平白无故添加一个无意义的参考元素不怎么好,我们可以去除作为参考的兄弟元素,转为给父元素添加一个伪元素,如下:

html 复制代码
<div class="container">
  <div class="item"></div>
</div>
css 复制代码
.container{
  width: 500px;
  height: 300px;
  background-color: pink;
  text-align: center;
}
.container::before{
  content : '';
  display: inline-block;
  vertical-align: middle;
  height: 100%;
}
.item{
  width: 100px;
  height: 100px;
  background-color: skyblue;
  vertical-align: middle;
  margin: 0 auto;
  display: inline-block;
}
  1. 通过绝对定位实现垂直居中

这种方式需要设置父元素为相对定位,子元素为绝对定位,然后配合 margin-left 为负的盒子高度一半来实现垂直居中

html 复制代码
<div class="container">
  <div class="item"></div>
</div>
css 复制代码
.container{
  width: 500px;
  height: 300px;
  background-color: pink;
  position: relative;
}
.item{
  width: 100px;
  height: 100px;
  background-color: skyblue;
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: -50px;
  margin-top: -50px;
}
  1. 通过 transform 实现垂直居中

可以通过定位配合 transform 也可以实现垂直居中

html 复制代码
<div class="container">
  <div class="item"></div>
</div>
css 复制代码
.container{
  width: 500px;
  height: 300px;
  background-color: pink;
  position: relative;
}
.item{
  width: 100px;
  height: 100px;
  background-color: skyblue;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translateX(-50px) translateY(-50px);
}
  1. 使用弹性盒子居中

通过设置父元素为弹性盒子,然后使用 justify-content: center 和 align-items: center 来设置内部盒子水平垂直居中

html 复制代码
<div class="container">
  <div class="item"></div>
</div>
css 复制代码
.container{
  width: 500px;
  height: 300px;
  background-color: pink;
  display: flex;
  justify-content: center;
  align-items: center;
}
.item{
  width: 100px;
  height: 100px;
  background-color: skyblue;
}

以上就是比较常见的盒子居中的解决方案,当然目前来讲最推荐的就是使用弹性盒子,这是目前最常用的一种方式,也是最推荐的一种方式。

真题解答

  • 怎么让一个 div 水平垂直居中

参考答案:

  1. 通过 verticle-align:middle 实现垂直居中
  2. 通过父元素设置伪元素 :before ,然后设置子元素 verticle-align:middle 实现垂直居中
  3. 通过绝对定位实现垂直居中
  4. 通过 transform 实现垂直居中
  5. 使用弹性盒子居中

-EOF-

相关推荐
inksci5 小时前
用飞帆做了个小网页
前端·javascript·css·html
zhanlang021712 小时前
HTML 零基础入门到企业实战【第八章 8.5】画布 canvas、SVG 矢量图形基础入门
前端·html
星云_byto13 小时前
CSS面试题
前端·css
zhanlang021713 小时前
HTML 零基础入门到企业实战全套教程【连载 8.4】HTML5 新增表单属性与表单控件
前端·html·html5
科研邪修法1 天前
PDF新增页面文字与文本批注怎么区分?以双版本培训讲义为例
前端·css·pdf
狮子座的男孩1 天前
html+css基础:31、响应式布局(媒体类型,媒体特性,运算符,常用阈值)
前端·css·经验分享·html·响应式布局·媒体类型/特性·常用阈值
SemiTris1 天前
Web 前端底层起源:HTML/CSS/JS、浏览器与 HTTP 完整脉络
http·html
狮子座的男孩1 天前
html+css基础:32、BFC(父子元素防止 margin 塌陷,父子元素清除浮动,兄弟元素两栏布局)
前端·css·经验分享·html·bfc·margin塌陷·清除浮动
zhanlang02171 天前
HTML 零基础入门到企业实战全套教程【第八章 8.3】HTML5 语义化标签|SEO 优化、页面布局实战应用
前端·html·html5
吴长建先生2 天前
Flutter IM 即时聊天应用开发:从 0 到 1 实
css