Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 久久久影院亚洲精品,91精品国产免费久久久久久 ,在线观看免费日本

          整合營銷服務(wù)商

          電腦端+手機端+微信端=數(shù)據(jù)同步管理

          免費咨詢熱線:

          css中4種方法使內(nèi)容居中

          css中4種方法使內(nèi)容居中

          lexbox

          通常首選方法是使用flexbox居中內(nèi)容。只需三行代碼即可:display:flex,然后使用 align-items:center justify-content:center 將子元素垂直和水平居中。

          如下代碼:

          html:

          <div class="flexbox-centering">
            <div>Centered content.</div>
          </div>

          css:

          .flexbox-centering {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100px;
          }

          Grid

          使用grid(網(wǎng)格)與flexbox非常相似,也是一種常見的技術(shù),尤其是布局中已經(jīng)使用網(wǎng)格的情況下。與前一種flexbox技術(shù)的唯一區(qū)別是它顯示為柵格。

          如下代碼:

          html:

          <div class="grid-centering">
            <div class="child">Centered content.</div>
          </div>

          css:

          SS中文字居中顯示的方式有以下五種:

          使用text-align屬性設(shè)置文本的對齊方式

          將text-align屬性值設(shè)置為center可以將文本居中顯示。

          .center {
            text-align: center;
          }

          使用vertical-align屬性設(shè)置元素的垂直對齊方式

          將vertical-align屬性值設(shè)置為middle可以將文本垂直居中顯示。

          .center {
            vertical-align: middle;
          }

          使用line-height屬性設(shè)置行高

          將line-height屬性值設(shè)置為比字體大小略大的值,可以使文本在容器中垂直居中顯示。

          .center {
            line-height: 20px;
          }

          使用display

          display: flex屬性將父元素設(shè)置為彈性布局,并使用align-items: center屬性將子元素在交叉軸上居中對齊。

          .center {
            display: flex;
            align-items: center;
          }
          

          使用position

          position: absolute屬性和transform: translateY(-50%)將子元素相對于其父元素垂直居中對齊。

          // 父容器
          .center {
            position: relative;
            height: 200px;
          }
          
          // 子容器
          .center > div {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translateY(-50%) translateX(-50%);
            height: 100px;
            width: 200px;
            background-color: #ccc;
          }

          以上就是CSS中文字居中顯示的幾種方式,根據(jù)實際需求選擇合適的方式即可。

          ● ●

          在網(wǎng)頁上使 HTML 元素居中看似一件很簡單的事情. 至少在某些情況下是這樣的,但是復(fù)雜的布局往往使一些解決方案不能很好的發(fā)揮作用。

          在網(wǎng)頁布局中元素水平居中比元素垂直居中要簡單不少,同時實現(xiàn)水平居中和垂直居中往往是最難的。現(xiàn)在是響應(yīng)式設(shè)計的時代,我們很難確切的知道元素的準確高度和寬度,所以一些方案不大適用。據(jù)我所知, 在CSS中至少有六種實現(xiàn)居中的方法。我將使用下面的HTML結(jié)構(gòu)從簡單到復(fù)雜開始講解:

          <div class="center"> <img src="jimmy-choo-shoe.jpg" alt></div>

          鞋子圖片會改變,但是他們都會保持500pxX500px的大小。 HSL colors 用于使背景顏色保持一致。

          使用text-align水平居中

          有時顯而易見的方案是最佳的選擇:

          div.center { text-align: center; background: hsl(0, 100%, 97%);
          }
          div.center img { width: 33%; height: auto;}

          這種方案沒有使圖片垂直居中:你需要給<div>添加padding或者給內(nèi)容添加margin-topmargin-bottom使容器與內(nèi)容之間有一定的距離。

          使用 margin: auto 居中

          這種方式實現(xiàn)水平居中和上面使用text-align的方法有相同局限性。

          div.center { background: hsl(60, 100%, 97%);}

          div.center img { display: block; width: 33%; height: auto; margin: 0 auto;}

          注意: 必須使用display: block使margin: 0 autoimg元素生效。

          使用table-cell居中

          使用 display: table-cell, 而不是使用table標簽; 可以實現(xiàn)水平居中和垂直居中,但是這種方法需要添加額外的元素作為外部容器。

          <div class="center-aligned"> <div class="center-core"> <img src="jimmy-choo-shoe.jpg"> </div></div>

          CSS:

          .center-aligned { display: table; background: hsl(120, 100%, 97%); width: 100%;}.center-core { display: table-cell; text-align: center; vertical-align: middle;}
          .center-core img { width: 33%; height: auto;}

          注意:為了使div不折疊必須加上width: 100%,外部容器元素也需要加上一定高度使得內(nèi)容垂直居中。給htmlbody設(shè)置高度后,也可以使元素在body垂直居中。此方法在IE8+瀏覽器上生效。

          使用absolute定位居中

          這種 方案 有非常好的跨瀏覽器支持。有一個缺點就是必須顯式聲明外部容器元素的height

          .absolute-aligned { position: relative; min-height: 500px; background: hsl(200, 100%, 97%);}.absolute-aligned img { width: 50%; min-width: 200px; height: auto; overflow: auto; margin: auto; position: absolute; top: 0; left: 0; bottom: 0; right: 0;}

          Stephen在他的 博客 中演示了這種方案的幾種變化。

          使用translate居中

          Chris Coiyer 提出了一個使用 CSS transforms 的新方案。 同樣支持水平居中和垂直居中:

          .center { background: hsl(180, 100%, 97%); position: relative; min-height: 500px;}.center img { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 30%; height: auto;}

          但是有以下幾種缺點:

          • CSS transform 在部分就瀏覽器上需要使用 前綴。

          • 不支持 IE9 以下的瀏覽器。

          • 外部容器需要設(shè)置height(或者用其他方式設(shè)置),因為不能獲取 絕對定位 的內(nèi)容的高度。

          • 如果內(nèi)容包含文字,現(xiàn)在的瀏覽器合成技術(shù)會使文字模糊不清。

          使用Flexbox居中

          當(dāng)新舊語法差異和瀏覽器前綴消失時,這種方法會成為主流的居中方案。

          .center { background: hsl(240, 100%, 97%); display: flex; justify-content: center; align-items: center;}.center img { width: 30%; height: auto;}

          在很多方面 flexbox 是一種簡單的方案, 但是它有新舊兩種語法以及早期版本的IE缺乏支持 (盡管可以使用 display: table-cell作為降級方案)。

          現(xiàn)在規(guī)范已經(jīng)最終確定,現(xiàn)代瀏覽器也大都支持,我寫了一篇詳細的教程 教程。

          使用calc居中

          在某些情況下比flexbox更全面:

          .center { background: hsl(300, 100%, 97%); min-height: 600px; position: relative;}.center img { width: 40%; height: auto; position: absolute; top: calc(50% - 20%); left: calc(50% - 20%);}

          很簡單,calc允許你基于當(dāng)前的頁面布局計算尺寸。在上面的簡單計算中, 50% 是容器元素的中心點,但是如果只設(shè)置50%會使圖片的左上角對齊div的中心位置。 我們需要把圖片向左和向上各移動圖片寬高的一半。計算公式為:

          top: calc(50% - (40% / 2));left: calc(50% - (40% / 2));

          在現(xiàn)在的瀏覽其中你會發(fā)現(xiàn),這種方法更適用于當(dāng)內(nèi)容的寬高為固定尺寸:

          .center img { width: 500px; height: 500px; position: absolute; top: calc(50% - (300px / 2)); left: calc(50% - (300px – 2)); }

          我在 這篇文章 中詳細講解了calc

          這種方案和flex一樣有許多相同的缺點: 雖然在現(xiàn)代瀏覽器中有良好的支持,但是在較早的版本中仍然需要瀏覽器前綴,并且不支持IE8。

          .center img { width: 40%; height: auto; position: absolute; top: calc(50% - 20%); left: calc(50% - 20%);}

          當(dāng)然還有 其他更多的方案。理解這六種方案之后,web開發(fā)人員在面對元素居中的時候會有更多的選擇。

          關(guān)注“網(wǎng)頁設(shè)計自學(xué)平臺訂閱號回復(fù)以下|關(guān)鍵字|

          |dw教程|js教程|淘寶案例|軟件下載|搜狐案例|網(wǎng)站模板

          |ps教程|ai教程 |ui教程|騰訊案例| ae教程 |字體下載|

          |上課素材|前端特效|華潤萬家案例|最新dw案例|

          戳“閱讀原文”入群獲取最新高清前端視頻!


          主站蜘蛛池模板: 日本一区二区三区不卡视频| 久久4k岛国高清一区二区| 在线精品亚洲一区二区小说| 无码播放一区二区三区| 波多野结衣中文一区| 亚洲国产日韩在线一区| 成人午夜视频精品一区| 国产伦精品一区二区三区免.费| 无码国产精品一区二区免费式芒果| 亚洲日本一区二区三区在线不卡| 国产精品久久一区二区三区| 久久一区不卡中文字幕| 中文字幕亚洲乱码熟女一区二区| 国产在线观看91精品一区| 国产精品分类视频分类一区| 国产一区二区电影| 精品日本一区二区三区在线观看| 在线免费视频一区二区| 亚洲一区二区三区久久久久| 亚州日本乱码一区二区三区 | 亚洲国产激情在线一区| 国产自产V一区二区三区C| 精品无码综合一区| 国产成人片视频一区二区| 色欲AV无码一区二区三区| 在线观看国产一区二区三区| 国产精品一区二区毛卡片| 国产精品电影一区| 久久精品无码一区二区三区免费| 波多野结衣久久一区二区| 亚拍精品一区二区三区| 狠狠色综合一区二区| 极品少妇一区二区三区四区| 中文字幕精品一区二区| 亚洲一区二区三区写真 | 精品视频在线观看一区二区 | 精品视频一区二区三区在线观看 | 国产在线一区二区三区av| 一区二区三区在线免费观看视频| 精品福利一区3d动漫| 台湾无码AV一区二区三区|