css 空白:使用flex的nowrap:1;子项宽度太小,无法容纳文本

9jyewag0  于 2023-06-25  发布在  其他
关注(0)|答案(3)|浏览(159)

我找不到任何解决方案,有2个相同大小的divs,将适合它内的文本在一行。看起来像空白:设置父宽度后,nowrap宽度不考虑:fit-content。
它们应该尽可能小,总是相同的大小,没有换行文本到第二行。

div {
  display: flex;
  width: fit-content;
}

.button {
  padding: 10px 20px;
  flex: 1;
  white-space: nowrap;
  min-width: 130px;
  width: fit-content;
}

.button1 {
  background: red;
}

.button2 {
  background: green;
}
<div>
  <div class="button button1">Short Text</div>
  <div class="button button2">A Long Text Button That Has Greater Width </div>
</div>
3zwtqj6y

3zwtqj6y1#

可以使用CSS Grid:

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

.button {
  padding: 10px 20px;
  white-space: nowrap;
  min-width: 130px;
  overflow: auto;
}

.button1 {
  background: red;
}

.button2 {
  background: green;
}
<div class="container">
  <div class="button button1">Short Text</div>
  <div class="button button2">A Long Text Button That Has Greater Width </div>
</div>
ezykj2lf

ezykj2lf2#

解决方案1(带flex)

  • 元素的宽度等于最短元素的宽度

使用flex-basis
flex-basis属性确定Flex项的初始大小,然后系统在布局过程中分配剩余的空间。
More information-堆栈溢出应答

.flex {
  display: flex;
  width: fit-content;
}

.button {
  flex-basis: 100%; /* HERE (in child element) */
  padding: 10px 20px; 
  white-space: nowrap;
  min-width: 130px;
  width: 100%;
  overflow: auto;
}

.button1 {
  background: red;
}

.button2 {
  background: green;
}
<h2>Solution (with 2 elements)</h2>
<div class="flex">
  <div class="button button1">Short Text</div>
  <div class="button button2">A Long Text Button That Has Greater Width </div>
</div>

<h2>Solution (with more elements)</h2>
<div class="flex">
  <div class="button button1"># 1 Short Text</div>
  <div class="button button2"># 2 A Long Text Button That Has Greater Width </div>
  <div class="button button1"># 3 Short Text</div>
  <div class="button button2"># 4 A Long Text Button That Has Greater Width </div>
  <div class="button button1"># 5 Short Text</div>
  <div class="button button1"># 6 Short Text</div>
  <div class="button button1"># 7 Short Text</div>
</div>

解决方案2(使用flex和JavaScript)

  • 元素的宽度等于最长元素的宽度

使用JavaScript将子元素的宽度设置为最长元素的宽度。

// Get all container
const flexContainers = document.querySelectorAll('.flex')

// Run function to all container
flexContainers.forEach((flexContainer) => {
  // Get all children from current container
  const children = flexContainer.querySelectorAll('.button')
  // Find longest child
  let longestChild = children[0]
  children.forEach((child) => {
    if (child.scrollWidth > longestChild.scrollWidth) {
      longestChild = child
    }
  })
  // Set width of children
  children.forEach((child) => {
    child.style.width = `${longestChild.scrollWidth}px`
  })
})
.flex {
  display: flex;
  width: fit-content;
}

.button {
  flex-basis: 100%; /* HERE (in child element) */
  padding: 10px 20px; 
  white-space: nowrap;
  min-width: 130px;
  width: 100%;
}

.button1 {
  background: red;
}

.button2 {
  background: green;
}
<h2>Solution (with 2 elements)</h2>
<div class="flex">
  <div class="button button1">Short Text</div>
  <div class="button button2">A Long Text Button That Has Greater Width </div>
</div>

<h2>Solution (with more elements)</h2>
<div class="flex">
  <div class="button button1"># 1 Short Text</div>
  <div class="button button2"># 2 A Long Text Button That Has Greater Width </div>
  <div class="button button1"># 3 Short Text</div>
  <div class="button button2"># 4 A Long Text Button That Has Greater Width </div>
  <div class="button button1"># 5 Short Text</div>
  <div class="button button1"># 6 Short Text</div>
  <div class="button button1"># 7 Short Text</div>
</div>
2guxujil

2guxujil3#

div {
  display: flex;
  width: fit-content;
}

.button {
  padding: 10px 20px;
  flex: 1;
  white-space: nowrap;
  min-width: 130px;
  width: fit-content;
}

.button1 {
  background: red;
}

.button2 {
  background: green;
  width: 500px;
}
<div>
  <div class="button button1">Short Text</div>
  <div class="button button2">A Long Text Button That Has Greater Width </div>
</div>

相关问题