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 一区二区中文字幕,日韩一级片韩国,一区二区免费电影

          整合營銷服務商

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

          免費咨詢熱線:

          Html5-CSS之元素的五大居中方式

          自己是一名從事了多年開發的web前端老程序員,目前辭職在做自己的web前端私人定制課程,今年年初我花了一個月整理了一份最適合2019年學習的web前端學習干貨,各種框架都有整理,送給每一位前端小伙伴,想要獲取的可以關注我的頭條號并在后臺私信我:前端,即可免費獲取

          Html5-CSS之五大居中方式

          你是不是也對元素居中的知識點很是模糊?是不是苦于找不到一個總結的通俗易懂的說明?是不是自己懶得去總結?恭喜你,搜到這篇博客! 這是鄙人在前端的學習與實踐中總結出的元素的五大居中方式,黏貼了代碼并對代碼做了解釋,希望對迷茫的有所幫助!

          下面的居中示例中,統一使用了同一個div作為父元素和p作為子元素

          設置一個div,并且設置了div的寬高邊框,div里面設置一個塊元素p,設置了它的寬高和背景色

          css居中方式1

          <!doctype html>
          <html>
          <head>
          <meta charset="utf-8">
          <title>五大居中1</title>
          <style>
          *{margin:0;}
          div{width:200px;height:300px;border:2px solid #000;margin:200px auto;
          text-align:center;font-size:0;
          }
          div p{width:100px;height:100px;background:#666;
          display:inline-block;vertical-align:middle;
          }
          div:after{content:"";display:inline-block;height:100%;vertical-align:middle;}
          </style>
          </head>
          <body>
          <div>
          	<p></p>
          </div>
          </body>
          </html>
          

          這里利用了偽元素讓子元素p在div盒子里左右水平居中只需要在它的父元素div里加text-align:center;垂直方向居中需要在父元素后面加了一個偽元素,并使得樣式為inline-block;height:100%;就是和父元素一樣高,vertical-align:middle;垂直居中,也就是p元素相對與偽元素居中,由于偽元素和div一樣高,所以相當于p元素在div里垂直居中。

          css居中方式2

          <!doctype html>
          <html>
          <head>
          <meta charset="utf-8">
          <title>五大居中2</title>
          <style>
          *{margin:0;}
          div{position:relative;width:300px;height:400px;border:1px solid #000;margin:100px auto;}
          p{position:absolute;left:0;bottom:0;right:0;top:0;margin:auto;width:100px;height:100px;background:#f99;}
          </style>
          </head>
          <body>
          <div>
          	<p></p>
          </div>
          </body>
          </html>
          

          這里利用了定位居中

          子元素p設置position:absolute脫離文檔流,默認以html作為父元素,所以我們給父元素div設置position:relative;使得p以div為父元素做位置的變動,left:0;tight:0;top:0;bottom:0;(只有設置了定位的元素才可以使用這種方式來移動),最后margin:auto;就會水平和垂直都居中。

          css居中方式3

          <!doctype html>
          <html>
          <head>
          <meta charset="utf-8">
          <title>五大居中3</title>
          <style>
          *{margin:0;}
          div{display:flex;justify-content:center;align-items:center;width:300px;height:400px;border:1px solid #000;margin:100px auto;}
          p{width:100px;height:100px;background:#f99;}
          </style>
          </head>
          <body>
          <div>
          	<p></p>
          </div>
          </body>
          </html>
          

          這里利用了彈性盒居中

          父元素div設置成彈性盒樣式,justify-content:center;主軸居中

          align-items:center;垂直居中(而且這兩個只能設置在父元素上,彈性盒知識)

          css居中方式4

          <!doctype html>
          <html>
          <head>
          <meta charset="utf-8">
          <title>五大居中4</title>
          <style>
          *{margin:0;}
          div{position:relative;width:300px;height:400px;border:1px solid #000;margin:100px auto;}
          p{width:100px;height:100px;background:#f99;position:absolute;
          left:50%;top:50%;margin:-50px 0 0 -50px;}
          </style>
          </head>
          <body>
          <div>
          	<p></p>
          </div>
          </body>
          </html>
          

          利用定位線左上角居中,然后左移子元素寬度的一半,再上移子元素高度的一半。

          css居中方式5

          <!doctype html>
          <html>
          <head>
          <meta charset="utf-8">
          <title>五大居中5</title>
          <style>
          *{margin:0;}
          div{position:relative;width:300px;height:400px;border:1px solid #000;margin:100px auto;}
          p{position:absolute;width:100px;height:100px;background:#f99;left:50%;top:50%;
          	transform:translate(-50%,-50%);}
          </style>
          </head>
          <body>
          <div>
          	<p></p>
          </div>
          </body>
          </html>
          

          利用動畫移動屬性transform

          結語

          相信看了上面的有關Html5、css的元素五大居中方式,你們就可以解決自己的小問題了,但是也要養成一個總結的好習慣。好記性不如爛筆頭!以前留下來的話語總是有他的道理。Come on!

          原文鏈接:https://blog.csdn.net/qq_38110274/article/details/102756968

          里是工作狂的聚集地

          職場

          學術

          新媒體

          設計

          極客

          專門治愈處女座強迫癥。

          本文為CSS入門

          翻譯 redman9

          原載CSS-Trick

          人們經常抱怨在 CSS 中居中元素的問題,其實這個問題并不復雜,只是因為方法眾多,需要根據情況從眾多方法中選取一個出來。接下來,我們做一個 "決定樹" 來幫我們把問題變的簡單一點。首先你需要居中:

          —— 水平 ——

          ?需要居中inline或者inline-*元素(如文字或者鏈接)?

          ? 需要居中block類的元素?

          ? 需要居中多個block元素?

          —— 垂直 ——

          ?需要居中inline或者inline-*元素(如文字或者鏈接)?

          ?需要居中block類的元素?

          ——既水平又垂直 ——

          ?固定寬高

          ?不固定寬高

          ?使用flexbox

          ● ● ●

          水平居中

          水平居中inline或者inline-*元素

          你可以輕松的在一個block元素中水平居中一個inline元素,以下代碼對inlineinline-blockinline-tableinline-flex等有效。

          .parent {
          text-align: center;
          }

          水平居中block類的元素

          block元素被設定固定寬度的情況下,可以使用設置元素margin-leftmargin-right的值為auto的方法實現水平居中。

          .child {
          width: 400px;
          margin: 0 auto;
          }

          水平居中多個block類的元素

          通過inline-block實現

          .parent {
          text-align: center;
          }
          .child {
          display: inline-block;
          text-align: left;
          }

          通過flexbox實現

          .parent {
          display: flex;
          justify-content: center;
          }

          ● ● ●

          垂直居中

          垂直居中inline或者inline-*元素

          【單行】

          inline/text元素可以簡單的用設置相同的上下padding值達到垂直居中的目的。

          .center {
          pading-top: 30px; padding-bottom: 30px;
          }

          如果因為某種原因不能使用padding的方法,你還可以設置line-height等于height來達到目的。

          .center {
          height: 100px; line-height: 100px; white-space: nowrap;
          }

          【多行】

          相同的上下padding也可以適用于此種情況,如果不能生效,你可以嘗試將該元素的父元素的display設置為table,同時該元素的display設置為table-sell,然后設置vertical-align

          .parent {
          display: table;
          width: 200px; height: 400px;
          } .child {
          display: table-cell;
          vertical-align: middle;
          }

          如果上述方法不能使用,你可以嘗試使用flexbox,一個單獨的flexbox子元素可以輕易的在其父元素中居中。謹記,這種方法需要父元素有固定的高度。

          .parent {
          display: flex; justify-content: center;
          flex-direction: column; height: 400px;
          }

          如果上述兩種方式均不能使用,你可以使用“幽靈元素”技術,這種方法采用偽元素::before撐開高度 ,文字垂直居中。

          .parent {
          position: relative;
          } .parent::before {
          content: " "; display: inline-block; height: 100%; width: 1%; vertical-align: middle;
          } .child {
          display: inline-block;
          vertical-align: middle; }

          垂直居中block類的元素

          【已知元素高度】

          .parent { 
          position: relative; } .child {
          position: absolute;
          top: 50%;
          height: 100px;
          margin-top: -50px; /* account for padding and border if not using box-sizing: border-box; */
          }

          【未知元素高度】

          .parent {
          position: relative; } .child {
          position: absolute;
          top: 50%;
          transform: translateY(-50%); }

          【使用flexbox

          .parent {
          display: flex;
          flex-direction: column;
          justify-content: center; }

          ● ● ●

          既水平又垂直

          【固定寬高】

          .parent {
          position: relative; } .child {
          width: 300px;
          height: 100px;
          padding: 20px;
          position: absolute;
          top: 50%;
          left: 50%;
          margin: -70px 0 0 -170px; }

          【不固定寬高】

          .parent { position: relative; } .child { position: absolute; top: 50%; left: 50%;
          transform: translate(-50%, -50%); }

          【使用flexbox

          .parent { display: flex; justify-content: center; align-items:center; }

          . 元素高度聲明的情況下在父容器中居中:絕對居中法

          <div class="parent">
           <div class="absolute-center"></div>
          </div>
          .parent {
           position: relative;
          }
          .absolute-center {
           position: absolute;
           margin: auto;
           top: 0;
           right: 0;
           bottom: 0;
           left: 0;
           height: 70%;
           width: 70%;
          }
          優點:
          1.跨瀏覽器,包括 IE8-10
          2.無需其他冗余標記,CSS 代碼量少
          3.完美支持圖片居中
          4.寬度高度可變,可用百分比
          缺點:
          1.必須聲明高度

          2. 負外邊距:當元素寬度高度為固定值時。設置 margin-top/margin-left 為寬度高度一 半的相反數,top:50%;left:50%

          <div class="parent">
           <div class="negative-margin-center"></div>
          </div>
          .parent {
           position: relative;
          }
          .negative-margin-center {
           position: absolute;
           left: 50%;
           top: 50%;
           margin-left: -150px;
           margin-top: -150px;
           height: 300px;
           width: 300px;
          }
          優點:
          良好的跨瀏覽器特性,兼容 IE6-7
          代碼量少
          缺點:
          不能自適應,不支持百分比尺寸和 min-/max-屬性設置
          內容可能溢出容器
          邊距大小域與 padding,box-sizing 有關

          3. CSS3 Transform 居中:

          <div class="parent">
           <div class="transform-center"></div>
          </div>
          .parent {
           position: relative;
          }
          .transform-center {
           position: absolute;
           left: 50%;
           top: 50%;
           margin: auto;
           width: 50%;
           -webkit-transform: translate(-50%, -50%);
           -moz-transform: translate(-50%, -50%);
           transform: translate(-50%, -50%);
          }
          優點:
          內容高度可變
          代碼量少
          缺點:
          IE8 不支持
          屬性需要瀏覽器廠商前綴
          可能干擾其他 transform 效果

          4. table-cell 居中:


          主站蜘蛛池模板: 日韩视频在线一区| 久久精品一区二区东京热| 日本在线观看一区二区三区| 在线精品自拍亚洲第一区| 国产精品无码亚洲一区二区三区| 无码一区二区三区亚洲人妻| 日韩十八禁一区二区久久| 中文字幕一区二区三区四区| 国产av一区二区三区日韩| 精品人妻一区二区三区浪潮在线| 在线精品亚洲一区二区| 国产成人综合亚洲一区| 国产亚洲综合一区柠檬导航| 99无码人妻一区二区三区免费| 日韩一区二区在线免费观看| 中文字幕一区二区区免| 亚洲国产情侣一区二区三区| 国产一区二区精品久久91| 国产区精品一区二区不卡中文| 精品亚洲AV无码一区二区三区| 国产成人一区二区动漫精品| 国产成人一区二区三区在线观看| 糖心vlog精品一区二区三区| 中文字幕一区二区区免| 国产成人精品一区二区A片带套| 国产伦一区二区三区高清| 亚洲高清一区二区三区 | 国产一区二区三区韩国女主播| 国产精品熟女视频一区二区| 亚洲第一区香蕉_国产a| 精品中文字幕一区在线| 久久精品黄AA片一区二区三区| 国产精品一区二区电影| 日韩AV无码久久一区二区| 人妻无码一区二区三区四区| 亚洲av综合av一区二区三区| 国产午夜精品一区二区三区嫩草| 日韩精品一区在线| 精品国产福利一区二区| 国产精品高清一区二区三区| 视频在线一区二区|