Home > Article > Web Front-end > How to prevent text from wrapping in css
In web development, it is often encountered that a whole paragraph of text needs to be displayed in a cell or container, but the text is too long, resulting in line breaks that affect the appearance. At this time, we can use CSS style control to prevent the text from wrapping and achieve more beautiful layout.
The following introduces several methods to control text not to wrap in CSS.
1. White-space attribute
In CSS, the white-space attribute is used to control the layout of text. By default, the value of the white-space attribute is "normal", that is, when it encounters whitespace characters such as spaces, newlines, tabs, etc., it will automatically wrap and no longer typeset forward. We just need to set it to "nowrap" so the text won't wrap.
The example is as follows:
.container { white-space: nowrap; }
In the above code, .container
represents the container that needs style control, and set white-space
to nowrap
will prevent the text from wrapping.
2. Overflow attribute
Another way to control text from wrapping is to use the overflow attribute. Use this attribute to hide or show the scroll bar when the content in the container exceeds the scope of the container. Here, we can set the value of overflow to "hidden", so that the text of the tributary beyond the scope of the container can be hidden, and the text will not wrap.
The example is as follows:
.container { overflow: hidden; }
In the above code, .container
represents the container for style control, and set overflow
to hidden
prevents text from wrapping.
3. text-overflow attribute
In some cases, the text is too long but cannot be hidden. It is necessary to display part of the text content and use ellipsis to indicate the undisplayed part. In this case, you can use text-overflow property. Use this attribute to display ellipses when the text exceeds a certain length, and the text will not wrap.
The example is as follows:
.container { overflow: hidden; /* 必须要设置overflow属性值为“hidden” */ white-space: nowrap; /* 禁止文本换行 */ text-overflow: ellipsis; /* 超出容器大小的文本以省略号表示 */ }
In the above code, .container
represents the container for style control, and set overflow
to hidden# Set ##,
white-space to
nowrap and
text-overflow to
ellipsis to prevent text from wrapping and display ellipses.
The above is the detailed content of How to prevent text from wrapping in css. For more information, please follow other related articles on the PHP Chinese website!