Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a <span> in a middle of a sentence when using a flexbox?

Tags:

css

flexbox

Given a flex container that contains a sentence, I'd like to wrap one of the words with a <span> for some additional styling. Example:

div {
  display: flex;
  border: 1px solid black;
  width: 190px;
  flex-wrap: wrap;
}

span {
  color: red;
}
<div>
  This is <span>not</span> a very long sentence.
</div>

enter image description here

As you can see, the sentence breaks after the <span>.

The desired outcome is:

enter image description here

div {
  display: flex;
  border: 1px solid black;
  width: 190px;
  flex-wrap: wrap;
  white-space: pre;
}

.red {
  color: red;
}
<div>
  <span>This </span>
  <span>is </span>
  <span class="red">not </span>
  <span>a </span>
  <span>very </span>
  <span>long </span>
  <span>sentence.</span>
</div>

How would you solve this?

like image 968
Misha Moroshko Avatar asked Sep 12 '25 03:09

Misha Moroshko


1 Answers

Try with display: inline-block;

div {
  display: inline-block;
  border: 1px solid black;
  width: 190px;
  flex-wrap: wrap;
}

span {
  color: red;
}
<div>
  This is <span>not</span> a very long sentence.
</div>
like image 105
Nitin Dhomse Avatar answered Sep 13 '25 17:09

Nitin Dhomse