nykek5i 发表于 2024-6-30 06:09:41

你会用CSS实现垂直居中吗?


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">原文链接:<a style="color: black;">Can you vertically center in CSS?</a></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">作者:<a style="color: black;">Usama Tahir</a></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">翻译:<a style="color: black;">Klaus</a></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;"><img src="https://pic1.zhimg.com/80/v2-0fd556d567f54ac98c4a393193e37060_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>用CSS(层叠样式表)实现垂直居中呢?这可能是网站<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>是什么呢?<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>会介绍<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>
    <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>,并且想让它居中,<span style="color: black;">首要</span><span style="color: black;">咱们</span><span style="color: black;">必须</span>创建一个div容器去包裹它,<span style="color: black;">而后</span>给它定义<span style="color: black;">有些</span>样式。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;">HTML
      -----<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;">img</span> <span style="color: black;">src</span><span style="color: black;">=</span><span style="color: black;">”http://bit.ly/2jeCm3H"</span> <span style="color: black;">alt</span><span style="color: black;">=</span><span style="color: black;">””</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>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">-----</span>
      <span style="color: black;">CSS</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;">#ccc</span><span style="color: black;">;</span> <span style="color: black;">/* just to make it visible in screenshots */</span>
      <span style="color: black;">}</span>
      <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;">300</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;">250</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;">#fff</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>
      <span style="color: black;">img</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>
    </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>有了一张包裹在div中的<span style="color: black;">照片</span>,<span style="color: black;">咱们</span>给<span style="color: black;">不但</span>给<span style="color: black;">照片</span>以及div元素定义了尺寸,还给div元素定义了#fff的背景色。</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>就是<span style="color: black;">照片</span>元素添加绝对定位属性。</p>
    <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;">300</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;">250</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;">#fff</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;">img</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;">position</span><span style="color: black;">:</span> <span style="color: black;">absolute</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>会将子元素的top属性设置为50%。</p>
    <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;">300</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;">250</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;">#fff</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;">img</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;">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;">}</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>给它一个负的margin-top值,这个值为你想要实现垂直居中的元素高度的一半,在本例中<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>margin-top,而是<span style="color: black;">运用</span>transform:translateY(-50%);属性。——译者注</p>
    <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;">300</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;">250</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;">#fff</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;">img</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;">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;">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;">/* half the size of image */</span>
      <span style="color: black;">}</span>
    </div>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic1.zhimg.com/80/v2-f7e83d34e8a2d0f63745f5e9ec224d94_720w.webp" style="width: 50%; margin-bottom: 20px;"></div>记住:<span style="color: black;">倘若</span>你想要<span style="color: black;">同期</span>实现水平居中




流星的美 发表于 2024-8-23 21:53:04

软文发布平台 http://www.fok120.com/

nykek5i 发表于 2024-10-7 09:33:25

同意、说得对、没错、我也是这么想的等。
页: [1]
查看完整版本: 你会用CSS实现垂直居中吗?