search
HomeWeb Front-endCSS TutorialDetailed explanation of CSS text layout properties: text-overflow and white-space

CSS 文本排版属性详解:text-overflow 和 white-space

Detailed explanation of CSS text layout properties: text-overflow and white-space

In web design, text layout is a very important link, and reasonable layout can Make text more readable and beautiful. CSS provides several properties to control how text is displayed, including text-overflow and white-space. This article will detail the usage and sample code of these two properties.

1. Text-overflow attribute

The text-overflow attribute is used to control how text is displayed when it exceeds the container. Common values ​​include the following:

  1. clip: Default value, the part beyond the container will be clipped.
  2. ellipsis: The part beyond the container will be displayed with ellipses (...).
  3. string: The displayed string can be customized.

The following is the sample code:

<style>
  .container {
    width: 200px;
    white-space: nowrap; /* 强制不换行 */
    overflow: hidden; /* 超出容器部分隐藏 */
    text-overflow: ellipsis; /* 超出部分以省略号显示 */
  }
</style>

<div class="container">
  This is a long text that should be truncated with an ellipsis when it overflows.
</div>

In the above code, we use a container and set the width to 200px, and the text content is a long sentence. By setting the white-space attribute to nowrap, it means that no line wrapping is forced, and the overflow attribute is hidden, which means that the part beyond the container is hidden. The most important thing is the text-overflow property, which we set to ellipsis, which means that the excess is displayed with ellipses.

2. White-space attribute

The white-space attribute is used to control the way whitespace characters are processed in text. Common values ​​are as follows:

  1. normal: Default value, automatically handles whitespace characters, merges consecutive whitespace characters into one space, and newlines and tabs are also treated as spaces.
  2. nowrap: Force no line breaks, merge consecutive whitespace characters into one space.
  3. pre: Keep the original whitespace characters and maintain the whitespace format of the text.
  4. pre-wrap: Keep the original whitespace characters, allow line breaks, and the text will be displayed in the original format.
  5. pre-line: Automatically handle whitespace characters, but retain line breaks, allow line breaks, and the text will be displayed in the original format.

The following is the sample code:

<style>
  .container {
    white-space: pre-wrap; /* 保留原始的空白符,允许换行 */
  }
</style>

<div class="container">
  This is a long text that should wrap when it reaches the container's width.
</div>

In the above code, we use a container and set the white-space attribute to pre-wrap, so that the text will retain the original whitespace, allowing line breaks.

By using the text-overflow and white-space properties, we can more flexibly control the layout of the text to make it more beautiful and readable. In actual web design, we can choose appropriate values ​​as needed and debug based on the sample code.

Summary:

text-overflow and white-space are properties in CSS used to control text layout. text-overflow is used to control the display method when the text exceeds the container. Common values ​​​​are clip, ellipsis and string; white-space is used to control the way whitespace characters are processed in the text. Common values ​​​​are normal, nowrap, pre, pre-wrap and pre-line. By properly applying these two properties, we can achieve better text layout effects.

The above is the detailed content of Detailed explanation of CSS text layout properties: text-overflow and white-space. 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
CSS 边界属性详解:padding,margin 和 borderCSS 边界属性详解:padding,margin 和 borderOct 21, 2023 am 11:07 AM

CSS边界属性详解:padding,margin和borderCSS是一种用于控制和布局网页元素的样式表语言。在网页设计中,边界属性是其中一项非常重要的部分。本文将详细介绍CSS中边界属性的使用方法,并提供具体的代码示例。padding(内边距)padding属性用于设置元素的内边距,即元素内容和元素边界之间的空间。我们可以用正数或百分比值来设置内边距

margin在css中什么意思margin在css中什么意思Dec 18, 2023 am 10:30 AM

在CSS中,margin是一个用于设置元素外边距的属性。外边距是元素边框与元素内容之间的空间。margin可以接受以下几种值:1、单独的值:例如,margin: 10px; 将所有四个外边距(上、右、下、左)都设置为10像素;2、两个值:例如,margin: 10px 20px; 将上下外边距设置为10像素,左右外边距设置为20像素;3、四个值等等。

CSS 文本排版属性详解:text-overflow 和 white-spaceCSS 文本排版属性详解:text-overflow 和 white-spaceOct 20, 2023 am 11:19 AM

CSS文本排版属性详解:text-overflow和white-space在网页设计中,文本排版是一个非常重要的环节,通过合理的排版可以使文本更易读、更美观。CSS提供了一些属性来控制文本的显示方式,其中包括text-overflow和white-space。本文将详细介绍这两个属性的用法和示例代码。一、text-overflow属性text

margin在html中的意思是什么margin在html中的意思是什么Sep 13, 2021 pm 04:26 PM

在html中,margin的意思为“外边距”,指的是围绕在元素边框的空白区域;设置外边距会在元素外创建额外的“空白”,让盒子与盒子之间有一个“空白”距离。设置外边距需要使用css margin属性,该属性接受任何长度单位、百分数值甚至负值。

css中的padding属性怎么用css中的padding属性怎么用Dec 07, 2023 pm 02:58 PM

在CSS中,padding属性用于设置元素的内边距。这意味着它定义了元素内容和其边框之间的空间。基本语法为“padding: value;”。

margin属性不影响行内元素margin属性不影响行内元素Feb 18, 2024 pm 04:36 PM

Margin对于行内元素的效果是不同于块级元素的。在行内元素中,margin属性只会对垂直方向的上下外边距起作用,而不会对水平方向的左右外边距起作用。举个例子,在HTML中有一个段落元素,我们可以为其设置一些样式,并观察margin属性对其的效果。HTML代码如下所示:

css文件margin什么意思css文件margin什么意思Jan 30, 2023 am 09:34 AM

css文件margin是一个用于定义元素周围空间的css属性;margin表示外边距,可以单独改变元素的上,下,左,右边距,也可以一次改变所有的属性;margin属性接受任何长度单位、百分数值甚至负值。

CSS 盒模型属性探索:padding,margin 和 borderCSS 盒模型属性探索:padding,margin 和 borderOct 20, 2023 pm 03:09 PM

CSS盒模型属性探索:padding,margin和borderCSS盒模型是网页布局的重要概念之一。在前端开发中,了解和正确使用padding,margin和border属性是关键。本文将深入探讨这三个属性的用法和相互之间的关联,并提供具体的代码示例。一、盒模型简介盒模型由四个部分组成:content(内容),padding(内边距),bo

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor