Table of Contents
1. white-space attribute
2. word-break attribute
3. overflow attribute
4. text-overflow attribute
5. Use pseudo-elements
Home Web Front-end Front-end Q&A css prevents text from wrapping

css prevents text from wrapping

May 21, 2023 am 09:41 AM

In web design and development, we usually need to control the layout and style of page elements. One of the more common problems with text layout is how to prevent text from wrapping. This article will introduce some common methods in CSS to help you solve this problem.

1. white-space attribute

white-spaceThe attribute determines how to handle the white space in the element box. It has 5 optional values: normal , nowrap, pre, pre-wrap, pre-line. Their effects are:

  • normal: Default value, merge excess blanks, text will not wrap, text will automatically wrap if it cannot fit on one line;
  • nowrap: No line breaks, which is equivalent to combining the features of normal and pre;
  • pre: Reserved Excess white space, no merging, no line wrapping. If you need to wrap a line, you need to add `
    ` manually;
  • pre-wrap: Keep excess white space, no merging, and automatic line wrapping, if necessary. In the case of line wrapping, it will be automatically wrapped;
  • pre-line: Merge excess blanks, and the text will be automatically wrapped if it cannot fit on one line.

Therefore, when we need to prevent text from wrapping automatically, we can use white-space: nowrap.

div {
  white-space: nowrap;
}
Copy after login

Of course, if you want to retain excess blanks, you can also use other values. For example, if you want the text to appear completely on one line, you can use white-space: pre.

div {
  white-space: pre;
}
Copy after login

2. word-break attribute

After setting the white-space attribute, if the text length exceeds the width of the box, it will automatically wrap. At this time, you need to use the word-break attribute to control the line breaking of text. It has three optional values: normal, break-all, keep-all. Their effects are:

  • normal: Default value, line breaks between words according to standard word breaking rules;
  • break -all: Break lines between words, and do not consider any rules for line breaking within English words;
  • keep-all: Break lines between words, but within English words Keep doing it.

Therefore, you can prevent text from automatically wrapping by setting the value of the word-break attribute to keep-all.

div {
  white-space: nowrap;
  word-break: keep-all;
}
Copy after login

3. overflow attribute

In some cases, even if the correct text layout is set through the white-space and word-break attributes way, the text length may also exceed the width of the box. At this time, you need to use the overflow attribute to control the overflow effect of the text. The overflow attribute has 4 optional values: visible, hidden, scroll, auto. Their effects are:

  • visible: Default value, no processing is performed, allowing content beyond the box range to be displayed outside the box;
  • hidden: The overflow part is hidden and invisible;
  • scroll: The scroll bar is displayed, and the user can view the overflow part through the scroll bar;
  • auto : Display scroll bars according to actual needs.

If you want the text to be displayed on one line without automatic line wrapping, and the overflow part is hidden, you can use the white-space and overflow properties in combination.

div {
  white-space: nowrap;
  overflow: hidden;
}
Copy after login

4. text-overflow attribute

When using the overflow: hidden attribute to hide the text overflow part, it may cause the user not to see the complete text content. At this time, you can use the text-overflow attribute to control the overflow effect of text. The text-overflow attribute has two optional values: clip, ellipsis. Their effects are:

  • clip: truncate the text and hide the excess part without adding ellipsis;
  • ellipsis : Truncate the text, hide the excess part, and add ellipsis symbols (...).

Therefore, you can truncate overly long text and add ellipses by setting the value of the text-overflow attribute to ellipsis.

div {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
Copy after login

5. Use pseudo-elements

In addition to the above methods, you can also control text not to wrap automatically by using pseudo-elements in CSS. For example, you can use "

The above is the detailed content of css prevents text from wrapping. 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)

How do you connect React components to the Redux store using connect()? How do you connect React components to the Redux store using connect()? Mar 21, 2025 pm 06:23 PM

Article discusses connecting React components to Redux store using connect(), explaining mapStateToProps, mapDispatchToProps, and performance impacts.

React's Role in HTML: Enhancing User Experience React's Role in HTML: Enhancing User Experience Apr 09, 2025 am 12:11 AM

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

How do you define routes using the <Route> component? How do you define routes using the <Route> component? Mar 21, 2025 am 11:47 AM

The article discusses defining routes in React Router using the &lt;Route&gt; component, covering props like path, component, render, children, exact, and nested routing.

What are the limitations of Vue 2's reactivity system with regard to array and object changes? What are the limitations of Vue 2's reactivity system with regard to array and object changes? Mar 25, 2025 pm 02:07 PM

Vue 2's reactivity system struggles with direct array index setting, length modification, and object property addition/deletion. Developers can use Vue's mutation methods and Vue.set() to ensure reactivity.

What are Redux reducers? How do they update the state? What are Redux reducers? How do they update the state? Mar 21, 2025 pm 06:21 PM

Redux reducers are pure functions that update the application's state based on actions, ensuring predictability and immutability.

What are the benefits of using TypeScript with React? What are the benefits of using TypeScript with React? Mar 27, 2025 pm 05:43 PM

TypeScript enhances React development by providing type safety, improving code quality, and offering better IDE support, thus reducing errors and improving maintainability.

What are Redux actions? How do you dispatch them? What are Redux actions? How do you dispatch them? Mar 21, 2025 pm 06:21 PM

The article discusses Redux actions, their structure, and dispatching methods, including asynchronous actions using Redux Thunk. It emphasizes best practices for managing action types to maintain scalable and maintainable applications.

How can you use useReducer for complex state management? How can you use useReducer for complex state management? Mar 26, 2025 pm 06:29 PM

The article explains using useReducer for complex state management in React, detailing its benefits over useState and how to integrate it with useEffect for side effects.

See all articles