麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > CSS > 正文

CSS 垂直水平居中的5種最佳解決方案

2024-07-11 09:07:59
字體:
來源:轉載
供稿:網友

CSS 居中對齊

  • 代碼中均省略了瀏覽器前綴
  • 以下例子以我的個人的標準排序
  • 當然也有更多的居中處理方法 但我覺得只有這5種方法是最完善的解決方案

flex 居中

優點:可對未知高度進行居中處理

<style>    .wrap{height: 100%;display: flex; justify-content: center; align-items: center;align-content:center;}        .other{background-color: #ccc; width: 400px;height: 400px;} /* 額外的樣式 可去除 */</style><div class="wrap">    <div class="other">        <h2>這是第二層的內容 不會居中</h2>    </div></div>

position + translate 居中

優點: 可對未知高度進行居中處理、嵌套層最少

<style>    /* position 可選 absolute|fixed*/    .center{position: absolute;left: 50%;top: 50%; transform: translate(-50%,-50%);}        .other{background-color: #ccc; } /* 額外的樣式 可去除 */</style><div class="center other">    <h2>這一層的內容 不會居中</h2></div>

table-cell 居中

缺點:1. 居中層需要設置寬度(.center)。 2.外層多嵌套一層(.cell) 3. 居中層必須設置寬度(允許 %)

<style>    .wrap{display: table;width: 100%;height: 100%;}    .cell{display: table-cell;vertical-align:middle;}    .center{width: 400px;margin-left:auto;margin-right:auto;}    .other{background-color: #ccc;  height: 400px;} /* 額外的樣式 可去除 */</style><div class="wrap">    <div class="cell">        <div class="center other">            <h2>這一層的內容 不會居中</h2>        </div>    </div></div>

傳統居中 (2種)

缺點:1. margin 值必須為auto。 2. 居中層必須設置高寬(允許 %) 3. 必須使用 position

<style>    /*        1. left、top、right、bottom 可以任意,但必須相等        2. position 可選 absolute|fixed    */    .center{position: absolute;left: 10px;top: 10px;right: 10px;bottom: 10px;margin: auto;width: 400px;height: 400px;}    .other{background-color: #ccc; } /* 額外的樣式 可去除 */</style><div class="center other">    <h2>這一層的內容 不會居中</h2></div>

缺點: 居中層必須設置固定高寬,并且magin需要通過高寬計算得出。

<style>    .wrap{position: relative;height: 100%;}    .center{position: absolute;left: 50%;top: 50%; width: 400px;height: 300px; margin-left: -200px;margin-top: -150px;}    .other{background-color: #ccc; } /* 額外的樣式 可去除 */</style><div class="wrap">    <div class="center other">        <h2>這一層的內容 不會居中</h2>    </div></div>

總結

以上所述是小編給大家介紹的CSS 垂直水平居中的5種最佳解決方案,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網網站的支持!


注:相關教程知識閱讀請移步到CSS教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 日本视频免费观看 | 中文在线观看www | 亚洲xxx在线观看 | 毛片在线免费 | japanesexxxx24videofree| 一级做受毛片免费大片 | 色综合视频 | 亚洲一区成人在线 | 久久欧美亚洲另类专区91大神 | 午夜视频在线看 | 91网站在线播放 | 色播av在线 | 国产精品视频自拍 | 国产精品一区二区三区在线 | 亚州成人在线观看 | 久久久成人精品视频 | 亚州精品天堂中文字幕 | 91短视频在线播放 | 欧美成人免费小视频 | 99精彩视频在线观看 | 成人福利视频导航 | 欧洲a级片| 国产一区二区视频精品 | 亚洲一级毛片 | 久久新网址 | 免费国产羞羞网站视频 | 国产成人午夜高潮毛片 | 男女隐私免费视频 | 成人毛片免费视频 | 久久久久久久久亚洲精品 | 中文字幕免费看 | 色综合狠狠| 亚洲男人的天堂在线视频 | 国产精品午夜性视频 | 91精品国产91久久久久久蜜臀 | 欧美巨乳在线观看 | 特片网久久 | 成人午夜免费观看 | 欧美中文字幕在线视频 | 亚洲视频精品在线 | 欧美69free性videos|