【发布时间】:2017-03-08 06:19:39
【问题描述】:
场景如下:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
border: 1px solid black;
}
.flex-item {
background-color: cornflowerblue;
height: 1.7rem;
padding: 0 1.2rem;
flex: 1 1 33.33333%;
white-space: nowrap;
max-width: 33.33333%;
}
</style>
</head>
<body>
<div class="flex-container">
<div class="flex-container">
<button class="flex-item">Foo</button>
<button class="flex-item">Bar</button>
<button class="flex-item">Long ass text here</button>
</div>
</div>
</body>
</html>
按钮大小相同,但问题是它们的大小不会根据内容最长的弹性项目进行缩放。相反,此处最后一个按钮的文本会溢出。看起来是这样的:
这就是我想要的样子:
简而言之,我想要3个宽度和高度相同的弹性按钮,宽度应根据内容最长的按钮确定。另外,我宁愿只用 CSS 来做这个(如果可能的话)。
编辑: 因为似乎存在一些误解,所以我想澄清一下宽度应该动态地改变,因此适用于为按钮提供的任何文本,而不是只是上面显示的那些。换句话说,您不能只添加例如width: 10rem 直接用于 flex-item,因为它只在特定情况下有效。
【问题讨论】:
-
如果它是大约 3 个元素,那么 display:grid 可能会在第二级输入:codepen.io/gc-nomade/pen/EWNoLa 仍然是实验性的,您需要在 Firefox 和 chrome 中设置实验性 CSS 标志才能对其进行测试.它还不能用于常规网站 :( css-tricks.com/snippets/css/complete-guide-grid
. .:-: flex 或 table 显示失败,没有在父级或子级上指定宽度