css水平置中和垂直置中的多種解決方案

來源:互聯網
上載者:User

vertical-align是個不錯的屬性,但是這個屬性起初是針對錶格的cell的。當然這個屬性雖然對區塊層級元素不起作用,但是對行內元素比如span還是有作用的(這時候,這個屬性設定為負值元素內容就下移,正值則上移)。

  下面是比較專業的垂直置中的集中解決方案。記錄於此,以便查閱。

1、單行垂直置中

  文字在層中垂直置中vertical-align 屬性是做不到的.我們這裡有個比較巧妙的方法就是:設定height的高度與line-height的高度相同!

 example source code [www.111cn.net]
<div style="line-height:500px;height:500;">
2、層水平置中

  設定div的寬度小於父div的寬度,設定 margin:0 auto;,即可讓div置中。

 example source code [www.111cn.net]
#parentdiv
{
width: 500px;
}
#childdiv {
width: 200px;
margin:0 auto;
}
3、層中的文字水平置中

  在childdiv的css加上text-align:center;

 example source code [www.111cn.net]
#parentdiv
{
width: 500px;
}
#childdiv {
width: 200px;
margin:0 auto;
text-align:center;
}
4、div層垂直置中

 example source code [www.111cn.net]
<div style="width:275px;height:375px;border: solid red;">
<div style=" background:green;height: 375px; width: 275px; position: relative; display: table-cell; vertical-align: middle;">
     <div
           style=" background:red;position:static;position:absolute9; top: 50%;">
        <div
            style=" background:blue;position: relative; top: -50%;">
                     www.111cn.net
         </div>
      </div>
   </div>
</div>
5、div層垂直水平置中,英文超長換行

 example source code [www.111cn.net]
   <div style="float:left;width:275px;height:375px;border: solid red;">
<div
          style=" height: 375px; width: 275px; position: relative; display: table-cell; vertical-align: middle;">
          <div
           style="position:static;position:absolute9; top: 50%;">
           <div style="position: relative; top: -50%; text-align: center;">
              <div style="width: 85px;word-wrap: break-word;table-layout: fixed;word-break:break-all;margin:0 auto;">
            www.111cn.netwww.111cn.netwww.111cn.netwww.111cn.netwww.111cn.netwww.111cn.netwww.111cn.net
              </div>
                    
              </div>
          </div>
         </div>
</div>
6、div垂直滾動

 example source code [www.111cn.net]
<div
              style="width: 160px; height: 260px; overflow-y: scroll; border: 1px solid;">


              www.111cn.net
             </div>
7、垂直置中和使用text-align水平置中

 example source code [www.111cn.net]
<div style="float:left;width:275px;height:375px;border: solid red;">
<div
          style=" height: 375px; width: 275px; position: relative; display: table-cell; vertical-align: middle;">
          <div
           style="position:static;position:absolute9;top: 50%;">
           <div
            style="position: relative; top: -50%; text-align:center;">
            <div style="width: 275px;">
                      <div style="width: 160px;word-wrap: break-word;table-layout: fixed;word-break:break-all;text-align:left;">
            www.111cn.netwww.111cn.netwww.111cn.netwww.111cn.netwww.111cn.netwww.111cn.netwww.111cn.net
              </div>
              </div>
                    
              </div>
          </div>
         </div>
</div>
8、垂直置中和使用margin水平置中

 example source code [www.111cn.net]
    <div style="float:left;width:275px;height:375px;border: solid red;">
<div
          style=" height: 375px; width: 275px; position: relative; display: table-cell; vertical-align: middle;">
          <div
           style="position:static;position:absolute9; top: 50%;">
           <div
            style="position: relative; top: -50%; ">
           
                      <div style="margin:0 auto;width: 160px;word-wrap: break-word;table-layout: fixed;word-break:break-all;">
            www.111cn.netwww.111cn.netwww.111cn.netwww.111cn.netwww.111cn.netwww.111cn.netwww.111cn.netwww.111cn.net
              </div>
            
                    
              </div>
          </div>
         </div>
</div>

 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.