css居中十八式

本文详细介绍了多种使用CSS实现元素居中的方法,包括:margin auto、text-align、inline-block、flexbox、grid、absolute定位等。通过实例展示了不同场景下如何使元素水平居中、垂直居中甚至水平垂直居中。总结了适用于不同需求的居中技巧,为网页布局提供参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

在使用css设置页面样式时会经常遇到需要居中的情况,下面我总结了一些css在不同条件下实现居中的方法。大家看看就好。
为了方便显示居中效果,给父元素和子元素都设置了边框和背景样式,效果都差不多,还是截图看看吧。
在这里插入图片描述

  1. 水平居中+阻止子元素定宽+ margin
<style>
      .parent {
        border: 1px solid #000;
        background-color: rgb(197, 108, 24);
        height: 200px;
      }

      .child {
        border: 1px solid #000;
        background-color: chartreuse;
        font-size: 32px;
        width: 150px;
        margin: 0 auto;
      }
    </style>
    <div class="parent">
      <div class="child">children 子元素</div>
    </div>
  1. 水平居中+ inline-block子元素不定宽+ text-align
<style>
      .parent2 {
        border: 1px solid #000;
        background-color: rgb(197, 108, 24);
        height: 200px;
        text-align: center;
      }

      .child2 {
        border: 1px solid #000;
        background-color: chartreuse;
        font-size: 32px;
        display: inline-block;
      }
    </style>
    <div class="parent2">
      <div class="child2">children 子元素</div>
    </div>
  1. 水平居中+ inline-block子元素定宽+ text-align
<style>
      .parent3 {
        border: 1px solid #000;
        background-color: rgb(197, 108, 24);
        height: 200px;
        text-align: center;
      }

      .child3 {
        border: 1px solid #000;
        background-color: chartreuse;
        font-size: 32px;
        width: 150px;
        display: inline-block;
      }
    </style>
    <div class="parent3">
      <div class="child3">children 子元素</div>
    </div>
  1. 水平居中+内联子元素不定宽+ text-align
<style>
      .parent4 {
        border: 1px solid #000;
        background-color: rgb(197, 108, 24);
        height: 200px;
        text-align: center;
      }

      .child4 {
        border: 1px solid #000;
        background-color: chartreuse;
        font-size: 32px;
        display: inline;
      }
    </style>
    <div class="parent4">
      <div class="child4">children 子元素</div>
    </div>
  1. 水平垂直居中+ flex +子元素不定宽高
<style>
      .parent5 {
        border: 1px solid #000;
        background-color: rgb(197, 108, 24);
        height: 200px;
        display: flex;
        justify-content: center;
        align-items: center;
      }

      .child5 {
        border: 1px solid #000;
        background-color: chartreuse;
        font-size: 32px;
      }
    </style>
    <div class="parent5">
      <div class="child5">children 子元素</div>
    </div> 
  1. 水平垂直居中+ flex +子元素不定宽高+ margin
<style>
      .parent6 {
        border: 1px solid #000;
        background-color: rgb(197, 108, 24);
        height: 200px;
        display: flex;
      }

      .child6 {
        border: 1px solid #000;
        background-color: chartreuse;
        font-size: 32px;
        margin: auto;
      }
    </style>
    <div class="parent6">
      <div class="child6">children 子元素</div>
    </div> 
  1. 水平垂直居中+网格+子元素不定宽高
<style>
      .parent7 {
        border: 1px solid #000;
        background-color: rgb(197, 108, 24);
        height: 200px;
        display: grid;
        justify-content: center;
        align-items: center;
      }

      .child7 {
        border: 1px solid #000;
        background-color: chartreuse;
        font-size: 32px;
      }
    </style>
    <div class="parent7">
      <div class="child7">children 子元素</div>
    </div> 
  1. 水平垂直居中+网格+子元素不定宽高(居中属性设置在子元素上)
<style>
      .parent8 {
        border: 1px solid #000;
        background-color: rgb(197, 108, 24);
        height: 200px;
        display: grid;
      }

      .child8 {
        border: 1px solid #000;
        background-color: chartreuse;
        font-size: 32px;
        justify-self: center;
        align-self: center;
      }
    </style>
    <div class="parent8">
      <div class="child8">children 子元素</div>
    </div> 
  1. 水平垂直居中+网格+子元素不定宽高+ margin
<style>
      .parent9 {
        border: 1px solid #000;
        background-color: rgb(197, 108, 24);
        height: 200px;
        display: grid;
      }

      .child9 {
        border: 1px solid #000;
        background-color: chartreuse;
        font-size: 32px;
        margin: auto;
      }
    </style>
    <div class="parent9">
      <div class="child9">children 子元素</div>
    </div> 
  1. 水平垂直居中+ absolute +子元素定宽高+ margin
