How To Prevent Line Breaks Using CSS | DigitalOcean (2024)

Introduction

Developers typically like to wrap text on a web page. Wrapping constrains text in one way or another and prevents design issues. Text wrapping can also prevent horizontal scrolling. But there are times when you want blocks of text to stay on the same line, regardless of length. You can prevent line breaks and text wrapping for specific elements using the CSS white-space property.

In this tutorial, you will style the same block of text four different ways, first with line breaks and then three times without line breaks:

Medusafish banded killifish convict blenny saury threadsail beluga sturgeon. Indian mul mora cisco masu salmon, roosterfish requiem shark longnose lancetfish bluefish red snapper Sacramento splittail giant danio.

Medusafish banded killifish convict blenny saury threadsail beluga sturgeon. Indian mul mora cisco masu salmon, roosterfish requiem shark longnose lancetfish bluefish red snapper Sacramento splittail giant danio.

Medusafish banded killifish convict blenny saury threadsail beluga sturgeon. Indian mul mora cisco masu salmon, roosterfish requiem shark longnose lancetfish bluefish red snapper Sacramento splittail giant danio.

Medusafishbandedkillifishconvictblennysaurythreadsailbelugasturgeon.Indianmulmoraciscomasusalmon,roosterfishrequiemsharklongnoselancetfishbluefishredsnapperSacramentosplittailgiantdanio.

This will provide you several options for wrapping or not wrapping your text.

Prerequisites

To complete this tutorial, you will need:

  • A code editor of your choice, such as nano or Visual Studio Code
  • A web browser
  • A comfort with HTML fundamentals. You can view our tutorial series How To Build a Website with HTML for an introduction.

Step 1 — Preventing and Forcing Line Breaks in CSS

In this step, you will create a style sheet with three different classes. Each one will handle line breaks differently: the first will break text in the default manner while the second and third will force the text not to create a newline and break.

First, create and open a new file called main.css using nano or your preferred editor:

  1. nano main.css

Add the following content, which will introduce three CSS classes that use several properties, including white-space:

./main.css

.sammy-wrap { border-radius: 6px; background-color: aliceblue; border: 2px dashed gray; max-width: 70%; padding: 1em; margin-bottom: .4em;}.sammy-nowrap-1 { border-radius: 6px; background-color: aliceblue; border: 2px dashed gray; max-width: 70%; padding: 1em; margin-bottom: .4em; white-space: nowrap;}.sammy-nowrap-2 { border-radius: 6px; background-color: aliceblue; border: 2px dashed gray; max-width: 70%; padding: 1em; margin-bottom: .4em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;}

Your first class is .sammy-wrap. It defines six common CSS properties including border-radius, background-color, border max-width, padding, and margin-bottom. This class will create a visual box, but it does not define any special wrapping properties. This means it will break lines in the default manner.

Your second class is .sammy-nowrap-1. It defines the same box as .sammy-wrap but now you add another property: white-space. The white-space property has numerous options, all of which define how to treat white space inside a given element. Here, you have set white-space to nowrap, which will prevent all line breaks.

Your third class is .sammy-nowrap-2. It adds white-space and two additional properties: overflow and text-overflow. The overflow property handles scrollable overflow, which occurs when the contents inside an element extend beyond that element’s edges. The overflow property can make that content scrollable, visible, or hidden. You are setting overflow to hidden and then using the text-overflow property to add even more customization. text-overflow can help you signal to a user that additional text remains hidden. You have set this to ellipsis, so now your line will neither break nor extend beyond the box. CSS will hide the overflow and signal the hidden content with a ....

Save and close your file.

Now that you have a stylesheet, you are ready to make a short HTML file with some sample text. Then you will load the webpage in a browser and inspect how CSS can prevent line breaks.

Step 2 — Creating the HTML File

With your CSS classes defined, you can apply them to some sample text.

Create and open a file called index.html in your preferred editor. Make sure to place it in the same folder as main.css:

  1. nano index.html

Add the following content, which will associate main.css as your stylesheet and then apply your classes to a sample text block:

./index.html

<!DOCTYPE HTML><html><head><meta charset="UTF-8"><title>How To Prevent Line Breaks with CSS</title><link href="main.css" rel="stylesheet"></head><body><p class="sammy-wrap" > Medusafish banded killifish convict blenny saury threadsail beluga sturgeon. Indian mul mora cisco masu salmon, roosterfish requiem shark longnose lancetfish bluefish red snapper Sacramento splittail giant danio.</p><p class="sammy-nowrap-1"> Medusafish banded killifish convict blenny saury threadsail beluga sturgeon. Indian mul mora cisco masu salmon, roosterfish requiem shark longnose lancetfish bluefish red snapper Sacramento splittail giant danio.</p><p class="sammy-nowrap-2"> Medusafish banded killifish convict blenny saury threadsail beluga sturgeon. Indian mul mora cisco masu salmon, roosterfish requiem shark longnose lancetfish bluefish red snapper Sacramento splittail giant danio.</p><p class="sammy-wrap" > Medusafish&nbsp;banded&nbsp;killifish&nbsp;convict&nbsp;blenny&nbsp;saury&nbsp;threadsail&nbsp;beluga&nbsp;sturgeon.&nbsp;Indian&nbsp;mul&nbsp;mora&nbsp;cisco&nbsp;masu&nbsp;salmon,&nbsp;roosterfish&nbsp;requiem&nbsp;shark&nbsp;longnose&nbsp;lancetfish&nbsp;bluefish&nbsp;red&nbsp;snapper&nbsp;Sacramento&nbsp;splittail&nbsp;giant&nbsp;danio.</p></body></html>

