Home Web Front-end CSS Tutorial Exploring CSS text wrapping properties: word-wrap and hyphens

Exploring CSS text wrapping properties: word-wrap and hyphens

Oct 27, 2023 pm 07:27 PM
text wrap word-wrap hyphens

CSS 文本换行属性探索:word-wrap 和 hyphens

Exploration of CSS text wrap properties: word-wrap and hyphens

In web design, text wrap processing is an important issue. When the text exceeds the width of the container, we need to choose an appropriate line wrapping method to ensure the readability and aesthetics of the content. This article will introduce two commonly used properties in CSS: word-wrap and hyphens, and provide specific code examples to illustrate their usage and effects.

  1. word-wrap attribute

The word-wrap attribute is used to specify how text is handled when wrapping. It has two optional values: normal and break-word.

  • normal: Default value. When text exceeds the width of the container, lines are wrapped between words, and if a word is too long to fit entirely on one line, it overflows to the next line.

Sample code:

<style>
.container {
  width: 200px;
  border: 1px solid #ccc;
  padding: 10px;
}

.normal-text {
  word-wrap: normal;
}
</style>

<div class="container">
  <p class="normal-text">这是一段很长很长很长很长很长很长的文本。</p>
</div>
Copy after login

Effect: The container width is 200px, and when the text exceeds the container width, line breaks will be performed between words.

  • break-word: When the text exceeds the width of the container, it will break inside the word, even if the newline character in the middle of the word does not exist.

Sample code:

<style>
.container {
  width: 200px;
  border: 1px solid #ccc;
  padding: 10px;
}

.break-word {
  word-wrap: break-word;
}
</style>

<div class="container">
  <p class="break-word">这是一段很长很长很长很长很长很长的文本。</p>
</div>
Copy after login

Effect: The container width is 200px, and when the text exceeds the container width, it will wrap inside the word.

  1. hyphens attribute

The hyphens attribute is used to control the use of hyphens to adapt to different languages ​​and text layout needs. It has three optional values: none, manual and auto.

  • none: Default value. Without hyphens, text wraps directly when needed.

Sample code:

<style>
.container {
  width: 200px;
  border: 1px solid #ccc;
  padding: 10px;
}

.no-hyphens {
  hyphens: none;
}
</style>

<div class="container">
  <p class="no-hyphens">这是一个没有连字符的例子。这个长长长长的单词不会被自动断行,而是直接溢出到下一行。Hyphens are not used in this example, so the long word will overflow to the next line instead of being automatically broken.</p>
</div>
Copy after login

Effect: The container width is 200px, and when the text exceeds the container width, the words overflow directly to the next line.

  • manual: Manual hyphenation, manually add hyphens according to the language's hyphenation rules.

Sample code:

<style>
.container {
  width: 200px;
  border: 1px solid #ccc;
  padding: 10px;
}

.manual-hyphens {
  hyphens: manual;
}
</style>

<div class="container">
  <p class="manual-hyphens">这是一个使用手动断字的例子。这个长长长长的单词被手动添加了连字符,使其在需要换行时正确断行显示。</p>
</div>
Copy after login

Effect: The container width is 200px, and when the text exceeds the container width, the words will be correctly broken and displayed according to the language's hyphenation rules.

  • auto: Automatically add hyphens according to the language's hyphenation rules.

Sample code:

<style>
.container {
  width: 200px;
  border: 1px solid #ccc;
  padding: 10px;
}

.auto-hyphens {
  hyphens: auto;
}
</style>

<div class="container">
  <p class="auto-hyphens">这是一个使用自动断字的例子。这个长长长长的单词会根据语言的断字规则自动添加连字符,使其在需要换行时正确断行显示。</p>
</div>
Copy after login

Effect: The container width is 200px, and when the text exceeds the container width, the words will be correctly broken and displayed according to the language's hyphenation rules.

Summary:

By using the two CSS properties word-wrap and hyphens, we can have more fine-grained control over the wrapping of text. The word-wrap attribute is used to specify the way to wrap lines, and you can choose to wrap lines between words or within words; the hyphens attribute is used to control the use of hyphens, and you can choose not to use hyphens, add hyphens manually, or add hyphens automatically. In actual web design, we can choose appropriate attribute values ​​according to the characteristics and language requirements of the text to achieve better readability and typesetting effects.

The above is the detailed content of Exploring CSS text wrapping properties: word-wrap and hyphens. For more information, please follow other related articles on the PHP Chinese website!

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

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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Making Your First Custom Svelte Transition Making Your First Custom Svelte Transition Mar 15, 2025 am 11:08 AM

The Svelte transition API provides a way to animate components when they enter or leave the document, including custom Svelte transitions.

Working With GraphQL Caching Working With GraphQL Caching Mar 19, 2025 am 09:36 AM

If you’ve recently started working with GraphQL, or reviewed its pros and cons, you’ve no doubt heard things like “GraphQL doesn’t support caching” or

Show, Don't Tell Show, Don't Tell Mar 16, 2025 am 11:49 AM

How much time do you spend designing the content presentation for your websites? When you write a new blog post or create a new page, are you thinking about

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Creating Your Own Bragdoc With Eleventy Creating Your Own Bragdoc With Eleventy Mar 18, 2025 am 11:23 AM

No matter what stage you’re at as a developer, the tasks we complete—whether big or small—make a huge impact in our personal and professional growth.

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Let's use (X, X, X, X) for talking about specificity Let's use (X, X, X, X) for talking about specificity Mar 24, 2025 am 10:37 AM

I was just chatting with Eric Meyer the other day and I remembered an Eric Meyer story from my formative years. I wrote a blog post about CSS specificity, and

See all articles