css prevents text from wrapping
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-space
The 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 ofnormal
andpre
; -
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; }
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; }
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; }
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; }
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; }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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.

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

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.

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

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

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.

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.