You have assigned your standard wrapping style to the first text block, your nowrap style to the second, and nowrap that is hidden with ellipsis to the third. You have assigned sammy-wrap to the fourth sample, but you are overriding the default wrapping by inserting non-breaking spaces (&nbsp;) directly into the HTML. If you need to prevent line-breaks as a one-off situation, then non-breaking spaces can provide a quick solution.

Open index.html in a web browser and view your results. Your four text blocks will appear like this:

Medusafish banded killifish convict blenny saury threadsail beluga sturgeon. Indian mul mora cisco masu salmon, roosterfish requiem shark longnose lancetfish bluefish red snapper Sacramento splittail giant danio.

Medusafish banded killifish convict blenny saury threadsail beluga sturgeon. Indian mul mora cisco masu salmon, roosterfish requiem shark longnose lancetfish bluefish red snapper Sacramento splittail giant danio.

Medusafish banded killifish convict blenny saury threadsail beluga sturgeon. Indian mul mora cisco masu salmon, roosterfish requiem shark longnose lancetfish bluefish red snapper Sacramento splittail giant danio.

Medusafishbandedkillifishconvictblennysaurythreadsailbelugasturgeon.Indianmulmoraciscomasusalmon,roosterfishrequiemsharklongnoselancetfishbluefishredsnapperSacramentosplittailgiantdanio.

You have successfully customized your CSS properties to prevent or allow line breaks in four different fashions.

Conclusion

In this tutorial, you used CSS to prevent line breaks on a block of text. You styled the text inside a box and then added the white-space property to override the default text wrapping. To learn more about handling text wrapping and white space, consider exploring the entire white-space CSS property.

How To Prevent Line Breaks Using CSS | DigitalOcean (2024)
Top Articles
Economy in Norway compared to the EU
Amazon Gains A Big Advantage Over Walmart (NASDAQ:AMZN)
Uihc Family Medicine
Koordinaten w43/b14 mit Umrechner in alle Koordinatensysteme
Botanist Workbench Rs3
Walgreens Alma School And Dynamite
Flights to Miami (MIA)
Barstool Sports Gif
Jasmine
The Haunted Drury Hotels of San Antonio’s Riverwalk
OnTrigger Enter, Exit ...
Bubbles Hair Salon Woodbridge Va
Nichole Monskey
Pro Groom Prices – The Pet Centre
Cooktopcove Com
Los Angeles Craigs List
Nitti Sanitation Holiday Schedule
Best Suv In 2010
Missed Connections Dayton Ohio
Lcwc 911 Live Incident List Live Status
bode - Bode frequency response of dynamic system
Il Speedtest Rcn Net
Kohls Lufkin Tx
Used Patio Furniture - Craigslist
Buhl Park Summer Concert Series 2023 Schedule
Calvin Coolidge: Life in Brief | Miller Center
Nikki Catsouras: The Tragic Story Behind The Face And Body Images
Stubhub Elton John Dodger Stadium
Evil Dead Rise - Everything You Need To Know
O'reilly's Wrens Georgia
Utexas Baseball Schedule 2023
Kattis-Solutions
Dreamcargiveaways
Puretalkusa.com/Amac
Newcardapply Com 21961
Craigslist In Myrtle Beach
Ni Hao Kai Lan Rule 34
Selfservice Bright Lending
R&J Travel And Tours Calendar
2008 Chevrolet Corvette for sale - Houston, TX - craigslist
Skip The Games Grand Rapids Mi
All Characters in Omega Strikers
Woody Folsom Overflow Inventory
Kate Spade Outlet Altoona
8 4 Study Guide And Intervention Trigonometry
Dlnet Deltanet
The 5 Types of Intimacy Every Healthy Relationship Needs | All Points North
Unit 4 + 2 - Concrete and Clay: The Complete Recordings 1964-1969 - Album Review
Lsreg Att
Craigslist Charlestown Indiana
Haunted Mansion Showtimes Near The Grand 14 - Ambassador
Latest Posts
Article information

Author: Lilliana Bartoletti

Last Updated:

Views: 5880

Rating: 4.2 / 5 (73 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Lilliana Bartoletti

Birthday: 1999-11-18

Address: 58866 Tricia Spurs, North Melvinberg, HI 91346-3774

Phone: +50616620367928

Job: Real-Estate Liaison

Hobby: Graffiti, Astronomy, Handball, Magic, Origami, Fashion, Foreign language learning

Introduction: My name is Lilliana Bartoletti, I am a adventurous, pleasant, shiny, beautiful, handsome, zealous, tasty person who loves writing and wants to share my knowledge and understanding with you.