Table of Contents
前面的话
空白符
文本换行
表格
Home Web Front-end HTML Tutorial 深入理解CSS中的空白符和换行_html/css_WEB-ITnose

深入理解CSS中的空白符和换行_html/css_WEB-ITnose

Jun 24, 2016 am 11:22 AM

× 目录 [1]white-space [2]word-wrap [3]word-break

前面的话

  CSS3新增了两个换行属性word-wrap和word-break。把空白符和换行放在一起说,是因为实际上空白符是包括换行的,且常用的文本不换行是使用的空白符的属性white-space: nowrap;但到底它们还有些什么属性值,以及有什么对应的用法。本文将就空白符和换行的内容做详细介绍和梳理

 

空白符

定义

  空白符是指空格、制表符和回车;HTML默认已经把所有空白符合并成一个空格。

white-space

  值: normal | nowrap | pre | pre-wrap | pre-line | inherit

  初始值: normal

  应用于: 所有元素

  继承性: 有

normal: 合并空白符,允许自动换行nowrap: 合并空白符,不允许自动换行pre-line: 合并空白符(不包括换行符),允许自动换行pre: 不合并空白符,不允许自动换行pre-wrap: 不合并空白符,允许自动换行 
Copy after login

  [注意]

元素默认带有样式white-space: pre;</p>    <p>  [注意]IE7-浏览器不支持pre-line和pre-wrap这两个属性值</p>    <p class="sycode">         <pre class='brush:php;toolbar:false;'><div>They can stay 72-hours     within the Shandong      province after they have entered China via the Qingdao International Airport.</div>
Copy after login

文本换行

  浏览器自身带有文本自动换行的功能,文本容器的右侧可以实现自动换行

  对于英文来说,浏览器会在半角空格或连字符的地方自动换行,而不会在单词的中间突然换行

  对于中文来说,可以在任何一个文字后面换行,但浏览器碰到标点符号时,通常将标点符号以及其前一个文字作为一个整体进行换行

  所以实际上,white-space解决不了长单词或UTL地址的换行问题

word-wrap

  word-wrap属性用来实现长单词或URL地址的自动换行

  值: normal | break-word

  初始值: normal

  应用于: 所有元素

  继承性: 有

word-wrap:normal(浏览器只在半角空格或连字符的地方进行换行)word-wrap:break-word(截断单词换行,长单词从下一行开始)
Copy after login

  [注意]当white-space的值是nowrap或pre时,word-break和word-wrap属性都失效

  [注意]word-wrap在标准中被改为overflow-wrap,但由于兼容问题,一般还是使用word-wrap

word-break

  CSS3使用word-break属性来决定自动换行的处理方法。通过具体的属性设置,不仅可以让浏览器实现半角空格或连字符后面的换行,而且还可以让浏览器实现任意位置的换行。

  值: normal | break-all | keep-all

  初始值: normal

  应用于: 所有元素

  继承性: 有

normal: 中文到边界上的汉字换行,英文从整个单词换行break-all: 对于英文长单词来说,会截断单词换行,长单词占据当前行剩余空间。但对于中文的处理,各浏览器不一致    [1]firefox及safari: 中文到边界上的汉字换行,且允许标点置于段首    [2]IE及chrome: 中文到边界上的汉字换行,但不允许标点置于段首keep-all: 对于英文长文本不能换行,但对于中文的处理,各浏览器不一致    [1]firefox: 在空白符处换行    [2]IE及chrome: 在空白符及标点处换行    [3]safari: 不支持
Copy after login

  [注意]移动端目前基本都不支持keep-all值  

  [注意]当word-break值为break-all时,word-wrap属性失效;否则两个属性都起作用

 

表格

  对于表单元格的长文本来说,使用word-wrap或word-break来强制换行需要设置table-layout:fixed

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)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1263
29
C# Tutorial
1237
24
Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

See all articles