【问题标题】:How to set flex items to equal width according to the item with the longest content?如何根据内容最长的项目将弹性项目设置为等宽?
【发布时间】: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,因为它只在特定情况下有效。

【问题讨论】:

标签: html css flexbox


【解决方案1】:

flex 在这种情况下失败了:

Display:grid 将来可以在这里做。

您的问题提到了 3 个要素:

教程https://css-tricks.com/snippets/css/complete-guide-grid/

浏览器支持http://caniuse.com/#search=grid

一个 Polyfill https://github.com/FremyCompany/css-grid-polyfill/

http://gridbyexample.com/browsers/

.flex-container {
  display: flex;
  border: 1px solid black;
  margin: 1em;
}

.flex-container .flex-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
}

.flex-item {
  background-color: cornflowerblue;
  height: 1.7rem;
  padding: 0 1.2rem;
  width: 100%;
  white-space: nowrap;
}

@supports (display: grid) {
  .discl {
    display: none
  }
}
<p class="discl"> grid CSS seems not supported by your browser</p>
<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>
<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 text & Long text here</button>
  </div>
</div>

此外,从已知数量的元素来看,column CSS 可能在这里有用

.flex-container {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  border: 1px solid black;
  margin:1em;
}

.flex-container .flex-container {
  display:block;
  -moz-column-count:3;
  -moz-column-gap:0;
  -webkit-column-count:3;
          column-count:3;
  -webkit-column-gap:0;
          column-gap:0;
}

.flex-item {
  background-color: cornflowerblue;
  height: 1.7rem;
  padding: 0 1.2rem;
  width: 100%;
  white-space: nowrap;
}
<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>
  <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 Long ass text here</button>
    </div>
  </div>

在这两种情况下,display flex 仅用于将第二个容器缩小到其内容。在块父级中,float 或 display:inline-block 在该级别也可以。

【讨论】:

  • 感谢您的回答。看来您不能使用 flex 来实现这一点,我决定只为容器指定一个宽度。如果网格可用,那就太好了。我宁愿不使用该列,因为它使按钮太宽。但是,我会接受这个作为答案,因为它涵盖了我目前的选择。
  • 太棒了! CSS 网格摇滚。自 2017 年 8 月起,所有现代浏览器都支持 CSS 网格。
  • 有没有办法只使用 css 来处理任意/动态数量的元素?
  • 我也在寻找一种方法来处理任意数量的项目/列。动态组件需要它。
【解决方案2】:

请试试这个,让我知道它是否有效。

.flex-container {
    display:flex;
    align-items: stretch;
    border: 1px solid black;
}

.flex-item {
    background-color: cornflowerblue;
    height: 1.7rem;
    width: 10rem;
    padding: 0 1.2rem;
    flex: 1 1 33.33333%;
    white-space: nowrap;
    max-width: 50%;
    flex-grow: 1;
    flex-basis: 0;
}

<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>

【讨论】:

  • 抱歉,它需要与 2 个弹性容器一起使用。此外,正如您在图片中看到的那样,内部 flex-container 不应占用外部 flex-container 的整个宽度,而应仅占需要的宽度。这就是为什么我在那里画了边界来证明这一点。此外,您删除了空格: nowrap 需要在那里,否则长文本会进入多行,我希望它始终在单行上。
  • @JZ555 你检查编辑答案了吗?因为这符合您的需求。
  • @GCyrillys 这和 JCD 的解决方案有同样的问题。您直接设置宽度,因此,如果我使最后一个按钮文本更长,它会停止工作。按钮的宽度应该是动态变化的,所以不管文本有多长,它总是有效的。
  • 抱歉,如果问题表述不当。还对原始帖子进行了说明。也请检查一下。
【解决方案3】:

我把显示器放在实心上。 只需给出一个固定宽度,它现在应该看起来像你想要的那样。

<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
    display: solid;
    border: 1px solid black;
}

.flex-item {
    background-color: cornflowerblue;
    height: 1.7rem;
    width: 10rem;
    padding: 0 1.2rem;
    flex: 1 1 33.33333%;
    white-space: nowrap;
    max-width: 50%;
}
</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>

【讨论】:

  • 这只允许您按照您的要求更改按钮的大小
  • 这里的问题是你把宽度设置为10rem。它需要是动态的。就像我说的:“宽度应该根据内容最长的按钮来确定”。换句话说,如果我使最后一个按钮中的文本更长,您的解决方案将不再有效。此外, display:solid 让我们在按钮之间产生了我不想要的奇怪间隙。
猜你喜欢
  • 2017-03-14
  • 2018-05-22
  • 2016-07-04
  • 1970-01-01
  • 2018-07-03
  • 2020-03-03
  • 1970-01-01
  • 2021-08-28
  • 2017-11-14
相关资源
最近更新 更多