f9yx0du 发表于 2024-6-30 05:29:34

CSS 水平+垂直居中的方式


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">关于CSS 水平+垂直居中的问题,<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>,我会介绍几种方式,来实现CSS的水平+垂直居中。</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>先将body,html的高度和宽度设置为100%,其次清除默认样式,margin和padding设置为0。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">写个宽度和高度均为200px,青色的方块。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">&lt;!DOCTYPE html&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">html</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">head</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">meta</span> <span style="color: black;">charset</span><span style="color: black;">=</span><span style="color: black;">"utf-8"</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">title</span><span style="color: black;">&gt;</span>index<span style="color: black;">&lt;/</span><span style="color: black;">title</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;">html</span><span style="color: black;">,</span><span style="color: black;">body</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;">#2d3436</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;">%</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;">{</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;">.</span><span style="color: black;">box</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;">#81ecec</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;">head</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">body</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;&lt;/</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;/</span><span style="color: black;">body</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;/</span><span style="color: black;">html</span><span style="color: black;">&gt;</span>
    </div>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic2.zhimg.com/80/v2-43637f8b5ba84ff6fd5f821d8b549959_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>用 margin: 0 auto;来设置。</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>元素平时的默认状态是position = static (静态)。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">垂直居中<span style="color: black;">必须</span>使 position = relative ,<span style="color: black;">这般</span>就<span style="color: black;">能够</span>用top 属性,使其移动。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">另<span style="color: black;">运用</span>margin-top属性,<span style="color: black;">由于</span><span style="color: black;">自己</span>是200px高度,<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;!DOCTYPE html&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">html</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">head</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">meta</span> <span style="color: black;">charset</span><span style="color: black;">=</span><span style="color: black;">"utf-8"</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">title</span><span style="color: black;">&gt;</span>index<span style="color: black;">&lt;/</span><span style="color: black;">title</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;">body</span><span style="color: black;">,</span><span style="color: black;">html</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;">#2d3436</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;">%</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;">{</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;">.</span><span style="color: black;">box</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;">#81ecec</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>
      <span style="color: black;">.</span><span style="color: black;">loca</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;">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;">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;">/* 偏移 */</span>
      <span style="color: black;">margin-top</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;">}</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;">head</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">body</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 loca"</span><span style="color: black;">&gt;&lt;/</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;/</span><span style="color: black;">body</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;/</span><span style="color: black;">html</span><span style="color: black;">&gt;</span>
    </div>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic2.zhimg.com/80/v2-e7528c2f975e58fc74b35d70aab64d75_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>就实现垂直居中的效果。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">CSS3的transform的属性<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;">transform:translateY(-50%) 意思是偏移<span style="color: black;">自己</span>高度的一半。</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;">&lt;!DOCTYPE html&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">html</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">head</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">meta</span> <span style="color: black;">charset</span><span style="color: black;">=</span><span style="color: black;">"utf-8"</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">title</span><span style="color: black;">&gt;</span>index<span style="color: black;">&lt;/</span><span style="color: black;">title</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;">body</span><span style="color: black;">,</span><span style="color: black;">html</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;">#2d3436</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;">%</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;">{</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;">.</span><span style="color: black;">box</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;">#81ecec</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>
      <span style="color: black;">.</span><span style="color: black;">loca</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;">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;">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;">/* 偏移 */</span>
      <span style="color: black;">/* margin-top:-100px; */</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>
      <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;">head</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">body</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 loca"</span><span style="color: black;">&gt;&lt;/</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;/</span><span style="color: black;">body</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;/</span><span style="color: black;">html</span><span style="color: black;">&gt;</span>
    </div>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic2.zhimg.com/80/v2-95b6ca8533127b71e8552ac0df7ecc9d_720w.webp" style="width: 50%; margin-bottom: 20px;"></div>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><a style="color: black;"><span style="color: black;"><span style="color: black;">水平垂直居中</span><span style="color: black;"><span style="color: black;">​</span>js.jirengu.com/qoloyemuno/1/edit?html,output</span></span><span style="color: black;"><img src="https://pic1.zhimg.com/v2-431c5bc6e396131dd6a529c496785f78_ipico.jpg" style="width: 50%; margin-bottom: 20px;"></span></a></div>
    <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;"><span style="color: black;">另一</span>CSS3中还有种<span style="color: black;">办法</span> FLEX布局,实现水平垂直居中的<span style="color: black;">办法</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">htt</span></a>p://www.<span style="color: black;">ruanyifeng.com/blog/201</span><span style="color: black;">5/07/flex-grammar.html</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;!DOCTYPE html&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">html</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">head</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">meta</span> <span style="color: black;">charset</span><span style="color: black;">=</span><span style="color: black;">"utf-8"</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">title</span><span style="color: black;">&gt;</span>index<span style="color: black;">&lt;/</span><span style="color: black;">title</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;">body</span><span style="color: black;">,</span><span style="color: black;">html</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;">#2d3436</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;">%</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;">{</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;">body</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;">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;">.</span><span style="color: black;">box</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;">#81ecec</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;">head</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">body</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;&lt;/</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;/</span><span style="color: black;">body</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;/</span><span style="color: black;">html</span><span style="color: black;">&gt;</span>
    </div>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic2.zhimg.com/80/v2-95b6ca8533127b71e8552ac0df7ecc9d_720w.webp" style="width: 50%; margin-bottom: 20px;"></div>




akbchina.cn 发表于 2024-8-27 21:01:20

可以发布外链的网站 http://www.fok120.com/

nqkk58 发表于 2024-10-3 02:35:01

这篇文章真的让我受益匪浅,外链发布感谢分享!

m5k1umn 发表于 4 天前

楼主发的这篇帖子,我觉得非常有道理。
页: [1]
查看完整版本: CSS 水平+垂直居中的方式