<style>
     .parent10 {
       border: 1px solid #000;
       background-color: rgb(197, 108, 24);
       height: 200px;
       position: relative;
     }

     .child10 {
       border: 1px solid #000;
       background-color: chartreuse;
       font-size: 32px;
       margin: auto;
       width: 150px;
       height: 100px;
       position: absolute;
       left: 0;
       right: 0;
       bottom: 0;
       top: 0;
     }
   </style>
   <div class="parent10">
     <div class="child10">children 子元素</div>
   </div>
  1. 水平垂直居中+ absolute +子元素不定宽高+ margin
<style>
       .parent11 {
         border: 1px solid #000;
         background-color: rgb(197, 108, 24);
         height: 200px;
         height: 200px;
         position: relative;
       }

       .child11 {
         border: 1px solid #000;
         background-color: chartreuse;
         font-size: 32px;
         margin: auto;
         position: absolute;
         width: fit-content;
         height: fit-content;
         left: 0;
         right: 0;
         bottom: 0;
         top: 0;
       }
     </style>
     <div class="parent11">
       <div class="child11">children 子元素</div>
     </div>
  1. 水平垂直居中+ absolute +子元素不定宽高+ transform
<style>
         .parent12 {
           border: 1px solid #000;
           background-color: rgb(197, 108, 24);
           height: 200px;
           position: relative;
         }

         .child12 {
           border: 1px solid #000;
           background-color: chartreuse;
           font-size: 32px;
           margin: auto;
           position: absolute;
           left: 50%;
           top: 50%;
           transform: translate(-50%, -50%);
         }
       </style>
       <div class="parent12">
         <div class="child12">children 子元素</div>
       </div>
  1. 水平垂直居中+ table-cell +子元素不定宽高+ text-align + vertical-align
<style>
          .parent13 {
            border: 1px solid #000;
            background-color: rgb(197, 108, 24);
            height: 200px;
            /* width 设置百分比会失效,如果宽度不设置就由子元素的内容宽度决定 */
            width: 1000px;
            display: table-cell;
            text-align: center;
            vertical-align: middle;
          }

          .child13 {
            border: 1px solid #000;
            background-color: chartreuse;
            font-size: 32px;
          }
        </style>
        <div class="parent13">
          <div class="child13">children 子元素</div>
        </div>
  1. 水平垂直居中+ inline / inline-block子元素不定宽高+ vertical-align
<style>
          .parent14 {
            border: 1px solid #000;
            background-color: rgb(197, 108, 24);
            height: 200px;
            text-align: center;
          }

          .parent14::before {
            content: "";
            line-height: 200px;
            font-size: 0;
          }

          .child14 {
            border: 1px solid #000;
            background-color: chartreuse;
            font-size: 32px;
            /* display: inline-block; */
            display: inline;
            vertical-align: middle
          }
        </style>
        <div class="parent14">
          <div class="child14">children 子元素</div>
        </div>
  1. 水平垂直居中+书写模式+内联/内联块子元素不定宽高+ text-align
<style>
          .parent15 {
            border: 1px solid #000;
            background-color: rgb(197, 108, 24);
            height: 200px;
            writing-mode: vertical-lr;
            text-align: center;
          }

          .child15 {
            border: 1px solid #000;
            background-color: chartreuse;
            font-size: 32px;
            writing-mode: horizontal-tb;
            /* display: inline; */
            display: inline-block;
            width: 100%;
          }
        </style>
        <div class="parent15">
          <div class="child15">children 子元素</div>
        </div>
  1. 水平垂直居中+水平垂直居中+块子元素定宽+父元素高度由子元素决定+ padding / margin
<style>
          .parent16 {
            border: 1px solid #000;
            background-color: rgb(197, 108, 24);
          }

          .child16 {
            border: 1px solid #000;
            background-color: chartreuse;
            font-size: 32px;
            width: 150px;
            /* margin: auto; */
            /* padding: 50px 0; */
            margin: 50px auto;
          }
        </style>
        <div class="parent16">
          <div class="child16">children 子元素</div>
        </div>
  1. 水平垂直居中+子元素不定宽高+父元素高度由子元素决定+ line-height
<style>
          .parent17 {
            border: 1px solid #000;
            background-color: rgb(197, 108, 24);
            text-align: center;
          }

          .child17 {
            border: 1px solid #000;
            background-color: chartreuse;
            font-size: 32px;
            /* display: inline-block; */
            display: inline;
            line-height: 200px;
          }
        </style>
        <div class="parent17">
          <div class="child17">children 子元素</div>
        </div>
  1. 正方形十字居中
