以下内容更像是一个通用的示例.

<div>
  <div>Short dynamic text.</div>
  <div id="in-between">Possible long dynamic text...</div>
  <div>Short dynamic text.</div>
</div>

更改元素类型或在其间添加辅助对象是可能的.并非所有文本都不换行.不能给出固定的宽度或高度.

如何仅使用CSS而不通过更改DOM来实现这样的功能?那么,如果元素的内容不适合,如何将元素之间的一些内容分隔到新行中呢?这完全有可能吗?

enter image description here

更详细地说明它并进行测试的一些代码片段.

.example {
  display: flex;
  flex-wrap: wrap;
  column-gap: 0.5rem;
  justify-content: space-between;
}

.example .separate {
  flex-grow: 1;
}

.example .long-b {
  order: 1; /* this should happen by some magic */
}

/* do not change below */

.container {
  border: 0.5rem solid black;
  background: blue;
}

.element {
 border: 0.5rem solid yellow;
 padding: 0.5rem;
 color: white;
 white-space: nowrap;
 overflow: hidden;
 text-overflow: ellipsis;
}

.short-a:before {
  content: "only three words";
}

.short-b:before {
 content: "flag";
}

.short-c:before {
  content: "i love stackoverflow";
}

.short-d:before {
 content: "attribute";
}

.long-a:before {
  content: "i am not that long";
}

.long-b:before {
  content: "i am very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long";
}
<div class="container example">
  <div class="element short-a"></div>
  <div class="element separate long-a"></div>
  <div class="element short-b"></div>
</div>
<br />
<div class="container example">
  <div class="element short-c"></div>
  <div class="element separate long-b"></div>
  <div class="element short-d"></div>
</div>

推荐答案

浮动是唯一的解决方案,但您必须调整HTML struct (我知道这不符合您的要求,但下面的内容可能会给您一些启发)

.box {
  border:1px solid red;
  padding: 3px;
  /* to debug */
  overflow: hidden;
  resize: horizontal;
  /**/
}
.box > div {
  border: 1px solid #000;
  box-sizing: border-box;
}

.box > :first-child {
  float: left;
}
.box > :nth-child(2) {
  float: right;
}

#in-between {
  display: inline-block;
  
  /* remove the below if you want text wrap instead of ellpsis*/
  max-width: 100%;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
<div class="box">
  <div>Short dynamic text.</div>
  <div>Short dynamic text.</div>
  <div id="in-between">Possible long dynamic text Possible long text</div>
</div>

Css相关问答推荐

响应的Megamenu位置css

Next.js中的TailWind CSS类名称疑难解答

如何将React应用程序主页的内容居中?

如何使用 tailwind css 修复滚动条始终位于底部?

如何为图像堆叠效果下方的图层实现与磨砂玻璃类似的视觉效果

Tailwind CSS 不输出背景类

如何减少弹性项目之间的差距

在变量中存储匹配 Select 器的子元素的计数

如何使代码块输出在 quarto revealjs 演示文稿中水平滚动

如何在相对位置和绝对位置之间转换元素?

在 Tailwind css 中,间距对我不起作用

CSS 字符串变量的正确null值是多少?

在表格中隐藏绝对伪元素

CSS中的旋转地球

表格的边框半径没有按预期工作

Angular 5 在某些类中添加了ng-star-inserted - 那是什么?

CSS:在图像周围创建白光

CSS 块格式化上下文是如何工作的?

如何应用 CSS 分页符来打印包含大量行的表格?

CSS垂直对齐不适用于浮动