美文网首页
水平居中&垂直居中&水平垂直居中

水平居中&垂直居中&水平垂直居中

作者: 前端小白的摸爬滚打 | 来源:发表于2021-08-01 16:47 被阅读0次

水平居中

  1. 行内元素,在父元素中设置(不是行内元素可以设置 display: inline-block 将子元素设置为行内元素)

text-align: center

  1. 子元素设置 width+margin
      .parent {
        background-color: aquamarine
      }
      .child {
        background-color: chocolate;
        opacity: 0.5;
        width: 200px;
        margin: 0 auto;
      }
  1. 绝对定位
.parent {
        position: relative;
        background-color: aquamarine
      }
      .child {
        background-color: chocolate;
        opacity: 0.5;
        width: 200px;
        position: absolute;
        margin: 0 auto;
        left:0;
        right: 0;
      }

注意:一定要设置 margin

如果不设置 width,子元素会被拉升和父元素宽度一样

  1. 绝对定位 + translateX(不需要知道子元素的宽度)
.parent {
        position: relative;
        background-color: aquamarine
      }
      .child {
        background-color: chocolate;
        opacity: 0.5;
        width: 200px;
        position: absolute;
        left: 50%;
        transform: translateX(-50%)
      }
  1. 绝对定位+负 margin(需要知道子元素的宽度)
 .parent {
        position: relative;
        background-color: aquamarine
      }
      .child {
        background-color: chocolate;
        opacity: 0.5;
        width: 200px;
        position: absolute;
        left: 50%;
        margin-left: -100px;
      }
  1. 父元素 display: flex
.parent {
        display: flex;
        justify-content: center;
        background-color: aquamarine
      }
      .child {
        background-color: chocolate;
        opacity: 0.5;
        width: 200px;
      }
  1. 父元素 display: flex + 子元素 margin: 0 auto
.parent {
        display: flex;
        background-color: aquamarine
      }
      .child {
        background-color: chocolate;
        opacity: 0.5;
        width: 200px;
        margin: 0 auto;
      }
  1. 父元素 display: grid
.parent {
        display: grid;
        /* justify-content: center; */
        justify-items: center;
        background-color: aquamarine
      }
      .child {
        background-color: chocolate;
        opacity: 0.5;
        width: 200px;
      }
  1. 父元素 display: grid + 子元素 margin: 0 auto
.parent {
        display: grid;
        background-color: aquamarine
      }
      .child {
        background-color: chocolate;
        opacity: 0.5;
        width: 200px;
        margin: 0 auto;
      }

垂直居中

  1. 行内元素 line-height 设置为父元素的高度
.child {
        height: 200px;
        line-height: 200px;
      }
  1. flex 布局
/*方法一*/
 .parent {
        height: 200px;
        display: flex;
        flex-direction: column;
        justify-content: center;
      }

/*方法二*/
      .parent {
        height: 200px;
        display: flex;
        align-items: center;
      }
  1. 绝对定位+负 margin

注意: 需要给父元素设置高度

 .parent {
        position: relative;
        background-color: aquamarine;
        height: 500px;
      }
      .child {
        background-color: chocolate;
        position: absolute;
        height: 200px;
        top: 50%;
        margin-top: -100px;
      }
  1. 绝对定位 + translateY
.parent {
        position: relative;
        background-color: aquamarine;
        height: 500px;
      }
      .child {
        background-color: chocolate;
        position: absolute;
        height: 200px;
        top: 50%;
        transform: translateY(-50%);
      }
  1. grid 布局
.parent {
        background-color: aquamarine;
        display: grid;
        align-items: center;
        height: 200px;
      }

水平垂直居中

  1. 绝对定位 + 负 margin
.parent {
        background-color: aquamarine;
        position: relative;
        height: 600px;
      }

      .child {
        background-color: chocolate;
        position: absolute;
        width: 200px;
        height: 200px;
        top: 50%;
        left: 50%;
        margin: -100px 0 0 -100px;
      }
  1. 绝对定位 + translate
.parent {
        background-color: aquamarine;
        position: relative;
        height: 600px;
      }

      .child {
        background-color: chocolate;
        position: absolute;
        width: 200px;
        height: 200px;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%)
      }
  1. 绝对定位 + margin: auto
.parent {
        background-color: aquamarine;
        position: relative;
        height: 600px;
      }

      .child {
        background-color: chocolate;
        position: absolute;
        width: 200px;
        height: 200px;
        left: 0;
        right: 0;
        bottom: 0;
        top:0;
        margin: auto;
      }
  1. flex 布局
 .parent {
        background-color: aquamarine;
        display: flex;
        justify-content: center;
        align-items: center;
        height: 600px;
      }

      .child {
        background-color: chocolate;
        width: 200px;
        height: 200px;
      }
  1. flex 布局+ margin
.parent {
        background-color: aquamarine;
        display: flex;
        height: 600px;
      }

      .child {
        background-color: chocolate;
        width: 200px;
        height: 200px;
        margin: auto;
      }
  1. grid 布局 + margin
.parent {
        background-color: aquamarine;
        display: grid;
        height: 600px;
      }

      .child {
        background-color: chocolate;
        width: 200px;
        height: 200px;
        margin: auto;
      }
  1. grid 布局
.parent {
        background-color: aquamarine;
        display: grid;
        justify-content: center;
        align-items: center;
        height: 600px;
      }

      .child {
        background-color: chocolate;
        width: 200px;
        height: 200px;
      }

相关文章

  • CSS居中布局方案

    水平居中 垂直居中 水平垂直居中

  • 常用的居中方法

    本文将介绍的居中方法有 ,水平居中,垂直居中和水平垂直居中。 一水平居中 二水平垂直居中

  • 元素居中的方式

    1 水平居中 2 垂直居中 3 水平垂直居中

  • css居中方式总结(亲测有效)

    水平居中(行内元素水平居中、块级元素水平居中) 垂直居中 水平垂直居中 行内元素水平居中 text-align: ...

  • CSS水平垂直居中总结

    CSS水平居中、垂直居中、水平垂直居中方法总结 文字的水平居中: 单行文字的垂直居中: 让有宽度的div水平居中:...

  • 居中布局

    水平居中 垂直居中 垂直水平居中 已知元素的宽高的居中布局 定位居中布局 盒模型居中布局 图片的垂直水平居中(利用...

  • CSS图片居中(水平居中和垂直居中)

    css图片水平居中 css图片垂直居中 css图片水平垂直居中

  • 居中对齐

    行内元素居中[#hang]垂直居中[#hc]水平居中[#hs] 块级元素居中[#kuai]垂直居中[#kc]水平居...

  • css 居中

    居中有水平居中和垂直居中。 水平居中+垂直居中 flex法 position法 就是计算呗~ 参考 CSS各种居中...

  • css多行垂直水平居中--table布局大法

    ======= SEO专用 table-cell 定高水平垂直居中 不定高水平垂直居中 单行定高水平垂直居中 单行...

网友评论

      本文标题:水平居中&垂直居中&水平垂直居中

      本文链接:https://www.haomeiwen.com/subject/bvcrvltx.html