How to add line break using only CSS?

Spread the love

We can add line breaks using CSS by utilizing the ::after or ::before pseudo-elements along with the content property. Here’s an example of how to add a line break using CSS:

For instance we write

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Line Break using CSS</title>
    /* Define a class for adding line break */
    .line-break::after {
        content: "\A"; /* Use "\A" to represent a line break */
        white-space: pre; /* Preserve white space */

<!-- Apply the line-break class to add a line break -->
<div class="line-break">This is a line<br>with a line break added using CSS.</div>


We define a CSS class .line-break::after and use the content property to insert a line break represented by \A. This special character represents a line break in CSS content.

Also we use white-space: pre; to preserve white space and ensure the line break behaves as expected.

Then we apply the .line-break class to an element, it will insert a line break after the content of that element.

Please note that this method is mainly used for adding line breaks in specific cases where you cannot modify the HTML structure directly, and it may not be suitable for all situations. In most cases, it’s better to use HTML markup like <br> tags to indicate line breaks.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *