最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

CSS水平垂直居中解決方案(6種)

  發(fā)布時間:2018-02-10 15:11:32   作者:我是南方大漢   我要評論
這篇文章主要介紹了CSS水平垂直居中解決方案(6種)的相關(guān)資料,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

準備

創(chuàng)建元素

<div class="parent">
  <div class="child">child</div>
</div>

垂直水平居中方案一:知道寬度的情況下 absolute+margin負值

.parent {
  width:400px;
  height:400px;
  background: red;
  position: relative;
}
.child {
  position: absolute;
  left:50%;
  top:50%;
  background: yellow;
  width:50px;
  height:50px;
  margin-left:-25px;
  margin-top:-25px;
}

垂直水平居中方案二:不知道寬高的情況下 absolute+transform

.parent {
  width:400px;
  height:400px;
  background: red;
  position: relative;
}
.child {
  position: absolute;
  left:50%;
  top:50%;
  transform: translate(-50%,-50%);
}

垂直居中方案三:position+margin:auto

.parent {
  position:relative;
  width:200px;
  height:200px;
  background: red;
}
.child {
  width:80px;
  height:40px;
  background: yellow;
  position: absolute;
  left:0;
  top:0;
  right:0 ;
  bottom:0;
  margin:auto;
}

垂直居中方案四:+ 多行文本的垂直居中 :table-cell+vertical-align:middle;

.parent {
    height: 300px;
    width:400px;
    border: 1px solid red;
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}
.child {
  display: inline-block;
  width:50px;
  height:50px;
  background: blue;
}
/* 或者 */
.parent {
    width: 400px;
    height: 300px;
    display: table-cell;
    vertical-align: middle;
    border: 1px solid red;
    text-align: center;
}
.child {
    display: inline-block;
    vertical-align: middle;
    background: blue;
}

垂直居中方案五:display: flex

.parent {
  width:400px;
  height:200px;
  background:red;
  display: flex;
  justify-content:center;
  align-items:center;
}
.child {
  height:100px;
  width:100px;
  background:green;
}

垂直居中方案六:偽元素

.parent {
  width:200px;
  height:200px;
  background:red;
  text-align: center;
}
.child {
  height:100px;
  width:100px;
  background:yellow;
  display: inline-block;
  vertical-align: middle;
}
.parent:before {
  content:"";
  height:100%;
  vertical-align: middle;
  display: inline-block;
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

宝应县| 永顺县| 蒙阴县| 祁门县| 锡林浩特市| 长宁县| 南平市| 鄂州市| 孟州市| 宜兴市| 寻乌县| 贡觉县| 兰考县| 永年县| 祁门县| 东兴市| 五峰| 星座| 泰来县| 松滋市| 潼关县| 白朗县| 翼城县| 佳木斯市| 白河县| 志丹县| 丽江市| 连云港市| 班玛县| 高台县| 仙桃市| 富源县| 赞皇县| 河曲县| 苏州市| 舒城县| 崇左市| 松阳县| 新营市| 丹阳市| 即墨市|