国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > CSS > 正文

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

2024-07-11 08:59:01
字體:
來源:轉載
供稿:網友

準備

創建元素

<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;}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宜川县| 屏东县| 海宁市| 区。| 鸡西市| 客服| 茌平县| 玉环县| 永胜县| 黄骅市| 沭阳县| 湛江市| 湘潭县| 东阿县| 兴隆县| 富平县| 万源市| 茌平县| 波密县| 泰和县| 泸水县| 田林县| 浪卡子县| 德清县| 安西县| 汝南县| 邯郸市| 建宁县| 新建县| 扬州市| 偃师市| 定南县| 左云县| 洛浦县| 田林县| 特克斯县| 绩溪县| 景德镇市| 宁德市| 泰和县| 吉林市|