Table of Contents
Example 1 (long word decomposition div)
Long words breaking the div in HTML5
Use the Word-break CSS property to break words
grammar
Example 2 (Prevent long words from damaging divs)
Use Overflow-wrap attribute
Example 3
Example 4 (Using JavaScript to set the Overflow-wrap attribute)
Home Web Front-end CSS Tutorial How can I prevent long words from breaking my div?

How can I prevent long words from breaking my div?

Aug 30, 2023 pm 04:29 PM

How can I prevent long words from breaking my div?

Sometimes, developers need to display long words on a web page. For example, display URLs, long file names, etc. Sometimes the word length is greater than the length of the parent container and the word destroys the container.

For example, we created a card to show file details and the file name is very long, which may break the card, which always looks worse. Therefore, developers need to prevent long words from breaking div elements by wrapping them.

Before starting the solution, let us understand the problem through an example.

Example 1 (long word decomposition div)

In the example below, we create a div element and add a "p" element inside the div element. Additionally, we added long words to the text of the "p" element.

In CSS, we set a fixed size of the div element. In the output, the user can observe when the word breaks the div element and overflows from it.

<html>
<head>
   <style>
      .container {
         width: 300px;
         border: 1px solid #ccc;
         padding: 10px;
         font-size: 1.5rem;
      }       
   </style>    
</head>
<body>
   <h2 id="Long-words-breaking-the-div-in-HTML"> Long words breaking the div in HTML5 </h2>
   <div class = "container">
      <p class = "long-word"> This is a longwordthatshouldnotbreakinsideadiv. </p>
   </div>
</body>
</html>
Copy after login

Use the Word-break CSS property to break words

In this approach, we will use the "word-break" CSS property to prevent words from breaking the div element. The "word-break" attribute allows us to decide how words should be broken when they exceed the width of the container.

Different values ​​are needed to break the word. The "normal" value breaks words only at specified breakpoints (e.g. spaces, hyphens, etc.). The "break-all" value breaks the word at any character that overflows, and the "keep-all" value never breaks the word. word.

Here we will use the "break-all" value to break the word from any character.

grammar

Users can use the "word-break" CSS property according to the following syntax to prevent long words from breaking div elements.

 word-break: break-all;  
Copy after login

Example 2 (Prevent long words from damaging divs)

In the example below, we have added the long word we added in the first example inside the container div element. In CSS, we use the "word-break" property and the "break-all" value to prevent words from breaking div elements.

In the output, we can observe that the word breaks from a specific character and the remaining characters of the word are displayed in the next line.

<html>
<head>
   <style>
      .container {
         width: 300px;
         border: 1px solid #ccc;
         padding: 10px;
         font-size: 1.5rem;
      }
      .long-word {
         word-break: break-all;
      }
   </style>
</head>
<body>
   <h2> Preventing the long words breaking the div in HTML5
   </h2>
   <div class = "container">
      <p class = "long-word"> This is a longwordthatshouldnotbreakinsideadiv.</p>
   </div>
</body>
</html>
Copy after login

Use Overflow-wrap attribute

The "overflow-wrap" attribute allows us to determine how the element content should wrap when it overflows from the parent element. We can use the "break-word" value of the "overflow-wrap" attribute to prevent long words from breaking the div element by wrapping.

grammar

Users can use the "overflow-wrap" CSS property to wrap long words according to the following syntax.

overflow-wrap: break-word;
Copy after login

Example 3

In the example below, we have added a very long word as the text of the "p" element. After that, we use the "overflow-wrap" attribute of the parent element to wrap the overflowed content in the next line by breaking the words.

In the output, we can see that the word is broken in the middle and the remaining characters are displayed on the next line.

<html>
<head>
   <style>
      .container {
         width: 300px;
         border: 1px solid #ccc;
         padding: 10px;
         overflow-wrap: break-word;
      }
   </style>
</head>
<body>
   <h3> Preventing the long words breaking the div in HTML5 using the overflow-wrap property
   </h3>
   <div class = "container">
      <p class = "long-word"> Thisisaverylongwordthatshouldnotbreakinsideadiv. </p>
   </div>
</body>
</html>
Copy after login

Example 4 (Using JavaScript to set the Overflow-wrap attribute)

Sometimes, we need to use JavaScript to prevent long words from breaking the div. For example, we get the product data from the database, if the product name is very long, we can use the "overflow-wrap" attribute for a specific product to wrap the long product name.

In JavaScript we can access the HTML element and use the "overflowWrap" property of the style object to prevent long words from breaking the div element.

<html>
<head>
   <style>
      .container {
         width: 300px;
         border: 1px solid #ccc;
         padding: 10px;

      }
   </style>
</head>
<body>
   <h3> Preventing the long words breaking the div in HTML5 using the <i>overflow-wrap</i> property
   </h2>
   <div class = "container">
      <p class = "long-word"> Thisisaverylongwordthatshouldnotbreakinsideadiv. </p>
   </div>
   <script>
      let longWord = document.querySelector('.long-word');
      longWord.style.overflowWrap = 'break-word';
   </script>
</body>
</html>
Copy after login

Users learned to use different CSS properties to prevent long words from breaking div elements. In the first method we use the "word-break" CSS property to specify how the browser should break words. In the second approach, we use the "overflow-wrap" CSS property to specify how to handle overflow of the div element's content.

The above is the detailed content of How can I prevent long words from breaking my div?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Let's use (X, X, X, X) for talking about specificity Let's use (X, X, X, X) for talking about specificity Mar 24, 2025 am 10:37 AM

I was just chatting with Eric Meyer the other day and I remembered an Eric Meyer story from my formative years. I wrote a blog post about CSS specificity, and

See all articles