219mze 发表于 2024-6-30 09:55:18

CSS 怎么样居中一个元素


    <h2 style="color: black; text-align: left; margin-bottom: 10px;">前言</h2>
    <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>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic2.zhimg.com/80/v2-9a639a296d535ffb68380027eb7172e5_720w.webp" style="width: 50%; margin-bottom: 20px;"></div>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">1、</span>水平居中</h2>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">1.行内元素水平居中</h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">利用 text-align: center <span style="color: black;">能够</span>实<span style="color: black;">此刻</span>块级元素内部的行内元素水平居中。此<span style="color: black;">办法</span>对inline、inline-block、inline-table和inline-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;">//在父容器设置</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>块级元素内部包着<span style="color: black;">亦</span>是一个块级元素,<span style="color: black;">咱们</span><span style="color: black;">能够</span>先将其由块级元素改变为行内块元素,再<span style="color: black;">经过</span>设置行内块元素居中以达到水平居中。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">&lt;</span><span style="color: black;">div</span> <span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">"parent"</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">div</span> <span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">"child"</span><span style="color: black;">&gt;</span>Demo<span style="color: black;">&lt;/</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;/</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">style</span><span style="color: black;">&gt;</span>
      <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>
      <span style="color: black;">&lt;/</span><span style="color: black;">style</span><span style="color: black;">&gt;</span>
    </div>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">2.块级元素的水平居中</h3>
    <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>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">①将该块级元素<span style="color: black;">上下</span>外边距margin-left和margin-right设置为auto</h3>
    <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;">//<span style="color: black;">保证</span>该块级元素定宽</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>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">②<span style="color: black;">运用</span>table+margin</h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">先将子元素设置为块级表格来<span style="color: black;">表示</span>(类似),再将其设置水平居中</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">display:table在表现上类似block元素,<span style="color: black;">然则</span>宽度为内容宽。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">&lt;</span><span style="color: black;">div</span> <span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">"parent"</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">div</span> <span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">"child"</span><span style="color: black;">&gt;</span>Demo<span style="color: black;">&lt;/</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;/</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">style</span><span style="color: black;">&gt;</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;">table</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>
      <span style="color: black;">&lt;/</span><span style="color: black;">style</span><span style="color: black;">&gt;</span>
    </div>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">③<span style="color: black;">运用</span>absolute+transform</h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">先将父元素设置为相对定位,再将子元素设置为绝对定位,向右移动子元素,移动距离为父容器的一半,最后<span style="color: black;">经过</span>向左移动子元素的一半宽度以达到水平居中。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">&lt;</span><span style="color: black;">div</span> <span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">"parent"</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">div</span> <span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">"child"</span><span style="color: black;">&gt;</span>Demo<span style="color: black;">&lt;/</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;/</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">style</span><span style="color: black;">&gt;</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>
      <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;">&lt;/</span><span style="color: black;">style</span><span style="color: black;">&gt;</span>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">不外</span>transform属于css3内容,兼容性存在<span style="color: black;">必定</span>问题,高版本浏览器<span style="color: black;">必须</span>添加<span style="color: black;">有些</span>前缀。</p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">④<span style="color: black;">运用</span>flex+justify-content</h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">经过</span>CSS3中的布局利器flex中的justify-content属性来达到水平居中。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">&lt;</span><span style="color: black;">div</span> <span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">"parent"</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">div</span> <span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">"child"</span><span style="color: black;">&gt;</span>Demo<span style="color: black;">&lt;/</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;/</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">style</span><span style="color: black;">&gt;</span>
      <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>
      <span style="color: black;">&lt;/</span><span style="color: black;">style</span><span style="color: black;">&gt;</span>
    </div>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">⑤<span style="color: black;">运用</span>flex+margin</h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">经过</span>flex将父容器设置为为Flex布局,再设置子元素居中。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">&lt;</span><span style="color: black;">div</span> <span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">"parent"</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">div</span> <span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">"child"</span><span style="color: black;">&gt;</span>Demo<span style="color: black;">&lt;/</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;/</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">style</span><span style="color: black;">&gt;</span>
      <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;">}</span>
      <span style="color: black;">.</span><span style="color: black;">child</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>
      <span style="color: black;">&lt;/</span><span style="color: black;">style</span><span style="color: black;">&gt;</span>
    </div>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">3.多块级元素水平居中</h3>




情迷布拉格 发表于 2024-9-3 16:15:57

你的留言真是温暖如春,让我感受到了无尽的支持与鼓励。

1fy07h 发表于 2024-9-30 04:08:12

“BS”(鄙视的缩写)‌
页: [1]
查看完整版本: CSS 怎么样居中一个元素