html如何把元素都放到页⾯居中,四种⽅法让块元素在⽹页中绝
对居中
在⼀些页⾯中需要让⼀个块元素绝对居中,例如,⼀个跳转页中的提⽰信息框就需要让其中页⾯的正中位置显⽰提⽰信息,那么如何让⼀个块元素在页⾯中绝对居中呢?下⾯我列出4种解决⽅法:
1.利⽤table中内容在单元格中默认垂直居中的特性。
块元素绝对居中
html,body,table{
margin:0;
height:100%;
}
table{
width:100%;
}
#box{
width:300px;
height:300px;
background:teal;
margin:auto;
}
2.利⽤css3中的transform属性
块元素绝对居中
*{
margin:0;
}
#box{
width:300px;
height:300px;
background:maroon;
position:absolute;
left:50%;
top:50%;
transform:translate(-50%,-50%);
}
3.利⽤margin属性
块元素绝对居中
*{
margin:0;
}
#box{
width:300px;
height:300px;
background:silver;
position:absolute;
left:50%;
top:50%;
margin-left:-150px;
margin-top:-150px;
}
4.利⽤利⽤position属性把left,top,right,bottom四个的值设为0,再⽤margin:auto;
块元素绝对居中
*{
margin:0;
}
#box{
width:300px;
height:300px;
background:pink;
position:absolute;
网页设计html代码大全居中
left:0;
right:0;
top:0;
bottom:0;
margin:auto;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。