首页 > 其他分享 >[css] 小案例---用position:定位小窗口位于版面正中心

[css] 小案例---用position:定位小窗口位于版面正中心

时间:2022-12-21 10:31:13浏览次数:61  
标签:top 50% 100px height --- position margin css 200px


fixed 常用

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body{
height: 1500px;
}
*{
margin: 0;
padding: 0;
}
.box{
width: 200px;
height: 200px;
background-color: red;
position: fixed;
/* 当前盒子的边:定位于,窗口正中,注意只是边定位于中心了 */
top: 50%;
left: 50%;
/* 再偏移到中心左侧,当前盒子的一宽度及高度一半 */
margin-top: -100px;
margin-left: -100px;

}
</style>
</head>
<body>
<div class="box">文字文字</div>
</body>
</html>

未加第三步时左上角在浏览器中心:

[css] 小案例---用position:定位小窗口位于版面正中心_css


加第三步中心在浏览器中心时:

[css] 小案例---用position:定位小窗口位于版面正中心_html_02

absolute也可以,但只在第一屏,划拉划拉划拉滚动条后,红块就没了

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body{
height: 1500px;
}
*{
margin: 0;
padding: 0;
}
.box{
width: 200px;
height: 200px;
background-color: red;
position: absolute;
/* 当前盒子的边:定位于,窗口正中,注意只是边定位于中心了 */
top: 50%;
left: 50%;
/* 再偏移到中心左侧,当前盒子的一宽度及高度一半 */
margin-top: -100px;
margin-left: -100px;

}
</style>
</head>
<body>
<div class="box">文字文字</div>
</body>
</html>



标签:top,50%,100px,height,---,position,margin,css,200px
From: https://blog.51cto.com/u_12881709/5957957

相关文章