Allowing CSS to break long words but not spaces for better readability

Imagine you have the following paragraph:

thisisaverylongwordthatneverbreaks

Applying word-wrap:break-word; results in:

thisisaverylongwordthat
neverbreaks

This works well, but what if I try:

this is avery long word that has spaces

The output becomes:

this
is
avery
long
word
that
has
spaces

My inquiry is how to achieve this layout:

this is avery long word that
neverbreaks

I want words to wrap at spaces but force breaks on long words. How can I accomplish this?

Answer №1

If you need the text to break at any point, use word-break: break-all;

Take a look at this demonstration where I set the background color to black for better visibility: JS Fiddle Example

Here's the HTML code:

<div>iama verylongwordthathasaspace yehey</div>

And here's the CSS code to achieve the desired effect:

div {
    width: 120px;
    word-break: break-all;
    background: black;
    color: white;  
}

You can find more information about the word-break property on CSS-Tricks: Word-break

Answer №2

When using the CSS property "word-break: break-all;" for languages like English or Latin, text will break at any character. This can result in very short words breaking awkwardly depending on available space. To see the difference between break-all and break-word, try resizing the container in the demo below...

Please note that these solutions are purely CSS-based. For more complex manipulations with words and characters, JavaScript would be required.

[draggable=true] {
  cursor: move;
}

.resizable {
  background: #e3e8ee;
  overflow: auto;
  resize: both;
  max-width: 360px;
  max-height: 360px;
  min-width: 140px;
  min-height: 360px;
  padding: .5em;
}

.resizable div {
  background: #fff;
  font: 1em/1.1em Arial, Helvetica, Sans-serif;
  margin-bottom: .5em;
  padding: .25em;
}

.resizable div h2 {
  font-size: 1em;
  font-weight: bold;
  margin: .1em 0;
}

.break-all span {
  word-break: break-all;
}

.break-word span {
  word-break: normal;
  word-wrap: break-word;
}

.hyphen span {
  hyphens: auto;
}

