1

I need to keep a sentence not wrapping when the width of the div is smaller than this sentence

.ex1 {
  background-color: #F0F0F0;
  width: 25%;
  word-wrap: normal;
}
<div class="ex1">
This is my sentence
</div>

The probleme is that properties overflow-x:visible or word-wrap: normal not working when the sentence contains spaces

Temani Afif
  • 245,468
  • 26
  • 309
  • 415

2 Answers2

2

Use white-space to control how white spaces are handled:

.ex1 {
  background-color: #F0F0F0;
  width: 25%;
  white-space: nowrap;
}
<div class="ex1">
  This is my sentence
</div>
<div class="ex1">
  Line breaks are suppressed here
</div>
Maxime Launois
  • 928
  • 6
  • 19
2

I'm not entirely certain what you mean by not wrapping.

Either, the text could disappear at the end of the div:

.ex1 {
  background-color: #F0F0F0;
  width: 25%;
  overflow-x: hidden;
  white-space: nowrap;
}
<div class="ex1">
This is my sentence
</div>

This is achieved by setting the white-space to nowrap, forcing the text to not wrap, and then hiding any overflow on the x-axis (overflow-x: hidden).

Or alternatively, the div could expand to fit the text:

.ex1 {
  background-color: #F0F0F0;
  width: 25%;
  white-space: nowrap;
}
<div class="ex1">
This is my sentence
</div>

As above, using no-wrap, but allowing overflow by not hiding it.

Martin
  • 16,093
  • 1
  • 29
  • 48