l14107cb 发表于 2024-6-30 05:32:00

平常的几种 CSS 水平垂直居中处理办法


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">用CSS实现元素的水平居中,比较简单,<span style="color: black;">能够</span>设置text-align center,<span style="color: black;">或</span>设置 margin-left:auto; margin-right:auto 之类的<span style="color: black;">就可</span>。</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>接下来<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;">水平垂直居中<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;">但,<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>到的10中<span style="color: black;">办法</span>。</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">办法</span><span style="color: black;">1、</span><span style="color: black;">运用</span> line-height</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>,其中<span style="color: black;">运用</span>overflow:hidden的设置是为了防止内容超出容器<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;">style</span> <span style="color: black;">type</span><span style="color: black;">=</span><span style="color: black;">"text/css"</span><span style="color: black;">&gt;</span>
      <span style="color: black;">html</span><span style="color: black;">,</span><span style="color: black;">body</span><span style="color: black;">,</span><span style="color: black;">div</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;">;</span><span style="color: black;">padding</span><span style="color: black;">:</span> <span style="color: black;">0</span><span style="color: black;">}</span>
      <span style="color: black;">.</span><span style="color: black;">box</span> <span style="color: black;">{</span>
      <span style="color: black;">margin</span><span style="color: black;">:</span> <span style="color: black;">20</span><span style="color: black;">px</span> <span style="color: black;">auto</span><span style="color: black;">;</span>
      <span style="color: black;">width</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;">bac<span style="color: black;">公斤</span>round</span><span style="color: black;">:</span> <span style="color: black;">#ddf</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>
      <span style="color: black;">.</span><span style="color: black;">content</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;">text-align</span><span style="color: black;">:</span> <span style="color: black;">center</span><span style="color: black;">;</span>
         <span style="color: black;">overflow</span><span style="color: black;">:</span> <span style="color: black;">hidden</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>

      <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;">"box"</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;">"content"</span><span style="color: black;">&gt;</span>
      This is text
      <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>
    </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>了。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic3.zhimg.com/80/v2-9881601cb0794882a78a642d6fc17d3e_720w.webp" style="width: 50%; margin-bottom: 20px;"></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>,IE6以上<span style="color: black;">能够</span>正常居中,以下(<span style="color: black;">包含</span>IE6)则不兼容。</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>在块元素内垂直居中,ie6<span style="color: black;">没</span>效果,<span style="color: black;">由于</span>ie6中含有替换元素的行高会失效。)</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic4.zhimg.com/80/v2-cad208ce497bb248b5165f7f7667349b_720w.webp" style="width: 50%; margin-bottom: 20px;"></div>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">2、</span><span style="color: black;">运用</span> vertical-align</h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">加上这个属性,<span style="color: black;">不外</span>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;"><span style="color: black;">倘若</span>不加上那个line-height的属性的话,div会认为你vertical-align的是默认高度,而不是自定义设置的高度。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">.</span><span style="color: black;">box</span> <span style="color: black;">{</span>
      <span style="color: black;">margin</span><span style="color: black;">:</span> <span style="color: black;">20</span><span style="color: black;">px</span> <span style="color: black;">auto</span><span style="color: black;">;</span>
      <span style="color: black;">width</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;">bac<span style="color: black;">公斤</span>round</span><span style="color: black;">:</span> <span style="color: black;">#ddf</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>
      <span style="color: black;">.</span><span style="color: black;">content</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;">vertical-align</span><span style="color: black;">:</span> <span style="color: black;">middle</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;">overflow</span><span style="color: black;">:</span> <span style="color: black;">hidden</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>,IE6的<span style="color: black;">照片</span>还是有问题</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">3、</span>把容器当作表格单元</h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">table<span style="color: black;">能够</span>设置vertical-align,自然能实现居中,<span style="color: black;">因此</span><span style="color: black;">咱们</span><span style="color: black;">能够</span>模拟出一个table</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span>display:table和display:table-cell的<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>定位的文本再<span style="color: black;">增多</span>一个&lt;div&gt;元素:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">&lt;</span><span style="color: black;">style</span> <span style="color: black;">type</span><span style="color: black;">=</span><span style="color: black;">"text/css"</span><span style="color: black;">&gt;</span>
      <span style="color: black;">html</span><span style="color: black;">,</span><span style="color: black;">body</span><span style="color: black;">,</span><span style="color: black;">div</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;">;</span><span style="color: black;">padding</span><span style="color: black;">:</span> <span style="color: black;">0</span><span style="color: black;">}</span>
      <span style="color: black;">.</span><span style="color: black;">box</span> <span style="color: black;">{</span>
      <span style="color: black;">margin</span><span style="color: black;">:</span> <span style="color: black;">20</span><span style="color: black;">px</span> <span style="color: black;">auto</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;">width</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;">bac<span style="color: black;">公斤</span>round</span><span style="color: black;">:</span> <span style="color: black;">#ddf</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>
      <span style="color: black;">.</span><span style="color: black;">content</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;">cell</span><span style="color: black;">;</span>
      <span style="color: black;">vertical-align</span><span style="color: black;">:</span> <span style="color: black;">middle</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;">&lt;/</span><span style="color: black;">style</span><span style="color: black;">&gt;</span>

      <span style="color: black;">&lt;</span><span style="color: black;">div</span> <span style="color: black;">cla</span>
    </div>




听听海 发表于 2024-8-24 14:31:11

我深感你的理解与共鸣,愿对话长流。

1fy07h 发表于 2024-10-2 09:51:57

外链发布社区 http://www.fok120.com/
页: [1]
查看完整版本: 平常的几种 CSS 水平垂直居中处理办法