.truncate span {
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
<div draggable="true" class="resizable">
  <div class="break-all" lang="en">
    <h2>Break-all</h2>
    <span>iama verylongwordthathasaspace yehey</span>
  </div>
  <div class="break-word" lang="en">
    <h2>Break-word</h2>
    <span>iama verylongwordthathasaspace yehey</span>
  </div>
  <div class="break-word hyphen" lang="en">
    <h2>Bk-wd Hyphens</h2>
    <span>iama verylongwordthathasaspace yehey</span>
  </div>
  <div class="break-word hyphen truncate" lang="en">
    <h2>Bk-wd Truncate</h2>
    <span>iama verylongwordthathasaspace yehey</span>
  </div>
</div>

Answer №3

managing long words wrapping with spaces intact

To ensure your text behaves as desired, you can utilize a combination of CSS properties:

  • word-break: break-all; This CSS property allows long words to break within the container width instead of overflowing it.

  • white-space: pre-wrap; By using this property, white spaces are preserved and text wraps accordingly while respecting line breaks.

Below is the code for your specific situation:

HTML

<div class="paragraph-container">
  iama verylongwordthathasaspace yehey
</div>

CSS:

.paragraph-container {
  white-space: pre-wrap;
  word-break: break-all;
}

The Result will be:

iama verylongwordthathas

nospace yehey

We hope this information proves helpful.

Answer №4

.custom-css {
  word-wrap: break-word; /* Breaking long words */
  overflow-wrap: break-word; /* For older browsers */
  max-width: custom-value; /* Set container width */
}

<div class="custom-css">
  Thisisaverylongwordwithnospaces hooray
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Picture in the form of a radio button

Is there a way to use an image instead of a radio button in my AngularJS form? I have managed to hide the radio button using CSS, but it disables the checked event. How can I make the image clickable? position: absolute; left: -9999px; Here is the code s ...

When the only source is available, the image undergoes a transformation

Is there a way to dynamically adjust the height of an image using JQuery or JavaScript, but only when the image source is not empty? Currently, I have an image element with a fixed height, and even when there is no source for it, Chrome still reserves sp ...

The footer navigation in CSS shifts position when it is hovered over

My attempt to add a <nav> at the footer was unsuccessful. The navigation bar in this fiddle is completely wrong as the entire nav moves when hovered, even though I did not include any animations. I want the items in the <nav> to stay fixed. ...

Develop a versatile currency symbol mixin that can be used in various small text formats

I am currently working on a website where I need to display prices in multiple locations. To achieve this, I have created a sass mixin that is designed to add the desired currency symbol before the price with a smaller font-size. Below are examples of how ...

Highlighting table column when input is selected

I am working with a table where each <td> contains an <input>. My goal is to apply the class .highlighted to all the column <td>s when an <input> is being focused on. Additionally, I want to remove this class from all other columns ...

How to disable scrolling for React components with CSS styling

When incorporating a React component into my HTML, I follow this pattern: <html> <body> <div id=app>${appHtml}</div> <script src="/bundle.js"></script> </body> </html> Within my application, th ...

Customize Link Appearance Depending on Current Section

Looking for a way to dynamically change the color of navigation links based on which section of the page a user is currently viewing? Here's an example setup: <div id="topNav"> <ul> <li><a href="#contact">Contac ...

Avoiding additional empty lines between selectors when setting up Stylelint

Can anyone help me with configuring Stylelint options? Specifically, I want to enforce a rule that no empty lines are allowed between selectors in a list of selectors: &:focus, &:hover, &.active { color: #fb3a5e; text-align: left; text-de ...

Adaptable design tailored for smartphones

When it comes to developing mobile websites for various platforms such as iPhone 3 and 4, Android, Blackberry Torch, etc., I usually find myself needing to slice images based on the specific platform. The challenge arises from constantly having to slice im ...

The style of the button label does not persist when onChange occurs

Encountered an interesting issue here. There is a button designed for selection purposes, similar to a select item. Here's the code snippet: <button class="btn btn-primary dropdown-toggle" style="width: 166%;" type="button" id="dropdownMe ...

By default, the first table row is displayed when using table grouping in AngularJS

I am attempting to display only the first tr in the table and hide all other tr elements by default. I have tried using ng-show and ng-hide but it does not seem to be working as expected. This is not originally my plunker; I used it for reference on group ...

What is the proper way to define a class attribute for an HTML element within a PHP file?

I am having trouble writing the class attribute in a PHP file for an HTML element. Here is my code: echo '<div><p class="'.($value->getIsRequire() == 1) ? 'required' : ''.'"> </p> <input type="tex ...

What is the best method for selecting or isolating the code for a single animation created using JavaScript?

Currently, I am attempting to extract the CSS and JS code of an image reveal animation that is part of a static HTML page: https://i.stack.imgur.com/bnmaO.gif The challenge lies in the fact that the desired effect is one among many showcased image animat ...

Design interactive elements such as buttons and input fields inspired by the layout of Google websites

Does anyone know how to achieve the sleek, lightweight buttons and text boxes seen on Google's web pages like Google Search, Google Plus, and Google Play? I am currently using JSP and CSS, but I am open to incorporating jQuery if necessary. Any help w ...

Resetting CSS animations using animation-delay

My aim is to animate a series of images on my landing page using Next.js and SCSS. The issue arises when I try to add dynamic animations that reset after each cycle. The delay in the animation causes the reset to also be delayed, which disrupts the flow. I ...

Launch the jQuery Fancybox on a separate webpage

I am facing an issue where I have a link in my index.html page and I need it to open a div located in another page named index2.html, but for some reason, it is not working. This is the code I currently have: Link in index.html <a href="#modal" id="o ...

Adjusting font size to perfectly fit within its confines

When using v-for to map out objects from an array into cards, it's important to keep the height consistent throughout the section. Manually adjusting text size with @media queries can be tedious and inelegant, requiring multiple rules for different sc ...

Can you provide the keycodes for the numpad keys: "/" and "." specifically for the libraries I am utilizing or any other library that does not overlook them?

I've hit a roadblock with my Chrome Extension development. Despite using two different methods to add keyboard functionality, the keys "/" for divide and "." for decimal on the keypad are just not registering. I've attempted to tackle this issue ...

Obtain and utilize the background color to easily implement the same color in another window

For my Chrome Extension project, I am looking to retrieve the background color of the current page and then set the background color of a window to match. Can someone guide me on how to accomplish this using JavaScript (with or without jQuery), and if ne ...

Can CSS be used to communicate to JavaScript which media queries are currently in effect?

Is there a way for Javascript to detect when a specific CSS media query is active without repeating the conditions of the media query in Javascript? Perhaps something similar to an HTML data attribute, but for CSS. For example: CSS @media (min-width: 94 ...