元素水平垂直居中的六种方法

简介: 元素水平垂直居中的六种方法

让某个元素在水平和垂直的方向都居中 内容不限于文字,可能会是图片和其他元素

方法一:定位 + margin负值

<style>
  .father {
    width: 400px;
    height: 400px;
    background-color: pink;
    position: relative;
  }
  .son {
    width: 200px;
    height: 200px;
    background-color: aquamarine;
    position: absolute;
    left: 50%;
    top: 50%;
    margin-left: -100px;  /* 子盒子宽度的一半 */
    margin-top: -100px;   /* 子盒子高度的一半 */
  }
</style>
<div class="father">
  <div class="son"></div>
</div>

方法二:定位 + transform负值

<style>
  .father {
    width: 400px;
    height: 400px;
    background-color: pink;
    position: relative;
  }
  .son {
    width: 200px;
    height: 200px;
    background-color: aquamarine;
    position: absolute;
    left: 50%;
    top: 50%;
       transform: translate(-50%, -50%);
  }
</style>
<div class="father">
  <div class="son"></div>
</div>

方法三:定位 + margin: auto

<style>
  .father {
    width: 400px;
    height: 400px;
    background-color: pink;
    position: relative;
  }
  .son {
    width: 200px;
    height: 200px;
    background-color: aquamarine;
    position: absolute;
    /* 以下四个属性必须都要写上 */
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    margin: auto;
  }
</style>
<div class="father">
  <div class="son"></div>
</div>

方法四:flex布局

<style>
  .father {
    width: 400px;
    height: 400px;
    background-color: pink;
    display: flex;
    justify-content: center; /* 主轴居中 */
    align-items: center; /* 侧轴居中 */
  }
  .son {
    width: 200px;
    height: 200px;
    background-color: aquamarine;
  }
</style>
<div class="father">
  <div class="son"></div>
</div>

方法五:table布局

将父元素设置 display:table-cell 子元素设置 display: inline-block

<style>
  .father {
    width: 400px;
    height: 400px;
    background-color: pink;
    display: table-cell;
    vertical-align: middle;
    text-align: center;
  }
  .son {
    width: 200px;
    height: 200px;
    background-color: aquamarine;
    display: inline-block;
  }
</style>
<div class="father">
  <div class="son"></div>
</div>

方法六:grid网格布局

<style>
  .father {
    width: 400px;
    height: 400px;
    background-color: pink;
    display: grid;
    align-items: center;
    justify-content: center;
  }
  .son {
    width: 200px;
    height: 200px;
    background-color: aquamarine;
  }
</style>
<div class="father">
  <div class="son"></div>
</div>

小结

上述方法不知道元素大小 但仍然可以实现水平垂直居中的有

  • 定位 + transform负值
  • 定位 + margin: auto
  • flex布局
  • table布局
  • grid网格布局

行内元素的居中布局

水平居中

  • text-align: center
  • 父元素 display: flex justify-content: center

垂直居中

  • 单行文本:height = line-height
  • 多行文本:disaply: table-cell; vertical-align: middle

块级元素居中布局

水平居中

  • margin: 0 auto
  • 定位 + left: 50% + transform: translateX(-50%);
  • 定位 + left: 50% + margin-left: 负的自己宽度一半;

垂直居中

  • 定位 + top: 50% + transform: translateY(-50%);
  • 定位 + top: 50% + margin-top: 负的自己宽度一半
  • flex布局
相关文章
|
缓存 前端开发 JavaScript
一看就懂的gulp操作指南:让前端工作变得更加轻松(一)
一看就懂的gulp操作指南:让前端工作变得更加轻松
|
JavaScript
ant design vue 设置表格选择框,全选按钮选不全
ant design vue 设置表格选择框,全选按钮选不全
1833 0
|
JavaScript
ElementUI的el-dialog弹窗修改设置可拖拽可最大化
该方案通过自定义Vue指令实现ElementUI的`el-dialog`弹窗的拖拽和最大化功能。只需在`main.js`中引入并注册指令,然后在Vue组件中绑定指令即可。支持自定义参数控制是否允许最大化和拖拽,并提供最大化后的回调函数。具体使用方法详见示例代码。
2373 0
ElementUI的el-dialog弹窗修改设置可拖拽可最大化
|
小程序 前端开发 索引
微信小程序中的条件渲染和列表渲染,wx:if ,wx:elif,wx:else,wx:for,wx:key的使用,以及block标记和hidden属性的说明
这篇文章介绍了微信小程序中条件渲染和列表渲染的使用方法,包括wx:if、wx:elif、wx:else、wx:for、wx:key以及block标记和hidden属性的使用。
微信小程序中的条件渲染和列表渲染,wx:if ,wx:elif,wx:else,wx:for,wx:key的使用,以及block标记和hidden属性的说明
echarts的xAxis和yAxis——x轴y轴以及网格线的详细配置
echarts的xAxis和yAxis——x轴y轴以及网格线的详细配置
6108 0
|
前端开发 Java C++
面试官:用 CSS 实现一个元素的水平垂直居中,写出你能想到的所有答案
面试官:用 CSS 实现一个元素的水平垂直居中,写出你能想到的所有答案
408 0
Echarts实战案例代码(3):饼图pie之南丁格尔玫瑰图rose实现代码
Echarts实战案例代码(3):饼图pie之南丁格尔玫瑰图rose实现代码
620 0
|
API UED
深入理解 uni-app 中的加载提示:uni.showLoading
深入理解 uni-app 中的加载提示:uni.showLoading
7398 0
|
开发框架 前端开发 JavaScript
开发人员必须了解的 10 大前端开发工具
近几年里,前端技术发展十分迅速。如果您是一个网络开发爱好者,那么您一定知道一个好的前端对商业运作的重要性。这里码匠将发布这篇前端开发工具指南,向读者介绍几款开发者常用的前端开发工具,希望能对您有所帮助。
1641 2
开发人员必须了解的 10 大前端开发工具
|
存储 缓存 网络安全
OpenLDAP集成sssd同步用户并集成SSH登录
OpenLDAP集成sssd同步用户并集成SSH登录
737 2