<style>
          .parent18 {
            border: 1px solid #000;
            background-color: rgb(197, 108, 24);
            width: 20%;
            position: relative;
          }
          .parent18::before {
            content: "";
            display: block;
            width: 0;
            height: 0;
            padding: 50% 0;
          }

          .child18::before, .child18::after {
            content: "";
            display: block;
            position: absolute;
            background-color: chartreuse;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
          }
          .child18::before {
            width: 50%;
            height: 5%;
          }
          .child18::after {
            height: 50%;
            width: 5%;
          }
        </style>
        <div class="parent18">
          <div class="child18"></div>
        </div> 

总结

主要总结了一些日常遇到的css居中方法,其中双重的方法也可以组合使用.

<think>嗯,用户想学习如何用CSS实现水平垂直居中布局,并且希望了解最佳实践技巧。首先,我需要回忆一下常见的居中方法,并根据当前的最佳实践来推荐。 首先,用户可能已经知道一些基础的居中方法,比如使用text-align或者margin,但垂直居中通常更复杂。我需要整理几种现代且兼容性好的方法。 Flexbox布局应该是最常用的方法了,因为它简单且支持性好。只需要在父容器设置display: flex,然后使用justify-content和align-items属性就可以轻松居中过要注意可能需要考虑容器的高度,比如设置min-height或者height为100vh,特别是在全屏居中时。 Grid布局也是一个强大的工具,尤其是对于更复杂的布局。使用place-items属性可以快速实现居中,但需要注意浏览器的支持情况,虽然现在大多数现代浏览器都支持了。 绝对定位结合transform的方法在需要兼容旧浏览器时可能更合适,比如IE的情况过transform可能会有性能问题,但通常影响大。这种方法的好处是子元素需要固定尺寸,适应性较强。 还有table-cell的方法,虽然比较传统,但在一些老项目中可能还会见到。过Flexbox和Grid的出现已经让这种方法逐渐被淘汰了,过作为备选方案还是可以提一下。 另外,用户提到的最佳实践,可能需要考虑同场景下的适用性。比如,Flexbox适合单行或单列布局,Grid适合更复杂的二维布局,而绝对定位适合需要脱离文档流的情况。同时,响应式设计的需求也可能影响方法的选择。 需要检查用户提供的引用内容,里面提到了使用:before伪元素和inline-block的方法。这种方法可能属于传统技巧,虽然有效,但现代方法更简洁。所以在回答中应该提到这种方法,但优先推荐Flexbox或Grid。 还要注意用户的问题是关于水平垂直居中,所以每种方法都需要同时处理水平和垂直方向。例如,Flexbox中的justify-content和align-items都设置为center,Grid的place-items: center,以及绝对定位的top/left 50%加上transform。 可能还需要提到margin: auto在Flex容器中的使用,因为在Flex或Grid中,子元素设置margin: auto也可以实现居中,但可能如直接使用容器属性直观。 最后,总结最佳实践时,应该根据项目需求和浏览器支持情况来选择。Flexbox作为首选,Grid次之,绝对定位和transform作为备选,传统方法如table-cell或inline-block作为兼容旧浏览器的方案。 现在需要组织这些思路,按照用户的要求分点说明,使用中文,确保每个方法都有代码示例,并正确应用LaTeX格式(虽然这里可能用到数学公式,但要注意其他格式要求)。同时生成相关问题,比如同方法的优缺点,如何选择,兼容性处理等。</think>### CSS水平垂直居中布局最佳实践技巧 #### 一、Flexbox 弹性盒子(推荐首选) ```css .parent { display: flex; justify-content: center; /* 水平居中 */ align-items: center; /* 垂直居中 */ min-height: 100vh; /* 保证容器有高度 */ } ``` **特点**:代码简洁、响应式友好、支持动态尺寸元素,适合现代浏览器[^1]。 #### 二、Grid 网格布局(二维布局推荐) ```css .parent { display: grid; place-items: center; /* 同时定义行列居中 */ min-height: 100vh; } ``` **特点**:单行代码实现居中,适合复杂布局场景,现代浏览器支持良好。 #### 三、绝对定位 + Transform(兼容方案) ```css .parent { position: relative; } .child { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } ``` **特点**:支持旧版浏览器(如IE9+),元素尺寸未知时效果最佳。 #### 四、Table-Cell 传统方案(兼容旧系统) ```css .parent { display: table; width: 100%; height: 300px; } .child { display: table-cell; text-align: center; vertical-align: middle; } ``` **特点**:兼容性好但灵活性低,适合需要支持老旧浏览器的场景。 #### 五、行内块元素 + 伪类(传统垂直居中) ```css .parent::before { content: &#39;&#39;; display: inline-block; height: 100%; vertical-align: middle; } .child { display: inline-block; vertical-align: middle; } ``` **特点**:需要设定子元素高度,适合文本类内容居中[^1]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蜜友

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值