Home  >  Article  >  Web Front-end  >  css does not wrap and css wraps

css does not wrap and css wraps

WBOY
WBOYOriginal
2023-05-29 10:08:37611browse

CSS is an important technical component in front-end development. You can use CSS to optimize and beautify web pages. One very important aspect is adjusting the layout and style of text. There are two commonly used properties in CSS: no line wrapping and line wrapping. Different attribute settings can produce different effects.

CSS does not break lines

The white-space attribute in CSS can be used to control whether text is broken. It has the following values ​​​​that can be set:

  • normal: default value , automatic line wrapping
  • nowrap: no line wrapping
  • pre: retain all spaces and newlines
  • pre-wrap: retain newlines, but will automatically wrap
  • pre-line: Keep spaces and newlines, but will automatically wrap

Among them, the nowrap value can prevent the text from wrapping. The value of white-space is inherited and can be set in the parent element and then inherited to the child element.

For example, in the following code, the div element will not automatically wrap when the text is too long, but will continue to extend.

<div style="white-space: nowrap;">
  这是一个不会换行的超长文本测试,看看会不会出现换行。
</div>

CSS line break

Similarly, you can also use the word-break and word-wrap attributes in CSS to control line breaks.

  • The word-break attribute can control line breaks within words, and is usually used to control different texts such as English words and Chinese. Its values ​​are as follows:

    • normal: Default value, use the browser's default line wrapping method
    • keep-all: Keep all words in one line, no automatic line wrapping
    • break-all: When a single character is too long, force a new line (regardless of word boundaries)
    • break-word: When the entire word is too long, force a new line

For example, in the following code, the text of the p element is forced to wrap when a single character is too long through the word-break attribute.

<p style="word-break: break-all;">
  这里有一段texttexttexttexttexttexttexttexttexttexttext的文本。
</p>
  • The word-wrap attribute is used to control whether word boundaries are preserved when text wraps. Its values ​​are as follows:

    • normal: Default value, use the browser's default line breaking method
    • break-word: When the word is too long, line breaking is forced, but word boundaries are retained.

For example, in the following code, the text of the div element is set through the word-wrap attribute to force line wrapping when the word is too long, but the word boundaries must be retained.

<div style="word-wrap: break-word;">
  这里有一段texttexttexttexttexttexttexttexttexttexttext的文本。
</div>

Conclusion

In CSS, through the white-space, word-break and word-wrap attributes, you can flexibly control whether or not text is wrapped. In actual development, appropriate attributes can be selected and set according to specific needs to achieve the best reading experience and aesthetic effects.

The above is the detailed content of css does not wrap and css wraps. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:Set font cssNext article:Set font css