How can I separate the middle element into new lines if its content doesn't fit using just CSS?
P粉691461301
P粉691461301 2024-02-26 22:10:44
0
1
400

The following is more of a general example.

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

It is possible to change the element order or type beforehand or add helpers in between. All text is not wrapped. It is not necessary to give a fixed width or height.

How to achieve this effect using only CSS instead of changing the DOM? So, how do you separate the middle element into a new line if the content doesn't fit? is it possible?

Some snippets could be explained in more detail and tested.

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

The current answer is helpful and acceptable. However, are there (or will there be) other ways that don't utilize floats?

P粉691461301
P粉691461301

reply all(1)
P粉147045274

float is the only solution here, but you'll have to adjust the HTML structure (I know this goes against what you're asking for, but the following might give you some ideas)

.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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template