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 久久成人18免费网站,欧美日韩激情,一级毛片视频播放

          整合營銷服務(wù)商

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

          免費咨詢熱線:

          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:

          中,是我們編碼過程中最常見的,那么,我們平時常見的居中方式,下面一一羅列出來,有錯誤的地方,望碼友多多包涵并加以矯正。

          水平居中

          1、多塊級元素,設(shè)置display:inline-block;使之在一行排列,在父級樣式里,設(shè)置text-align:center;就可以實現(xiàn)水平居中的效果

          body {

          text-align: center;

          }

          div{

          width: 100px;

          height: 100px;

          border: 1px solid;

          display: inline-block;

          }

          2、內(nèi)聯(lián)元素,利用text-align:center;可以實現(xiàn)塊級元素內(nèi)部的內(nèi)聯(lián)元素的水平居中

          div {

          border: 1px solid red;

          width: 100px;

          height: 100px;

          text-align: center;

          }

          <div>

          <span>塊級元素中的行內(nèi)元素的水平居中</span>

          </div>

          3、塊級元素,通過把固定寬高的塊級元素的margin-left和margin-right設(shè)置為auto,方可實現(xiàn)

          div{

          width: 100px;

          height: 100px;

          border: 1px solid;

          margin: 0 auto;

          }

          <div></div>

          4、利用彈性盒子(display: flex;)

          給父級定寬定高,然后設(shè)置display: flex;以及justify-content: center;方可達到水平居中效果

          body {

          width: 500px;

          height: 500px;

          display: flex;

          justify-content: center;

          border: 1px solid red;

          }

          div {

          width: 100px;

          height: 100px;

          border: 1px solid;

          }

          <body>

          <div></div>

          </body>

          垂直居中

          1、內(nèi)聯(lián)元素(單行)

          通過設(shè)置元素的height和line-height,方可達到居中效果

          2、多行元素,利用表布局(table)

          通過給想要居中的元素的父級設(shè)置display: talbe-cell;以及vertical-align:enter;方可居中

          3、彈性盒子(flex)

          給父級設(shè)置display: flex;變成彈性盒子。

          分兩種,

          (1),主軸方向為水平,直接設(shè)置 align-items: center;

          (2),主軸方向為垂直,設(shè)置flex-direction: column;改變主軸方向,然后設(shè)置justify-content: center;

          彈性盒模型主軸不同,居中的方式也不同,靈活應(yīng)用。

          4、固定寬高的塊級元素

          利用父相子絕的定位原理,實現(xiàn)垂直居中

          position: absolute;

          left: 50%;

          top: 50%;

          margin-left: (自身高度的一半);

          5,未知寬高的塊級元素

          利用transform: translateY(-50%);方可實現(xiàn)

          position: absolute;

          top: 50%;

          transform: translateY(-50%);

          水平垂直方向的居中

          1、固定寬高

          通過margin平移整體寬高的一半,實現(xiàn)水平垂直居中

          {

          position: absolute;

          width: 100px;

          height: 100px;

          border: 1px solid;

          left: 50%;

          top: 50%;

          margin-top: -50px;

          margin-left: -50px;

          }

          2、未知寬高

          利用transform中的translate()屬性實現(xiàn)

          {

          position: absolute;

          border: 1px solid;

          left: 50%;

          top: 50%;

          transform: translateY(-50%);

          transform: translateX(-50%);

          }

          3、彈性盒子(flex)

          通過display:flex,把父級變成彈性盒模型,利用align-items: center;justify-content: center;方可實現(xiàn)居中。

          注意:彈性盒子容器中,多行項目的居中方式另加計算。

          body {

          border: 1px solid;

          width: 300px;

          height: 300px;

          position: relative;

          display: flex;

          align-items: center;

          justify-content: center;

          }

          div {

          border: 1px solid;

          width: 100px;

          height: 100px;

          }

          隨著學(xué)習(xí)的不斷深入,居中方式可以有很多種,我們要善于利用,更加明確什么情況下用怎樣的居中方式。

          容導(dǎo)讀

          當(dāng)父div的行高等于自身高度時,內(nèi)部的行內(nèi)元素會上下居中顯示。行內(nèi)塊沒有固定高度時也會上下居中顯示。所以需要對父div的 line-height 進行調(diào)整。利用定位屬性(top、left、right、bottom)百分比的模式。若為100%,則代表偏移的長度為父div的高度(寬度)的100%。定位屬性top和bottom(或是left和right)值分別設(shè)置為0,但子div有固定高度(寬度),并不能達到上下(左右)間距為0,此時給子div設(shè)置 margin:auto 會使它居中顯示。

          轉(zhuǎn)載自喜歡JS的無名小站

          例如 一個父div(w:100%;h:400px)中有一個子div(w:100px;100px;)。讓其上下左右居中。

          方法一(varticle-align

          理念

          利用表格單元格的居中屬性。

          步驟

          • 父div外層配置一個div,同時設(shè)置為表格元素 (display: table),寬度為100%

          • 父div配置為表格單元格元素 (display: table-cell)

          • 父div配置居中屬性(vertical-align: middle),使子div上下居中

          • 子div通過margin配置左右居中(margin-left:auto; margin-right:auto

          例子

          <style>
           * {margin: 0; padding: 0; box-sizing: border-box;}
           .table {display: table; width: 100%;}
           .father {display: table-cell; vertical-align: middle;}
           .son {margin: auto;}
          </style>
          <body>
           <div class="table" >
           <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;">
           <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div>
           </div>
           </div>
          </body>

          注:

          • 表格單元格比較特殊,如果只有一個單元格時,它的寬度默認會占父級(table|tr)寬度的100%;

          • table默認寬度不會撐開,需要手動配置width:100%;

          方法二(line-height)

          理念

          當(dāng)父div的行高等于自身高度時,內(nèi)部的行內(nèi)元素會上下居中顯示。行內(nèi)塊沒有固定高度時也會上下居中顯示。通過文本居中屬性text-align:center,可以使內(nèi)部行內(nèi)元素或行內(nèi)塊元素左右居中顯示。

          步驟

          • 子div設(shè)定為行內(nèi)塊元素(display:inline-block);

          • 父div設(shè)置行高(line-height)使子div上下居中

          • 父div設(shè)置文本居中(text-align:center)使子div左右居中。

          <style>
           * {margin: 0; padding: 0; box-sizing: border-box;}
           .father {line-height: 500px; text-align: center; font-size: 0;}
           .son { display: inline-block;
           /* display: inline-flex;
           display: inline-grid;
           display: inline-table; */
           }
          </style>
          <body>
           <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;">
           <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div>
           </div>
          </body>

          注: 行高如果設(shè)置為當(dāng)前父div的高度(400px)的話,有固定高度的子div并不會居中顯示的,問題出在瀏覽器默認將其當(dāng)做文本居中的,即把它當(dāng)做了一段文本(chrome默認font-size:16px;hight:21px)進行居中,沒把它當(dāng)做高度100px進行居中。所以需要對父div的line-height進行調(diào)整。以font-size:0(對應(yīng)的字體高度為0)為例子,則需要line-height增加一個子div的高度(400px + 100px;)。

          方法三(定位

          理念

          利用定位屬性(top、left、right、bottom)百分比的模式。若為100%,則代表偏移的長度為父div的高度(寬度)的100%。

          步驟

          • 父div標(biāo)記下定位(position:relative|absolute|fixed);子div絕對定位(position:absolute

          • 子div上下居中:top:50%;margin-top:-h/2; 或是 bottom:50%;margin-bottom:-h/2;

          • 子div左右居中: left:50%;margin-left:-w/2 或是 right:50%;margin-right:-w/2

          例子

          <style>
           * {margin: 0; padding: 0; box-sizing: border-box;}
           .father {position: relative;}
           .son {position: absolute;bottom:50%;margin-bottom: -50px;left: 50%;margin-left: -50px;
           }
          </style>
          <body>
           <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;">
           <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div>
           </div>
          </body>

          方法四(定位)

          理念:

          定位屬性top和bottom(或是left和right)值分別設(shè)置為0,但子div有固定高度(寬度),并不能達到上下(左右)間距為0,此時給子div設(shè)置margin:auto會使它居中顯示。

          步驟:

          • 父div標(biāo)記下定位(position:relative|absolute|fixed|sticky);子div絕對定位(position:absolute

          • 子div上下居中:top:0;bottom:0;margin-top:auto;margin-bottom:auto

          • 子div左右居中:left:0;right:0;margin-left:auto;margin-right:auto

          例子

          <style>
           * {margin: 0; padding: 0; box-sizing: border-box;}
           .father {position: relative;}
           .son {position: absolute; top: 0; bottom:0; left: 0; right: 0; margin: auto}
          </style>
          <body>
           <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;">
           <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div>
           </div>
          </body>

          方法五(flex)

          理念

          彈性盒子,自帶的一個居中功能

          例子

          <style>
           * {margin: 0; padding: 0; box-sizing: border-box;}
           .father {display: flex; align-items: center}
           .son {margin: auto}
          </style>
          <body>
           <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;">
           <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div>
           </div>
          </body>

          flex兼容性,以及存在的已知問題

          結(jié)尾

          方法二和方法三兼容性要比其它好些

          參考資料

          Can I use
          css-vertical-center-solution
          CSS實現(xiàn)垂直居中的5種方法--前端觀察


          主站蜘蛛池模板: 一区二区三区亚洲| 中文字幕一区二区三区四区| 亚洲午夜日韩高清一区| 精品一区二区三区无码免费直播| 无码乱码av天堂一区二区| 国产小仙女视频一区二区三区| 国产香蕉一区二区三区在线视频| 日韩一区二区三区在线精品| 国产在线精品一区二区夜色| 久久99精品一区二区三区| 在线视频精品一区| 中文字幕久久亚洲一区| 日韩一区精品视频一区二区 | 精品国产免费观看一区| 国产高清视频一区二区| 精品乱子伦一区二区三区高清免费播放 | 国产精品小黄鸭一区二区三区 | 亚洲变态另类一区二区三区| 日本中文一区二区三区亚洲| 亚洲av综合av一区| 亚洲AV无码一区二区三区牲色 | 国产精品揄拍一区二区久久| 国产在线观看一区二区三区| 精品久久一区二区| 精品国产a∨无码一区二区三区| 国产精品成人一区二区三区| 秋霞午夜一区二区| 国产免费av一区二区三区| 精品国产免费一区二区三区香蕉| 伊人久久大香线蕉AV一区二区| 亚洲国产一区二区视频网站| 中文字幕AV一区二区三区人妻少妇| 精品视频在线观看一区二区| 色狠狠AV一区二区三区| 亚洲中文字幕丝袜制服一区 | 亚洲日韩激情无码一区 | 国产一区二区三区国产精品| 无码欧精品亚洲日韩一区| 无码人妻精品一区二区蜜桃AV| 日日摸夜夜添一区| 亚洲国产精品一区二区久久hs|