1fy07h 发表于 2024-6-30 09:49:10

详解 CSS 居中布局技巧


    <h2 style="color: black; text-align: left; margin-bottom: 10px;">水平居中元素:</h2>通用<span style="color: black;">办法</span>,元素的宽高未知<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">方式一:CSS3 transform </p>

    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">.</span><span style="color: black;">parent</span> <span style="color: black;">{</span>
      <span style="color: black;">position</span><span style="color: black;">:</span> <span style="color: black;">relative</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>
      <span style="color: black;">.</span><span style="color: black;">child</span> <span style="color: black;">{</span>
      <span style="color: black;">position</span><span style="color: black;">:</span> <span style="color: black;">absolute</span><span style="color: black;">;</span>
      <span style="color: black;">left</span><span style="color: black;">:</span> <span style="color: black;">50</span><span style="color: black;">%</span><span style="color: black;">;</span>
      <span style="color: black;">transform</span><span style="color: black;">:</span> <span style="color: black;">translateX</span><span style="color: black;">(</span><span style="color: black;">-50</span><span style="color: black;">%</span><span style="color: black;">);</span>
      <span style="color: black;">}</span>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">方式二:Flex 布局</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">.</span><span style="color: black;">parent</span> <span style="color: black;">{</span>
      <span style="color: black;">display</span><span style="color: black;">:</span> <span style="color: black;">flex</span><span style="color: black;">;</span>
      <span style="color: black;">justify-content</span><span style="color: black;">:</span> <span style="color: black;">center</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">适用于子元素为浮动,绝对定位,内联元素,均可水平居中。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">居中的元素为常规文档流中的内联元素(display: inline)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">平常</span>的内联元素有:span, a, img, input, label 等等</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">.</span><span style="color: black;">parent</span> <span style="color: black;">{</span>
      <span style="color: black;">text-align</span><span style="color: black;">:</span> <span style="color: black;">center</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">此<span style="color: black;">办法</span><span style="color: black;">一样</span>适用于 display: inline-block 的元素。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">居中的元素为常规文档流中的块元素(display: block)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">平常</span>的块元素:div, h1~h6, table, p, ul, li 等等</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">方式一:设置 margin</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">.</span><span style="color: black;">parent</span> <span style="color: black;">{</span>
      <span style="color: black;">width</span><span style="color: black;">:</span> <span style="color: black;">100</span><span style="color: black;">%</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>
      <span style="color: black;">.</span><span style="color: black;">child</span> <span style="color: black;">{</span>
      <span style="color: black;">width</span><span style="color: black;">:</span> <span style="color: black;">600</span><span style="color: black;">px</span><span style="color: black;">;</span>
      <span style="color: black;">height</span><span style="color: black;">:</span> <span style="color: black;">50</span><span style="color: black;">px</span><span style="color: black;">;</span>
      <span style="color: black;">margin</span><span style="color: black;">:</span> <span style="color: black;">0</span> <span style="color: black;">auto</span><span style="color: black;">;</span>
      <span style="color: black;">bac<span style="color: black;">公斤</span>round</span><span style="color: black;">:</span> <span style="color: black;">#999</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">此<span style="color: black;">办法</span>只能进行水平的居中,且对浮动元素或绝对定位元素<span style="color: black;">没</span>效。</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 方式二:修改为 inline-block 属性</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">.</span><span style="color: black;">parent</span> <span style="color: black;">{</span>
      <span style="color: black;">text-align</span><span style="color: black;">:</span> <span style="color: black;">center</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>
      <span style="color: black;">.</span><span style="color: black;">child</span> <span style="color: black;">{</span>
      <span style="color: black;">display</span><span style="color: black;">:</span> <span style="color: black;">inline</span><span style="color: black;">-</span><span style="color: black;">block</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>
    </div>居中的元素为浮动元素<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">.</span><span style="color: black;">child</span> <span style="color: black;">{</span>
      <span style="color: black;">width</span><span style="color: black;">:</span> <span style="color: black;">100</span><span style="color: black;">px</span><span style="color: black;">;</span>
      <span style="color: black;">float</span><span style="color: black;">:</span> <span style="color: black;">left</span><span style="color: black;">;</span>
      <span style="color: black;">position</span><span style="color: black;">:</span> <span style="color: black;">relative</span><span style="color: black;">;</span>
      <span style="color: black;">left</span><span style="color: black;">:</span> <span style="color: black;">50</span><span style="color: black;">%</span><span style="color: black;">;</span>
      <span style="color: black;">margin-left</span><span style="color: black;">:</span> <span style="color: black;">-50</span><span style="color: black;">px</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>
    </div>居中的元素为绝对定位元素<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">方式一:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">.</span><span style="color: black;">parent</span> <span style="color: black;">{</span>
      <span style="color: black;">position</span><span style="color: black;">:</span> <span style="color: black;">relative</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>
      <span style="color: black;">.</span><span style="color: black;">child</span> <span style="color: black;">{</span>
      <span style="color: black;">position</span><span style="color: black;">:</span> <span style="color: black;">absolute</span><span style="color: black;">;</span>
      <span style="color: black;">width</span><span style="color: black;">:</span> <span style="color: black;">100</span><span style="color: black;">px</span><span style="color: black;">;</span>
      <span style="color: black;">left</span><span style="color: black;">:</span> <span style="color: black;">50</span><span style="color: black;">%</span><span style="color: black;">;</span>
      <span style="color: black;">margin-left</span><span style="color: black;">:</span> <span style="color: black;">-50</span><span style="color: black;">px</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">方式二:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">.</span><span style="color: black;">parent</span> <span style="color: black;">{</span>
      <span style="color: black;">position</span><span style="color: black;">:</span> <span style="color: black;">relative</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>
      <span style="color: black;">.</span><span style="color: black;">child</span> <span style="color: black;">{</span>
      <span style="color: black;">position</span><span style="color: black;">:</span> <span style="color: black;">absolute</span><span style="color: black;">;</span>
      <span style="color: black;">width</span><span style="color: black;">:</span> <span style="color: black;">100</span><span style="color: black;">px</span><span style="color: black;">;</span>
      <span style="color: black;">left</span><span style="color: black;">:</span> <span style="color: black;">0</span><span style="color: black;">;</span>
      <span style="color: black;">right</span><span style="color: black;">:</span> <span style="color: black;">0</span><span style="color: black;">;</span>
      <span style="color: black;">margin</span><span style="color: black;">:</span> <span style="color: black;">0</span> <span style="color: black;">auto</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>
    </div>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;">垂直居中元素:</h2>通用<span style="color: black;">办法</span>,元素的宽高未知<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">方式一:CSS3 transform</p>

    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">.</span><span style="color: black;">parent</span> <span style="color: black;">{</span>
      <span style="color: black;">position</span><span style="color: black;">:</span> <span style="color: black;">relative</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>
      <span style="color: black;">.</span><span style="color: black;">child</span> <span style="color: black;">{</span>
      <span style="color: black;">position</span><span style="color: black;">:</span> <span style="color: black;">absolute</span><span style="color: black;">;</span>
      <span style="color: black;">top</span><span style="color: black;">:</span> <span style="color: black;">50</span><span style="color: black;">%</span><span style="color: black;">;</span>
      <span style="color: black;">transform</span><span style="color: black;">:</span> <span style="color: black;">translateY</span><span style="color: black;">(</span><span style="color: black;">-50</span><span style="color: black;">%</span><span style="color: black;">);</span>
      <span style="color: black;">}</span>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">方式二:Flex 布局</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">.</span><span style="color: black;">parent</span> <span style="color: black;">{</span>
      <span style="color: black;">display</span><span style="color: black;">:</span> <span style="color: black;">flex</span><span style="color: black;">;</span>
      <span style="color: black;">align-items</span><span style="color: black;">:</span> <span style="color: black;">center</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">适用于子元素为浮动,绝对定位,内联元素,均可垂直居中。</p>
    居中元素为单行文本<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">.</span><span style="color: black;">text</span> <span style="color: black;">{</span>
      <span style="color: black;">line-height</span><span style="color: black;">:</span> <span style="color: black;">200</span><span style="color: black;">px</span><span style="color: black;">;</span>
      <span style="color: black;">height</span><span style="color: black;">:</span> <span style="color: black;">200</span><span style="color: black;">px</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">把文字的 line-height 设为文字父容器的高度,适用于<span style="color: black;">仅有</span>一行文字的<span style="color: black;">状况</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">已知元素宽高</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">方式一:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">.</span><span style="color: black;">parent</span> <span style="color: black;">{</span>
      <span style="color: black;">position</span><span style="color: black;">:</span> <span style="color: black;">relative</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>
      <span style="color: black;">.</span><span style="color: black;">child</span><span style="color: black;">{</span>
      <span style="color: black;">position</span><span style="color: black;">:</span> <span style="color: black;">absolute</span><span style="color: black;">;</span>
      <span style="color: black;">top</span><span style="color: black;">:</span> <span style="color: black;">50</span><span style="color: black;">%</span><span style="color: black;">;</span>
      <span style="color: black;">height</span><span style="color: black;">:</span> <span style="color: black;">100</span><span style="color: black;">px</span><span style="color: black;">;</span>
      <span style="color: black;">margin-top</span><span style="color: black;">:</span> <span style="color: black;">-50</span><span style="color: black;">px</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">方式二:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">.</span><span style="color: black;">parent</span> <span style="color: black;">{</span>
      <span style="color: black;">position</span><span style="color: black;">:</span> <span style="color: black;">relative</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>
      <span style="color: black;">.</span><span style="color: black;">child</span><span style="color: black;">{</span>
      <span style="color: black;">position</span><span style="color: black;">:</span> <span style="color: black;">absolute</span><span style="color: black;">;</span>
      <span style="color: black;">top</span><span style="color: black;">:</span> <span style="color: black;">0</span><span style="color: black;">;</span>
      <span style="color: black;">bottom</span><span style="color: black;">:</span> <span style="color: black;">0</span><span style="color: black;">;</span>
      <span style="color: black;">height</span><span style="color: black;">:</span> <span style="color: black;">100</span><span style="color: black;">px</span><span style="color: black;">;</span>
      <span style="color: black;">margin</span><span style="color: black;">:</span> <span style="color: black;">auto</span> <span style="color: black;">0</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>
    </div>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;">垂直居中元素:</h2>1. 绝对居中定位<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">div</span> <span style="color: black;">{</span>
      <span style="color: black;">width</span><span style="color: black;">:</span> <span style="color: black;">100</span><span style="color: black;">px</span><span style="color: black;">;</span>
      <span style="color: black;">height</span><span style="color: black;">:</span> <span style="color: black;">100</span><span style="color: black;">px</span><span style="color: black;">;</span>
      <span style="color: black;">margin</span><span style="color: black;">:</span> <span style="color: black;">auto</span><span style="color: black;">;</span>
      <span style="color: black;">position</span><span style="color: black;">:</span> <span style="color: black;">fixed</span><span style="color: black;">;</span>
      <span style="color: black;">//absolute</span> <span style="color: black;">is</span> <span style="color: black;">ok</span>
      <span style="color: black;">top</span><span style="color: black;">:</span> <span style="color: black;">0</span><span style="color: black;">;</span>
      <span style="color: black;">right</span><span style="color: black;">:</span> <span style="color: black;">0</span><span style="color: black;">;</span>
      <span style="color: black;">bottom</span><span style="color: black;">:</span> <span style="color: black;">0</span><span style="color: black;">;</span>
      <span style="color: black;">left</span><span style="color: black;">:</span> <span style="color: black;">0</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">优点:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">不仅<span style="color: black;">能够</span>实<span style="color: black;">此刻</span>正中间,还<span style="color: black;">能够</span>在正左方,正右方</p>元素的宽高支持百分比 % 属性值和 min-/max- 属性<span style="color: black;">能够</span>封装为一个公共类,可做弹出层浏览器支持性好2. 负边距居中<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">.</span><span style="color: black;">child</span> <span style="color: black;">{</span>
      <span style="color: black;">width</span><span style="color: black;">:</span> <span style="color: black;">100</span><span style="color: black;">px</span><span style="color: black;">;</span>
      <span style="color: black;">height</span><span style="color: black;">:</span> <span style="color: black;">100</span><span style="color: black;">px</span><span style="color: black;">;</span>
      <span style="color: black;">position</span><span style="color: black;">:</span> <span style="color: black;">abso</span>
    </div>




nqkk58 发表于 2024-9-30 07:25:07

真情实感,其含义为认真了、走心了的意思,是如今的饭圈常用语。

nykek5i 发表于 2024-9-30 19:08:00

我深感你的理解与共鸣,愿对话长流。
页: [1]
查看完整版本: 详解 CSS 居中布局技巧