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 麻豆精品一区二区三区免费,国产嫩草影院,欧美成人高清在线视频大全

          整合營(yíng)銷服務(wù)商

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

          免費(fèi)咨詢熱線:

          CSS 中幾種最常用的水平垂直居中的方法

          SS 是前端里面的基礎(chǔ)之一,也是非常重要的一部分,它往往決定了你所做出來(lái)的網(wǎng)頁(yè)頁(yè)面是否美觀。在設(shè)計(jì)網(wǎng)頁(yè)頁(yè)面的過(guò)程中,總會(huì)有將元素或者文字進(jìn)行水平垂直居中的要求。下面w3cschool編程獅就為大家介紹 CSS 中幾種常用到的水平垂直居中的方法。


          一、使用 margin:auto

          當(dāng)元素有給定的高度以及寬度的時(shí)候,使用 margin: auto; 元素僅會(huì)水平居中,并不會(huì)進(jìn)行垂直居中。此時(shí)就需要設(shè)置元素的 position 為 absolute,父級(jí)元素的 position 為 relative,同時(shí)元素的上下左右都需要設(shè)置為 0。

          HTML 代碼

          <div class="box">
            <div class="center1"></div>
          </div>

          CSS 代碼

          .box{
            width: 200px;
            height: 200px;
            background-color: #eee;
            position: relative;
            margin-top: 20px;
          }
          .center1{
            width: 50px;
            height: 50px;
            background-color: #00ACED;
            margin: auto;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
          }

          效果展示:



          二、使用 position:absolute

          當(dāng)已經(jīng)知道了要進(jìn)行水平垂直居中的元素的寬高時(shí),就可以通過(guò)設(shè)置 position: absolute 來(lái)實(shí)現(xiàn)。但是,使用的同時(shí)還需要結(jié)合其他屬性才完整實(shí)現(xiàn)。因?yàn)椋瑔问窃O(shè)置 absolute,上左距離均為一半,就會(huì)出現(xiàn)下面這種情況。很顯然可以看到,元素并不是完全居中,僅只有左上角的位置在中心點(diǎn)

          概念圖:

          因此想要實(shí)現(xiàn)元素完全水平垂直居中,在設(shè)置了 absolute 定位后,可以設(shè)置 margin 值為負(fù),或者使用 calc 來(lái)計(jì)算,上左距離在 50% 的基礎(chǔ)上還要減去元素本身一半的寬高。

          margin 值為負(fù)或者 calc 計(jì)算均是在已知元素寬高的情況下,假設(shè)不知道元素的寬高,那么怎么實(shí)現(xiàn)水平垂直居中呢?這里就可以使用 transform 屬性,通過(guò)坐標(biāo)位移來(lái)實(shí)現(xiàn)居中。

          CSS 代碼

          /* 結(jié)合 margin */
          .center2{
            width: 50px;
            height: 50px;
            background-color: #7FFFD4;
            position: absolute;
            left: 50%;
            top: 50%;
            margin-left: -25px;
            margin-top: -25px;
          }
          /* 結(jié)合 calc 計(jì)算*/
          .center2{
            width: 50px;
            height: 50px;
            background-color: #7FFFD4;
            position: absolute;
            left: calc(50% - 25px)
            top: calc(50% - 25px);
          }
          /* 結(jié)合 transform */
          .center2{
          width: 50px;
          height: 50px;
          background-color: #7FFFD4;
          position: absolute;
          left: 50%;
          top: 50%;
          transform: translate(-50%, -50%);
          }

          效果展示



          03

          PART

          三、使用彈性布局

          可以通過(guò)彈性布局來(lái)設(shè)置水平垂直居中,這里需要設(shè)置父級(jí)元素 display:flex; 還需要設(shè)置兩個(gè)屬性,水平布局 justify-content 以及垂直布局 align-items。

          HTML代碼

          <div class="box2">
            <div class="center4"></div>
          </div>

          CSS代碼:

          .box2{
            background-color: #eee;
            width: 200px;
            height: 200px;
            position: relative;
            margin-top: 20px ;
            display: flex;
            justify-content: center;
            align-items: center;
          }
          .center4{
            width: 50px;
            height: 50px;
            background-color: #B39873;
          }

          效果展示:


          四、文本水平對(duì)齊和行高

          前面介紹的是元素如何實(shí)現(xiàn)水平垂直居中,下面介紹的是如何將文字進(jìn)行水平垂直居中。這第一個(gè)方法也是最經(jīng)常用的,使用文本水平對(duì)齊 text-align 和行高 line-height 來(lái)實(shí)現(xiàn)的。

          HTML 代碼

          <div class="box3">
            <div class="center5">文字居中</div>
          </div>

          CSS 代碼

          .box3{
            background-color: #eee;
            width: 200px;
            height: 200px;
            margin-top: 20px;
          }
          .center5{
            text-align: center;
            line-height: 200px;
          }

          效果展示


          05

          PART

          五、使用網(wǎng)格布局

          第二個(gè)方法可以通過(guò)網(wǎng)格布局 grid 來(lái)實(shí)現(xiàn)。而這里通過(guò) grid 有兩種方式實(shí)現(xiàn),一種對(duì)元素本身屬性進(jìn)行設(shè)置,另一種在元素的父級(jí)元素中設(shè)置。兩者看上去內(nèi)容似乎差不多,不同的是在元素中設(shè)置的是 align-self 還要多了一個(gè) margin,父級(jí)元素中是 align-items。

          相關(guān)代碼:

          /* grid 元素中設(shè)置 */
          .box4{
            background-color: #eee;
            width: 200px;
            height: 200px;
            margin-top: 20px;
            display: grid;
          }
          .center6{
            align-self: center;
            justify-content: center;
            margin: auto;
          }
          /* grid 父級(jí)元素中設(shè)置 */
          .box5{
            background-color: #eee;
            width: 200px;
            height: 200px;
            margin-top: 20px;
            display: grid;
            align-items: center;
            justify-content: center;
          }
          
          

          效果展示:


          六、總結(jié)

          以上就是關(guān)于 CSS 如何將元素或者文字進(jìn)行水平垂直居中的幾種常用方法,大家還其他關(guān)于 CSS 實(shí)現(xiàn)水平垂直居中的方法嗎?請(qǐng)?jiān)谠u(píng)論區(qū)留下你的想法。

          關(guān)注w3cschool編程獅訂閱更多IT資訊、技術(shù)干貨~

          中,是我們編碼過(guò)程中最常見(jiàn)的,那么,我們平時(shí)常見(jiàn)的居中方式,下面一一羅列出來(lái),有錯(cuò)誤的地方,望碼友多多包涵并加以矯正。

          水平居中

          1、多塊級(jí)元素,設(shè)置display:inline-block;使之在一行排列,在父級(jí)樣式里,設(shè)置text-align:center;就可以實(shí)現(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;可以實(shí)現(xiàn)塊級(jí)元素內(nèi)部的內(nèi)聯(lián)元素的水平居中

          div {

          border: 1px solid red;

          width: 100px;

          height: 100px;

          text-align: center;

          }

          <div>

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

          </div>

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

          div{

          width: 100px;

          height: 100px;

          border: 1px solid;

          margin: 0 auto;

          }

          <div></div>

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

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

          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)元素(單行)

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

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

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

          3、彈性盒子(flex)

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

          分兩種,

          (1),主軸方向?yàn)樗剑苯釉O(shè)置 align-items: center;

          (2),主軸方向?yàn)榇怪?,設(shè)置flex-direction: column;改變主軸方向,然后設(shè)置justify-content: center;

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

          4、固定寬高的塊級(jí)元素

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

          position: absolute;

          left: 50%;

          top: 50%;

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

          5,未知寬高的塊級(jí)元素

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

          position: absolute;

          top: 50%;

          transform: translateY(-50%);

          水平垂直方向的居中

          1、固定寬高

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

          {

          position: absolute;

          width: 100px;

          height: 100px;

          border: 1px solid;

          left: 50%;

          top: 50%;

          margin-top: -50px;

          margin-left: -50px;

          }

          2、未知寬高

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

          {

          position: absolute;

          border: 1px solid;

          left: 50%;

          top: 50%;

          transform: translateY(-50%);

          transform: translateX(-50%);

          }

          3、彈性盒子(flex)

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

          注意:彈性盒子容器中,多行項(xiàng)目的居中方式另加計(jì)算。

          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í)的不斷深入,居中方式可以有很多種,我們要善于利用,更加明確什么情況下用怎樣的居中方式。

          信IDWEB_wysj(點(diǎn)擊關(guān)注) ◎ ◎ ◎ ◎ ◎◎◎◎◎一┳═┻︻▄

          (頁(yè)底留言開(kāi)放,歡迎來(lái)吐槽)

          ● ● ●

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

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

          1

          2

          3

          <divclass="center">

          <imgsrc="jimmy-choo-shoe.jpg"alt>

          </div>

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

          使用text-align水平居中

          有時(shí)顯而易見(jiàn)的方案是最佳的選擇:

          1

          2

          3

          4

          5

          6

          7

          8

          div.center{

          text-align: center;

          background: hsl(0, 100%, 97%);

          }

          div.centerimg

          {

          width: 33%; height: auto;

          }

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

          使用 margin: auto 居中

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

          1

          2

          3

          4

          5

          6

          7

          8

          9

          div.center{

          background: hsl(60, 100%, 97%);

          }

          div.centerimg {

          display: block;

          width: 33%;

          height: auto;

          margin: 0auto;

          }

          注意: 必須使用display: block使 margin: 0 auto對(duì)img元素生效。

          使用table-cell居中

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

          1

          2

          3

          4

          5

          <divclass="center-aligned">

          <divclass="center-core">

          <imgsrc="jimmy-choo-shoe.jpg">

          </div>

          </div>

          CSS:

          1

          2

          3

          4

          5

          6

          7

          8

          9

          10

          11

          12

          13

          .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)容垂直居中。給html和body設(shè)置高度后,也可以使元素在body垂直居中。此方法在IE8+瀏覽器上生效。

          使用absolute定位居中

          這種 方案 有非常好的跨瀏覽器支持。有一個(gè)缺點(diǎn)就是必須顯式聲明外部容器元素的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 提出了一個(gè)使用 CSS transforms 的新方案。 同樣支持水平居中和垂直居中:

          1

          2

          3

          4

          5

          6

          7

          8

          9

          10

          11

          .center{

          background: hsl(180, 100%, 97%);

          position: relative;

          min-height: 500px;

          }

          .centerimg {

          position: absolute;

          top: 50%; left: 50%;

          transform: translate(-50%, -50%);

          width: 30%; height: auto;

          }

          但是有以下幾種缺點(diǎn):

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

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

          • 外部容器需要設(shè)置height (或者用其他方式設(shè)置),因?yàn)椴荒塬@取 絕對(duì)定位 的內(nèi)容的高度。

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

          使用Flexbox居中

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

          1

          2

          3

          4

          5

          6

          7

          8

          9

          .center{

          background: hsl(240, 100%, 97%);

          display: flex;

          justify-content: center;

          align-items: center;

          }

          .centerimg {

          width: 30%; height: auto;

          }

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

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

          使用calc居中

          在某些情況下比f(wàn)lexbox更全面:

          1

          2

          3

          4

          5

          6

          7

          8

          9

          10

          11

          12

          .center{

          background: hsl(300, 100%, 97%);

          min-height: 600px;

          position: relative;

          }

          .centerimg {

          width: 40%;

          height: auto;

          position: absolute;

          top: calc(50%- 20%);

          left: calc(50%- 20%);

          }

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

          1

          2

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

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

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

          1

          2

          3

          4

          5

          6

          .centerimg {

          width: 500px; height: 500px;

          position: absolute;

          top: calc(50%- (300px/ 2));

          left: calc(50%- (300px2));

          }

          我在 這篇文章 中詳細(xì)講解了calc。

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

          1

          2

          3

          4

          5

          6

          .centerimg {

          width: 40%; height: auto;

          position: absolute;

          top: calc(50%- 20%);

          left: calc(50%- 20%);

          }

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

          干貨!免費(fèi)領(lǐng)取騰訊高級(jí)講師網(wǎng)頁(yè)設(shè)計(jì)教程


          點(diǎn)我領(lǐng)取

          點(diǎn)擊下方“閱讀原文”結(jié)交更多有才華的設(shè)計(jì)師!

          ↓↓↓


          主站蜘蛛池模板: 久久亚洲中文字幕精品一区| 国产一区二区三区露脸| 免费无码毛片一区二区APP| 97久久精品无码一区二区天美 | 国产精品亚洲一区二区三区 | 精品人妻一区二区三区四区| 亚洲视频一区调教| 久久精品国产一区二区三区不卡| 狠狠综合久久av一区二区| 久久er99热精品一区二区 | 日韩一区二区在线观看| 精品国产一区二区三区久久影院| 亚洲一区二区三区亚瑟| 亚洲国产精品一区二区成人片国内 | 一区二区三区中文| 国产主播福利精品一区二区| 在线精品视频一区二区| 国产精品一区二区久久精品| 精品少妇ay一区二区三区| 精品一区二区三区免费毛片爱 | 亚洲熟女一区二区三区| 日本一区午夜艳熟免费| 中文字幕一区二区三区在线播放 | AV鲁丝一区鲁丝二区鲁丝三区| 国产成人久久精品一区二区三区| 国精品无码一区二区三区左线| 中文字幕一精品亚洲无线一区| 国产在线不卡一区二区三区| 亚洲综合无码AV一区二区| 无码人妻AⅤ一区二区三区| 日韩人妻精品一区二区三区视频| 精品无码一区二区三区爱欲| 91视频国产一区| 四虎在线观看一区二区| 高清一区二区三区视频| 精品国产亚洲一区二区在线观看 | 精品无码综合一区| 伊人精品视频一区二区三区| 无码aⅴ精品一区二区三区浪潮 | 大伊香蕉精品一区视频在线 | 亚洲一区无码中文字幕乱码|