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 在线一区视频,夜夜艹天天干,亚洲国产天堂久久精品网

          整合營銷服務商

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

          免費咨詢熱線:

          自己總結(jié)下web移動端圖片垂直居中的方法

          、移動端圖片垂直居中

          利用height與line-height相等:類似文字垂直居中的方法,這里圖片就需要吧font-size設置為0;

          這種在圖片寬高不同的時候任然可以居中;

          如果給一個div容器設置width:100%;height:100%;background:#ff0000;這時候如果不加position:absolute或者position:fixed;這個容器是不會顯示的;

          <!DOCTYPE html>

          <html>

          <head>

          <meta charset="UTF-8">

          <title>Title</title>

          <style>

          *{

          margin:0;

          padding:0;

          }

          div{

          width:100%;

          margin-top: 0px;

          height: 667px;

          line-height: 667px;

          font-size: 0;

          text-align: center;

          background-color: #A2A590;

          }

          div img{

          width:100%;

          vertical-align: middle;

          }

          </style>

          </head>

          <body>

          <div>

          <img src="1.png" alt="logo" id="logo" />

          </div>

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

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

          ● ● ●

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

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

          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 或者給內(nèi)容添加 margin-top和 margin-bottom使容器與內(nèi)容之間有一定的距離。

          使用 margin: auto 居中

          這種方式實現(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對img元素生效。

          使用table-cell居中

          使用 display: table-cell, 而不是使用table標簽; 可以實現(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設置高度后,也可以使元素在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 (或者用其他方式設置),因為不能獲取 絕對定位 的內(nèi)容的高度。

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

          使用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作為降級方案)。

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

          使用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));

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

          1

          2

          3

          4

          5

          6

          .centerimg {

          width: 500px; height: 500px;

          position: absolute;

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

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

          }

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

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

          1

          2

          3

          4

          5

          6

          .centerimg {

          width: 40%; height: auto;

          position: absolute;

          top: calc(50%- 20%);

          left: calc(50%- 20%);

          }

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

          干貨!免費領取騰訊高級講師網(wǎng)頁設計教程


          點我領取

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

          ↓↓↓

          在網(wǎng)頁中將 HTML 元素水平居中,可以使用 CSS 中的 margin: 0 auto; 屬性。以下是一種常用的實現(xiàn)方法:

          <!DOCTYPE html>

          <html lang="en">

          <head>

          <meta charset="UTF-8">

          <meta name="viewport" content="width=device-width, initial-scale=1.0">

          <title>Horizontal Centering</title>

          <style>

          .center {

          width: 300px; /* 設置元素寬度 */

          margin: 0 auto; /* 水平居中 */

          background-color: lightblue;

          padding: 20px;

          }

          </style>

          </head>

          <body>

          <div class="center">

          <p>This element is horizontally centered.</p>

          </div>

          </body>

          </html>

          在上面的示例中, center 類的元素使用了 width: 300px; 來設置寬度,然后通過 margin: 0 auto; 來實現(xiàn)水平居中。這樣,無論屏幕寬度如何變化,元素都會始終水平居中顯示。

          您也可以將此樣式應用到任何 HTML 元素(例如 div 、 span 、 p 等),以實現(xiàn)水平居中效果。


          主站蜘蛛池模板: 人妻AV中文字幕一区二区三区 | 国产精品亚洲产品一区二区三区| 大帝AV在线一区二区三区| 一区二区三区四区无限乱码| 久久久久无码国产精品一区| 亚洲国模精品一区| 国产一区在线电影| 亚洲熟女综合色一区二区三区| 成人区精品人妻一区二区不卡| 高清一区二区三区视频| 久久精品日韩一区国产二区| 久久久无码一区二区三区| 亚洲熟妇av一区二区三区| 久久99精品免费一区二区| 麻豆精品人妻一区二区三区蜜桃| 亚洲一区二区影院| 精品一区二区三区免费| 国产午夜精品一区二区三区不卡| 一区二区三区内射美女毛片| 国产精品视频免费一区二区三区| 精品人妻少妇一区二区三区不卡| 麻豆亚洲av熟女国产一区二| 台湾无码一区二区| 日韩人妻不卡一区二区三区| 无码精品国产一区二区三区免费| 秋霞午夜一区二区| 国产AV午夜精品一区二区三区| 国精品无码一区二区三区左线| 日韩人妻无码一区二区三区综合部| 中文字幕一区二区三区乱码| 三上悠亚精品一区二区久久| 国产精品丝袜一区二区三区| 亚洲一区二区电影| 亚洲午夜精品一区二区公牛电影院| 乱色精品无码一区二区国产盗| 中文字幕精品一区二区日本| 在线欧美精品一区二区三区| 夜夜高潮夜夜爽夜夜爱爱一区| 国产三级一区二区三区| 中文字幕一区二区三区有限公司| 亚洲成AV人片一区二区密柚 |