For example, in this sentence, "Comment allez-vous ?", the question mark and the last word in the sentence are separated by a whitespace.
When French text is written in a column, you will often get something like this:
Elle zigzague pour empiéter sur des impostures
? Jacqueline porte chance.
The line break happens between the last word of the sentence and the question mark, which is not desirable.
Elle zigzague pour empiéter sur des
impostures ? Jacqueline porte chance.
Is there a way to solve this in pure CSS? Or do we have to manually process text and wrap the punctuation and the word in a non-breaking span?
The display:inline-block property is used to show an element in the inline-level block container. It converts the block of elements into an inline element. Use height and width property to set an inline element. The display property is used to prevent a line break of a list of items.
If you want to prevent the text from wrapping, you can apply white-space: nowrap; Notice in HTML code example at the top of this article, there are actually two line breaks, one before the line of text and one after, which allow the text to be on its own line (in the code).
Remove white space using font-size We can also remove white space by setting parent element font-size to 0 and child elements font-size to 17px .
In CSS, you can use either the margin or padding properties to add space around elements. Additionally, the text-indent property adds space to the front of the text, such as for indenting paragraphs.
Use
in HTML or white-space: nowrap;
in CSS.
.sentence {
width: 314px;
border: 1px solid #eee;
}
.nowrap {
white-space: nowrap;
}
.sentence > span {
border-bottom: 1px solid darkred;
}
code {
background-color: #ddd;
padding: 0.2em;
}
<main>
<h3>Regular space</h3>
<p class="sentence">Elle zigzague pour empiéter sur des <span>impostures ?</span> Jacqueline porte chance.</p>
<h3>Avoid new line with non-breaking space HTML entity <code>&nbsp;</code></h3>
<p class="sentence">Elle zigzague pour empiéter sur des <span>impostures ?</span> Jacqueline porte chance.</p>
<h3>Avoid new line with CSS <code>white-space: nowrap</code></h3>
<p class="sentence">Elle zigzague pour empiéter sur des <span class="nowrap">impostures ?</span> Jacqueline porte chance.</p>
</main>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With