我有两行:第一行有5个按钮,第二行有一个div,应该与按钮的长度相同。
这是我的代码:
BODY * {
box-sizing: border-box;
}
.container {
background: blue;
width: 100%;
height: 66px;
position: relative;
}
.logo {
background: red;
width: 65px;
height: 65px;
}
.rightcontainer {
position: absolute;
right: 0;
top: 0;
background-color: green;
}
.buttons > DIV {
display: inline-block;
padding: 5px;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
.buttons > DIV:first-child {
max-width: 30%;
}
.search {
width: 100%;
background-color: yellow;
}
OKAY:
<div class="container">
<div class="logo"></div>
<div class="rightcontainer">
<div class="buttons">
<div>Buttons 1</div>
<div>Buttons 2</div>
<div>Buttons 3</div>
<div>Buttons 4</div>
</div>
<div class="search">
Search
</div>
</div>
</div>
Not okay: The gap between button 4 and border should be gone
<div class="container">
<div class="logo"></div>
<div class="rightcontainer">
<div class="buttons">
<div>Buttons 1 long long long long</div>
<div>Buttons 2</div>
<div>Buttons 3</div>
<div>Buttons 4</div>
</div>
<div class="search">
Search
</div>
</div>
</div>
max-width
。如果按钮变大(
max-width
被使用),第二行的宽度不会被缩小。此样式是问题所在(如果使用像素值,一切都正常)。
.buttons > DIV:first-child
{
max-width:30%;
}
您有什么想法可以实现这个功能吗?flexbox会有帮助吗?
提前致谢。
min-width: max-content
。 - Collin