css - 多个 sibling ,将某人放在同一行并填充额外的空间

标签 css flexbox

我无法控制 html。我有一个有多个 child 的 parent 。只有其中一些必须在同一行,而其余的不受影响,其中一个必须占用所有额外的空间。内容是自动生成的,% 不是一个选项。

除了内联放置在同一行以避免问题之外的其他选项也是受欢迎的。

.parent {
  background: red;
}

.same-row-child {
  background: green;
  display: inline-flex;
}
<div class="parent">
  <div class="other-child">A</div>
  <div class="same-row-child">B</div>
  <div class="same-row-child">C</div>
</div>

总结一下:第一行的 Α 不受影响。 B+C 在同一行,B 占据了所有额外的空间。

最佳答案

如果想法是使用 flex,那么它应该是 flex box 的父级:

https://css-tricks.com/snippets/css/a-guide-to-flexbox/

display:flex; display:inline-flex; It enables a flex context for all its direct children.

.parent {
  background: red;
  display: flex;
  flex-wrap: wrap;
}

.other-child {
  width: 100%;
}

.same-row-child {
  background: green;
}

.parent :last-child {
  flex: 1;
  margin-left:2px;
}
<div class="parent">
  <div class="other-child">A</div>
  <div class="same-row-child">B</div>
  <div class="same-row-child">C</div>
</div>

看起来不是您会使用的选项 查看下一个选项


最古老的方法是floatoverflowfloat的是先行并且应该会自行收缩。

see https://css-tricks.com/all-about-floats/

Aside from the simple example of wrapping text around images, floats can be used to create entire web layouts.

.parent {
  background: red;
}

.other-child {}

.same-row-child {
  float: left;
  background: green;
  margin-right: 2px;
}

.parent :last-child {
  float: none;
  overflow: hidden;
  margin: 0;
}
<div class="parent">
  <div class="other-child">A</div>
  <div class="same-row-child">B</div>
  <div class="same-row-child">C</div>
</div>

关于css - 多个 sibling ,将某人放在同一行并填充额外的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56908361/

相关文章:

css - HTML5,当我在其上添加内容时背景视频不运行

css - 在 Metro IE 中禁用导航滑动

html - 具有固定元素的 Flexbox

html - 对齐复选框和标签

HTML、CSS - 防止按钮在单击时向上移动

html - 将 flex 容器内的元素向右移动

html - Flexbox - 最后一个元素放置在 "floating"右边

html - 为什么百分比填充/边距不适用于 Firefox 和 Edge 中的 flex 元素?

html - 为什么 flex 元素包裹而不是收缩?

php - 在wordpress中创建一个包含导航栏的div