日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

网页垂直居中的写法

發(fā)布時間:2025/7/25 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 网页垂直居中的写法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

網(wǎng)頁設(shè)計(jì)中居中的格式定義非常地常見,現(xiàn)在我就把我在工作中遇到的種種居中的情況做個小小的整理,這里主要講的是垂直居中


一、最簡單的,水平居中



XML/HTML 代碼復(fù)制內(nèi)容到剪貼板


  • ?
  • *{margin:0; padding:0}
  • .wrapper{ margin:0 auto; width:400px;border:1px solid #F00;}



  • 只要定義margin:0 auto和層寬就可以了。不過有個前提,這種代碼是xhtml中的寫法,所以文件頭一定要定義


    比較一下






  • <html>
  • <head>
  • <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  • <title>無標(biāo)題文檔</title>
  • <style>
  • *{margin:0; padding:0}
  • .a{ margin:0 auto; width:400px;border:1px solid #F00;}
  • </style>
  • ?
  • </head>
  • ?
  • <body>
  • <div class="a">aaaaaaaaaa</div>
  • </body>
  • </html>




  • <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>無標(biāo)題文檔</title>
    <style>
    *{margin:0; padding:0}
    .a{ margin:0 auto; width:400px;border:1px solid #F00;}
    </style>

    </head>

    <body>
    <div class="a">aaaaaaaaaa</div>
    </body>
    </html>









  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  • <html xmlns="http://www.w3.org/1999/xhtml">
  • <head>
  • <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  • <title>無標(biāo)題文檔</title>
  • <style>
  • *{margin:0; padding:0}
  • .a{ margin:0 auto; width:400px;border:1px solid #F00;}
  • </style>
  • ?
  • </head>
  • ?
  • <body>
  • <div class="a">aaaaaaaaaa</div>
  • </body>
  • </html>




  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>無標(biāo)題文檔</title>
    <style>
    *{margin:0; padding:0}
    .a{ margin:0 auto; width:400px;border:1px solid #F00;}
    </style>

    </head>

    <body>
    <div class="a">aaaaaaaaaa</div>
    </body>
    </html>





    二、高度和寬度都固定,且只有一行文字,將line-height和height定義成相等


    < br />


  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  • <html xmlns="http://www.w3.org/1999/xhtml">
  • <head>
  • <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  • <title>無標(biāo)題文檔</title>
  • <style>
  • *{margin:0; padding:0}
  • .wrapper{ margin:10px auto auto; width:400px; height:40px;border:1px solid #F00; text-align:center; line-height:40px}
  • </style>
  • ?
  • </head>
  • ?
  • <body>
  • <div class="wrapper">aaaaaaaaaa</div>
  • </body>
  • </html>




  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>無標(biāo)題文檔</title>
    <style>
    *{margin:0; padding:0}
    .wrapper{ margin:10px auto auto; width:400px; height:40px;border:1px solid #F00; text-align:center; line-height:40px}
    </style>

    </head>

    <body>
    <div class="wrapper">aaaaaaaaaa</div>
    </body>
    </html>




    三,層的高度自適應(yīng),將padding-top和 padding-bottom高成等值






  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  • <html xmlns="http://www.w3.org/1999/xhtml">
  • <head>
  • <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  • <title>無標(biāo)題文檔</title>
  • <style>
  • *{margin:0; padding:0}
  • .wrapper{ margin:10px auto auto; width:400px;border:1px solid #F00; text-align:center; padding-top:40px; padding-bottom:40px}
  • </style>
  • ?
  • </head>
  • ?
  • <body>
  • <div class="wrapper">aaaaaaaaaa<br />aaaaaaaaaa<br />aaaaaaaaaa<br />aaaaaaaaaa<br />aaaaaaaaaa<br /></div>
  • </body>
  • </html>




  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>無標(biāo)題文檔</title>
    <style>
    *{margin:0; padding:0}
    .wrapper{ margin:10px auto auto; width:400px;border:1px solid #F00; text-align:center; padding-top:40px; padding-bottom:40px}
    </style>

    </head>

    <body>
    <div class="wrapper">aaaaaaaaaa<br />aaaaaaaaaa<br />aaaaaaaaaa<br />aaaaaaaaaa<br />aaaaaaaaaa<br /></div>
    </body>
    </html>




    ?


    四、層的長和寬固定(這種情況多用于針對body的垂直顯水平居中)


    將position設(shè)成absolute; left:50%; top:50%; margin-left和margin-top向左和向上縮進(jìn)一半






  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  • <html xmlns="http://www.w3.org/1999/xhtml">
  • <head>
  • <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  • <title>無標(biāo)題文檔</title>
  • <style>
  • *{margin:0; padding:0}
  • .wrapper{ width:400px; height:200px;border:1px solid #F00; text-align:center;position:absolute; left:50%; top:50%; margin-left:-200px; margin-top:-100px}
  • </style>
  • ?
  • </head>
  • ?
  • <body>
  • <div class="wrapper"></div>
  • </body>
  • </html>




  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>無標(biāo)題文檔</title>
    <style>
    *{margin:0; padding:0}
    .wrapper{ width:400px; height:200px;border:1px solid #F00; text-align:center;position:absolute; left:50%; top:50%; margin-left:-200px; margin-top:-100px}
    </style>

    </head>

    <body>
    <div class="wrapper"></div>
    </body>
    </html>





    居中的情況很多種,我們在日常開發(fā)時經(jīng)常遇到層的高度固定,要使里面的多行l(wèi)i內(nèi)容垂直居中。上面的方式由于使用到了絕對定位,很多情況下我們都是通過目測的方式,將padding-top設(shè)置大概的距離,來達(dá)到所謂的“垂直居中”


    ?

    轉(zhuǎn)載于:https://www.cnblogs.com/haozime/archive/2010/01/19/2639159.html

    總結(jié)

    以上是生活随笔為你收集整理的网页垂直居中的写法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。