就是我在页面中做了一个div的展示框,但是左边有个导航栏,这样我就没办法直接在页面居中了,居中以后导航栏一打开就不对称,请问我要怎么做一个相对定位,或者怎么才能在页面中除掉导航栏以后在剩下的空间做居中,感谢
其实无需相对定位。 建议用 flex来吧
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
html,body {
padding: 0;
margin: 0;
height: 100%;
}
.root {
display: flex;
height: 100%;
background-color: red;
}
.nav {
width: 120px;
background-color: blue;
}
.content {
flex: 1;
background-color: green;
display: flex;
align-items: center;
justify-content: center;
}
.box {
width: 120px;
height: 120px;
background-color: yellow;
}
</style>
</head>
<body>
<div class="root">
<div class="nav"></div>
<div class="content">
<div class="box">我是一个盒子</div>
</div>
</div>
</body>
</html>
在剩下的空间加个父盒子,展示div放在这个盒子下,父盒子position: relative; 子盒子position: absolute;居中就行了
div嵌套,开启flex布局,即可