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

Why do style assignments lose their motion when executed right after being made?

If you take a look at this specific fiddle in Webkit, you will see exactly what I am referring to. Is there a way to define the style of an element when it is first specified, and then its final state? I would like to be able to fully define a single-ste ...

Troubleshooting AngularJS Directives' Cross-Origin Problems within Eclipse

Hello, I am facing an issue while using Angular JS in Eclipse. Specifically, when attempting to use Directives, I encounter a problem with the Cross-Origin Resource Sharing (CORS) policy when loading the Directives template in the index.html file. XMLHttp ...

Troubleshooting a Custom Menu Control in HTML

Would you mind taking a look at the menu I have set up in this fiddle: http://jsfiddle.net/Gk_999/mtfhptwo/3 (function ($) { $.fn.menumaker = function (options) { var cssmenu = $(this), settings = $.extend({ title: "Menu", ...

Learn how to dynamically switch the background image of a webpage using a button in AngularJS

Hey there, I'm currently working on incorporating interactive buttons into my website to give users the ability to customize the background. I've been experimenting with Angular to achieve this feature. So far, I've managed to change the ba ...

What is the best way to create a border with an inside radius?

Looking to design a div with an inner circle in the corner similar to the image below. Seeking assistance to find a solution for this issue. ...

Can CSS stylesheets be set up to automatically load based on the browser being used to access the website?

I am inquiring because I recently completed a beautiful homepage design and, after viewing the website on Chrome and Safari on OSX, I noticed that certain elements were broken when I opened it in Firefox. In addition, an issue I was experiencing with the ...

Concealing specific sections of HTML content in a webview on the fly

I've been experimenting with a proof of concept feature to implement the ability to conceal certain parts of a web page loaded in a webview, but I seem to be encountering some issues... Within a UIWebview extension, I have something similar to this c ...

What steps should I take in modifying my existing code to use jQuery to set my div to a minimum height rather than a fixed height?

Could someone assist me in adjusting my div to have a min-height instead of a regular height? Whenever I click on the "Learn more" button, it extends past my div because the function is designed to set a specific height rather than an equal height. $.fn.e ...

Is it possible to have a hidden div box within a WordPress post that is only visible to the author of the

How can I create a div box with "id=secret" inside a post that is only visible to the author of the post? I initially made it for the admin, but now I want the id to be visible exclusively to the post's author. For instance: If the author is curren ...

Is there a way to automatically determine the text direction based on the language in the input text?

When posting in Google Plus (and other platforms), I noticed that when I type in Persian, which is a right-to-left language, the text direction changes automatically to rtl and text-alignment:right. However, when I switch to English, the direction switches ...

Ensure that buttons are cropped when extending beyond the border of another DIV

My issue involves a main DIV with buttons contained within it. I am seeking a solution to ensure that the buttons are cut off at the edge of the DIV, similar to the concept illustrated in this image. In the image, the blue represents the body, the light gr ...

Placing a crimson asterisk beside the input field within Bootstrap

Trying to insert a red * at the end of my input field using bootstrap, but all my attempts so far have been unsuccessful. Is there a way to align the Currently experiencing this issue: https://i.stack.imgur.com/61pzb.png Code snippet: .required-after ...

Swap a jQuery class with another if the class for ul li is currently active

I am currently developing a form builder and I would like to customize the appearance, specifically changing the color of the text. I want the text to be white when the class is set to active, and black when the class is not active. Is there a way to achi ...

Deactivate interactive functions on the mat-slider while preserving the CSS styling

I'm attempting to create a mat-slider with a thumb label that remains visible at all times. Below is my mat-slider component: <mat-slider class="example-margin" [disabled]="false" [thumbLabel]="true" [tickInterval]="tickInterval" ...

How can I make the burger icon responsive and centered in my navbar?

Struggling to center the burger icon in the navbar has been a bit of a challenge for me. Although it appears centered on small mobile devices, the icon shifts to the upper side on larger screens like tablets, losing its centered position. I am seeking advi ...

What exactly is the CSS element grouping operator?

I am investigating the concept of element grouping in CSS. Consider the following code snippet: .user-form .group-area .test, .user-form .group-area .test2{} Is it possible to group .test and .test2 elements together like this: .user-form .group-area (. ...

Unable to adjust the width of a table column within a horizontally scrollable container

I am struggling to resize the columns of a large table with 20 headers. Despite trying to place the table inside a div with overflow:auto, I still cannot achieve horizontal scrolling or make the div expand when resizing the columns. A sample code snippet ...

When utilizing *NgIf, the button will be shown without the accompanying text being displayed

When trying to display either a confirm or cancel button based on a boolean set in my component.ts, I implemented the following code in my HTML: <mat-dialog-actions class="dialog-actions"> <button class="cancel-btn" ...

Flex: 1 does not completely fill the Angular layout div in vertical dimensions

I am currently developing a sidebar using Angular and Flex. The app-sidebar is nested within the Angular component layout shown below: <div fxlayout="row" fxFill> <app-sidebar fxLayout="column" fxFlex="10%"></app-sidebar> <div ...

Switch image upon hover within a sprite

I have a sprite that I utilize for displaying various images. Currently, my aim is to change the sprite image upon hovering using solely CSS. .sprE { background-color: transparent; background-image: url(/i/fW.png); background-repeat: no-repeat; height: 30 ...