.out{
width: 300px;
height: 300px
}
.in{
width: 50%;
height: 50%;
display: inline-block;
vertical-align: middle
}
.box{
width: 100%;
height: 100%;
background-color: #ff0000;
}
.box span {
width: 50%;
height: 50%;
background: #000;
overflow: auto;
margin: auto;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
.box {
position: relative;
height: 500px;
background-color: #ff0000;
}
.ver-box {
position: absolute;
top: 50%;
left: 50%;
width: 300px;
height: 300px;
background: cadetblue;
transform: translate(-50%,-50%) /* 向左上偏移半个内元素宽高 */
}
.box {
position: relative;
height: 500px;
width: 1000px;
background-color: #ff0000;
}
.ver-box {
position: absolute;
top: calc(50% - 150px);
left: calc(50% - 150px);
width: 300px;
height: 300px;
background: cadetblue;
}
.box {
display: flex;
width: 300px;
height: 300px;
background: cadetblue;
justify-content: center;/* 使子元素水平居中 */
align-items: center /* 使子元素垂直居中 */
}
.ver-box {
background-color: yellow;
width: 100px;
height: 100px;
}
table-cell相当于表格的td,行内元素,无法设置宽和高,需嵌套一层(inline-block)元素
.box {
display: table;
width: 300px;
height: 300px;
background: paleturquoise
}
.ver-box {
display: table-cell;
text-align: center;
vertical-align: middle; /* 使子元素垂直居中 */
background: cadetblue /* 使子元素水平居中 */
}
.ver-box-in{
display: inline-block;
width: 30%;
height: 30%;
background: yellow
}
display : table-cell 属性(IE6/7不支持)
该属性是指让标签以单元格的形式呈现,类似 td 标签(带垂直居中对齐、关联伸缩等)
该属性不能同position、float、margin等属性同用,但可以设置 padding 值,对 width 值敏感
使用场景:列表个数不固定,需要平分容器控件空间时;多行元素垂直居中
//父级:宽度要设置 子元素设置table-cell就会自动等分
.parent{ width:100%; display:table }
.child { display: table-cell ; vertical-aligh: middle; }
内联(inline-)元素居中
{
height:400px;
line-hright:400px
}
//方法一:
.box{
width: 600px;
height: 300px;
display: flex;
flex-direction: row; /* 默认,水平居中,只要修改为column */
background-color: cadetblue
}
.ver-box{
align-self: center;
width: 50%;
height: 50%;
background-color: yellow;
}
//方法二
.box{
width: 600px;
height: 300px;
display: flex;
flex-direction: column; /* 默认为row(水平居中)*/
background-color: cadetblue;
justify-content: center
}
.parent{
position:relative
}
.child{
position:absolute;
top:50%;
height:100px;
margin-top:-50px
}
.parent{
position: relative
}
.child{
position:absolute;
top:50%;
transform:translateY(-50%)
}
//或者:(已知父元素和子元素宽高时)
.box{
position:relative;
top:50%;
transform:translateY(-50%)
}
.center{
display:-webkit-box;
-webkit-box-pack: center;
-webkit-box-aligh: center;
-webkit-box-orient: center;
text-aligh: center
}
//窗口缩小时不会被截断,滚动条出现,所有兼容
//缺点:新增了空元素
Content here
.floater {
float:left;
height:50%;
margin-bottom:-120px;
}
.content {
clear:both;
height:240px;
position:relative;
}
.box{
width: 600px;
height: 300px;
flex-direction: column;
background-color: cadetblue;
}
.box::before{
content: "";
display: inline-block;
vertical-align: middle;
height: 100%;
}
.ver-box{
width: 300px;
height: 150px;
display: inline-block;
background-color: yellow;
vertical-align: middle
}
//文档流中
{
width: 100%;
margin:0 auto;
}
//脱离文档流时
{
position: absolute;
width: 666px;
height:400px;
left: 50%;
margin-left: -333px; //margin : -333px auto;
}
.flex{
display:flex;
justify-content:center; /*横轴对齐方式 */
}
//或者:
.out{
display:flex;
flex-direction:column;
}
.in{
alight-self:center
}
行内元素:inline、inline-table、inline-block、inline-flex
4.通过table-cell和margin-left实现(父元素和子元素宽度确定)
.box{
width: 600px;
height: 300px;
display: table-cell;
flex-direction: column;
background-color: cadetblue;
justify-content: center
}
.ver-box{
width: 300px;
height: 150px;
background-color: yellow;
margin-left: 150px
}
.out{
width:300px;
height:150px;
}
.in{
width:fit-content;
height:150px;
margin:0 auto;
text-alight:center
}