整合營銷服務商

          電腦端+手機端+微信端=數據同步管理

          免費咨詢熱線:

          css中4種方法使內容居中

          lexbox

          通常首選方法是使用flexbox居中內容。只需三行代碼即可: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(網格)與flexbox非常相似,也是一種常見的技術,尤其是布局中已經使用網格的情況下。與前一種flexbox技術的唯一區別是它顯示為柵格。

          如下代碼:

          html:

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

          css:

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

          水平居中

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

          body {

          text-align: center;

          }

          div{

          width: 100px;

          height: 100px;

          border: 1px solid;

          display: inline-block;

          }

          2、內聯元素,利用text-align:center;可以實現塊級元素內部的內聯元素的水平居中

          div {

          border: 1px solid red;

          width: 100px;

          height: 100px;

          text-align: center;

          }

          <div>

          <span>塊級元素中的行內元素的水平居中</span>

          </div>

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

          div{

          width: 100px;

          height: 100px;

          border: 1px solid;

          margin: 0 auto;

          }

          <div></div>

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

          給父級定寬定高,然后設置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、內聯元素(單行)

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

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

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

          3、彈性盒子(flex)

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

          分兩種,

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

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

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

          4、固定寬高的塊級元素

          利用父相子絕的定位原理,實現垂直居中

          position: absolute;

          left: 50%;

          top: 50%;

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

          5,未知寬高的塊級元素

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

          position: absolute;

          top: 50%;

          transform: translateY(-50%);

          水平垂直方向的居中

          1、固定寬高

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

          {

          position: absolute;

          width: 100px;

          height: 100px;

          border: 1px solid;

          left: 50%;

          top: 50%;

          margin-top: -50px;

          margin-left: -50px;

          }

          2、未知寬高

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

          {

          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;方可實現居中。

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

          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;

          }

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

          信IDWEB_wysj(點擊關注) ◎ ◎ ◎ ◎ ◎◎◎◎◎一┳═┻︻▄

          (頁底留言開放,歡迎來吐槽)

          ● ● ●

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

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

          1

          2

          3

          <divclass="center">

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

          </div>

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

          使用text-align水平居中

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

          1

          2

          3

          4

          5

          6

          7

          8

          div.center{

          text-align: center;

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

          }

          div.centerimg

          {

          width: 33%; height: auto;

          }

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

          使用 margin: auto 居中

          這種方式實現水平居中和上面使用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對img元素生效。

          使用table-cell居中

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

          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%,外部容器元素也需要加上一定高度使得內容垂直居中。給html和body設置高度后,也可以使元素在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 的新方案。 同樣支持水平居中和垂直居中:

          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;

          }

          但是有以下幾種缺點:

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

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

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

          • 如果內容包含文字,現在的瀏覽器合成技術會使文字模糊不清。

          使用Flexbox居中

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

          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 是一種簡單的方案, 但是它有新舊兩種語法以及早期版本的IE缺乏支持 (盡管可以使用 display: table-cell作為降級方案)。

          現在規范已經最終確定,現代瀏覽器也大都支持,我寫了一篇詳細的教程 教程。

          使用calc居中

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

          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%);

          }

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

          1

          2

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

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

          在現在的瀏覽其中你會發現,這種方法更適用于當內容的寬高為固定尺寸:

          1

          2

          3

          4

          5

          6

          .centerimg {

          width: 500px; height: 500px;

          position: absolute;

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

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

          }

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

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

          1

          2

          3

          4

          5

          6

          .centerimg {

          width: 40%; height: auto;

          position: absolute;

          top: calc(50%- 20%);

          left: calc(50%- 20%);

          }

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

          干貨!免費領取騰訊高級講師網頁設計教程


          點我領取

          點擊下方“閱讀原文”結交更多有才華的設計師!

          ↓↓↓


          主站蜘蛛池模板: 福利一区在线视频| 能在线观看的一区二区三区| 久久精品一区二区三区不卡| 一区国严二区亚洲三区| 91秒拍国产福利一区| 一区二区高清视频在线观看| 精品深夜AV无码一区二区| 国产第一区二区三区在线观看| 好爽毛片一区二区三区四| 国产一区二区三区露脸| 亚洲欧洲无码一区二区三区| 亚洲av午夜福利精品一区| 成人乱码一区二区三区av| 国产av一区二区精品久久凹凸| 国产成人综合一区精品| 狠狠色成人一区二区三区| 国产福利电影一区二区三区,免费久久久久久久精| 成人精品一区二区三区不卡免费看 | 国产在线一区二区杨幂| 日韩一区二区a片免费观看| 亚洲综合av一区二区三区| 国产精品熟女视频一区二区| 中文无码一区二区不卡αv| 精品视频一区二区三区免费| 福利一区福利二区| 人妻无码一区二区三区| 视频在线一区二区| 视频在线一区二区| 精品国产免费一区二区三区| 亚洲av无码一区二区三区观看| 久久久精品人妻一区二区三区四| 激情内射亚州一区二区三区爱妻| 国产伦理一区二区| 精品一区二区久久| 性色A码一区二区三区天美传媒 | 久久99精品波多结衣一区| 亚洲日本va午夜中文字幕一区| 99精品国产高清一区二区| 免费一本色道久久一区| 国产色欲AV一区二区三区| 久久国产精品免费一区二区三区|