1 回答
TA贡献2011条经验 获得超2个赞
通过 javascript ,您可以循环遍历 .item 并查找最宽的,然后更新自定义 css 属性。
通过 js 和 flex 的可能示例
var bigW = "0";
for (let e of document.querySelectorAll("nav .item")) {
elW = e.offsetWidth;
if (elW > bigW) {
bigW = elW;
}
document.documentElement.style.setProperty("--myW", bigW + 1 + "px");
}
nav {
display: flex;
background: #e8e8e8;
width: 100%;
}
.item {
min-width: var(--myW, 3em);
text-align: center;
}
.separator {
flex: 1;
background: #d3d3d3;
}
nav div+div {
border-left: solid;
}
<nav>
<div class="item">A</div>
<div class="item">BBB</div>
<div class="item">CC</div>
<div class="separator"></div>
<div class="item">D</div>
</nav>
从下面的评论编辑
var bigW = "0";
for (let e of document.querySelectorAll("nav > div")) {
elW = e.offsetWidth;
if (elW < 7) {// includes partially possible border and padding, mind it
e.style.flexGrow = 1;
} else if (elW > bigW) {
bigW = elW;
}
}
document.documentElement.style.setProperty("--myW", bigW + 1 + "px");
nav {
display: flex;
background: #e8e8e8;
}
.item {
min-width: var(--myW, 0);
text-align: center;
border: solid 1px;
}
.separator {
background: #d3d3d3;
}
<nav>
<div class="item">A</div>
<div class="item">BBBBBBBB</div>
<!--<div class="separator"></div>-->
<div class="item">CC</div>
<div class="separator"></div>
<div class="item">D</div>
</nav>
- 1 回答
- 0 关注
- 91 浏览
添加回答
举报