主要设置在行内元素父元素中
#box1 {
/* 水平居中 */
text-align: center;
}
#box2 #radiu{
height: 100px;
width: 100px;
border: 1px solid black;
border-radius: 50%;
background-color: yellow;
/* 水平居中 */
margin: auto;
}
注意:使用这种定位必须要知道他的高度,并且别忘记父元素设置positive:reletive
#box3 #diamond{
height: 200px;
width: 200px;
border: 1px solid black;
background-color: yellowgreen;
/* 水平居中 */
position: absolute;
left: 50%;
margin-left: -100px;
/* transform: translate(-100px); */
}
综合三种情况的代码和效果如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
body{
margin: 0;
padding: 0;
}
#box1{
height: 50px;
border: 1px solid black;
}
#box2{
height: 200px;
border: 1px solid black;
}
#box3{
height: 300px;
border: 1px solid black;
position: relative;
}
#box1 {
/* 水平居中 */
text-align: center;
/* height: 50px;
line-height:50px ; */
}
#box2 #radiu{
height: 100px;
width: 100px;
border: 1px solid black;
border-radius: 50%;
background-color: yellow;
/* 水平居中 */
margin: auto;
}
#box3 #diamond{
height: 200px;
width: 200px;
border: 1px solid black;
background-color: yellowgreen;
/* 水平居中 */
position: absolute;
left: 50%;
margin-left: -100px;
/* transform: translate(-100px); */
}
</style>
<body>
<div class="box" id="box1">
<span>提目</span>
</div>
<div class="box" id="box2">
<div id="radiu"></div>
</div>
<div class="box" id="box3">
<div id="diamond"></div>
</div>
</body>
</html>
这种就是必须要知道元素的height值,主要设置在行内元素父元素中
#box1{
/* 水平居中 */
text-align: center;
height: 50px;
line-height:50px ;
}
注意:使用这种定位必须要知道他的高度,并且别忘记父元素设置positive:reletive
#box3 #diamond{
height: 200px;
width: 200px;
border: 1px solid black;
background-color: yellowgreen;
/* 水平垂直居中 */
position: absolute;
left:50%;
margin-left:-100px
top: 50%;
margin-top: -100px;
}
这里只用考虑到transform的兼容性问题
#box3 #diamond{
height: 200px;
width: 200px;
border: 1px solid black;
background-color: yellowgreen;
/* 水平居中 */
/* 垂直居中 */
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
这个是最优的既能保持兼容性又不用考虑他的高度宽度值
#box3 #diamond{
height: 200px;
width: 200px;
border: 1px solid black;
background-color: yellowgreen;
/* 水平居中 */
/* 垂直居中 */
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: ;
}
本文地址:https://blog.csdn.net/weixin_43342105/article/details/107236347
如对本文有疑问, 点击进行留言回复!!
移动端适配布局(设备像素,独立像素和css像素的区别,以及自适应适配方案)
重定向方法sendRedirect()中的路径问题的初步了解
网友评论