我正在为我的网站构建导航。我使用flexbox这个。我想在.nav-item
的底部显示一个酒吧,当它徘徊时。跨度应该在nav
中垂直居中,.bar
应始终位于nav
和.nav-item.
的底部以获得此布局在文本上方有另一个.bar
,其中包含background-color: transparent;
。比我使用flexbox和justify-content: space-between;
来获取我的布局(悬停时跨度中心垂直和.bar
)。目前这工作正常。我想改变一些观点,但我不知道如何:
.nav-item
上时,.bar
应该改变它的高度,而不会弹跳nav
(查看代码段)。 nav
应该保留,只有.bar
应该上下。padding
,但是当我这样做时,布局是错误的(栏不在导航栏的底部,它很清楚为什么)。如何解决这个问题(在导航上填充并仍然得到正确的布局)?
html,
body {
margin: 0;
}
nav {
display: flex;
align-items: center;
/*padding: 20px;*/
border-bottom: 1px solid lightgrey;
}
.nav-item {
display: flex;
flex-direction: column;
justify-content: space-between;
margin-right: 20px;
height: 100%;
transition: all 1s ease-in-out;
cursor: pointer;
/*padding: 20px;*/
}
.bar {
width: 100%;
height: 0px;
background-color: blue;
transition: height 200ms ease-in-out;
}
.hidden {
background-color: transparent;
}
.nav-item:hover>.bar {
height: 3px;
}

<nav>
<div class="nav-item">
<div class="bar hidden"></div>
<span>Item 1</span>
<div class="bar"></div>
</div>
<div class="nav-item">
<div class="bar hidden"></div>
<span>Item 2</span>
<div class="bar"></div>
</div>
<div class="nav-item">
<div class="bar hidden"></div>
<span>Item 3</span>
<div class="bar"></div>
</div>
</nav>
&#13;
答案 0 :(得分:1)
这是因为新的height
不断扩大。要以你的方式制作,你需要使用position
并从布局中取出栏:
nav {
display: flex;
align-items: center;
/*padding: 20px;*/
border-bottom: 1px solid lightgrey;
min-height: 27px; /* This one */
}
<强>段强>
html,
body {
margin: 0;
}
nav {
display: flex;
align-items: center;
/*padding: 20px;*/
border-bottom: 1px solid lightgrey;
}
.nav-item {
display: flex;
flex-direction: column;
justify-content: space-between;
margin-right: 20px;
height: 100%;
transition: all 1s ease-in-out;
cursor: pointer;
/*padding: 20px;*/
position: relative;
}
.bar {
left: 0;
right: 0;
bottom: 0;
height: 0px;
background-color: blue;
transition: height 200ms ease-in-out;
position: absolute;
}
.hidden {
background-color: transparent;
}
.nav-item:hover>.bar {
height: 3px;
}
&#13;
<nav>
<div class="nav-item">
<div class="bar hidden"></div>
<span>Item 1</span>
<div class="bar"></div>
</div>
<div class="nav-item">
<div class="bar hidden"></div>
<span>Item 2</span>
<div class="bar"></div>
</div>
<div class="nav-item">
<div class="bar hidden"></div>
<span>Item 3</span>
<div class="bar"></div>
</div>
</nav>
&#13;
答案 1 :(得分:1)
试试这个CSS
html,
body {
margin: 0;
}
nav {
display: flex;
align-items: center;
/*padding: 20px;*/
border-bottom: 1px solid lightgrey;
}
.nav-item {
display: flex;
flex-direction: column;
justify-content: space-between;
margin-right: 20px;
height: 100%;
transition: all 1s ease-in-out;
cursor: pointer;
position: relative;
padding:10px;
/*padding: 20px;*/
}
.bar {
width: 100%;
height: 0px;
position: absolute;
background-color: blue;
bottom:0px;
left: 0;
right: 0;
margin:auto;
transition: height 200ms ease-in-out;
}
.hidden {
background-color: transparent;
}
.nav-item:hover>.bar {
height: 3px;
}
希望这会有所帮助..