CSS:
.navbar {
left: 0;
right: 0;
display: flex;
justify-content: space-between;
align-content: center;
height: 10vh;
background-color: blue;
}
HTML:
<nav class="navbar">
<div class="burger">
<div class="layer1"></div>
<div class="layer2"></div>
<div class="layer3"></div>
</div>
</nav>
我原以为这些物品之间有间隔,但它们没有。有什么建议吗?
我试过用.navbar .burger
作为选择器,但它的格式不太好。
您需要将flexbox设置应用于要影响的项目的直接父项,在您的情况下为.burger
:
.burger {
left: 0;
right: 0;
display: flex;
justify-content: space-between;
align-content: center;
height: 10vh;
background-color: blue;
color: white;
}
<nav class="navbar">
<div class="burger">
<div class="layer1">Entry 1</div>
<div class="layer2">Entry 2</div>
<div class="layer3">Entry 3</div>
</div>
</nav>
这就是您想要的:
.burger {
left: 0;
right: 0;
display: flex;
justify-content: space-between;
align-content: center;
height: 10vh;
background-color: blue;
color: #fff;
}
<nav class="navbar">
<div class="burger">
<div class="layer1">Text1</div>
<div class="layer2">Text2</div>
<div class="layer3">Text3</div>
</div>
</nav>
如果您的Flexbox和属性(justify-content: space-between
)切换为仅一个元素:div.burguer
,您希望在它们之间有什么空间?
我不认为你错过了我们的一些信息,只是忘记用</nav>
关闭你的<nav>
。
<!doctype html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<title>Document</title>
<style>
body {
width: 100vw;
height: 100vh;
margin: 0;
padding: 0;
}
.navbar {
left: 0;
right: 0;
display: flex;
justify-content: space-between;
align-content: center;
height: 10vh;
background-color: gray;
}
.navbar > div {
padding: 2px 10px;
color: white;
}
.navbar > div:nth-child(1) {
background: red;
}
.navbar > div:nth-child(2) {
background: blue;
}
.navbar > div:nth-child(3) {
background: yellow;
color: black !important;
}
</style>
</head>
<body>
<nav class="navbar">
<div class="layer1">Home</div>
<div class="layer2">Panel</div>
<div class="layer3">Contact</div>
</nav>
</body>
</html>