以下更像是一个一般示例。
<div> <div>Short dynamic text.</div> <div id="in-between">Possible long dynamic text...</div> <div>Short dynamic text.</div> </div>
可以事先更改元素顺序或类型或在其间添加帮助程序。所有文本均不换行。不必给出固定的宽度或高度。
如何仅使用 CSS 而不是通过更改 DOM 来实现这样的效果?那么,如果内容不适合,如何将中间元素分离到新行中呢?这可能吗?
一些片段可以更详细地说明并进行测试。
.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>
当前的答案很有帮助并且可以接受。然而,是否有(或将会有)其他不利用浮动的方法?
P粉1470452742024-02-27 10:47:52
float 是这里唯一的解决方案,但是你必须调整 HTML 结构(我知道这违背了你的要求,但下面的内容可能会给你一些想法)
.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; }
Short dynamic text.Short dynamic text.Possible long dynamic text Possible long text