Table of Contents
line-height
text-decoration
text-indent
text-overflow
text-shadow
white-space
word-break
word-wrap
Home Web Front-end HTML Tutorial A few things about text attributes of CSS basics_html/css_WEB-ITnose

A few things about text attributes of CSS basics_html/css_WEB-ITnose

Jun 24, 2016 pm 12:04 PM
css basic knowledge Attributes text

line-height

You can specify a scaling factor without a unit for an element, so that its descendant elements will inherit this scaling factor and then adjust it according to their own font size To calculate your own line-height value,

body {  font-size: 12px;  line-height: 1.5;}h1 {  font-size: 36px;}
Copy after login

Here, the line-height of body is 18px (12 * 1.5), and the line-height of h1 is is 54px (36*1.5).

Even if relative units such as em and percentage are used, descendant elements still inherit the calculated line-height (line-height) value. For example, if the line-height of the body above is changed to 1.5em, then the line-height of h1 Will inherit this value 18px.

text-decoration

Text decoration is a non-inherited class attribute. Setting it to none for the body element will not affect the elements in the descendants that have text decoration by default, such as Hyperlink, so if you want to remove the default underline of the hyperlink, you still need to set it separately.

a {  text-decoration: none;}
Copy after login

Although this attribute is not inherited by default, the modification set on the ancestor element will "extend" ” into the descendant elements,

<p>我有下划线 <span>我咋会有下划线呢?</span></p>
Copy after login

p {  color: red;  text-decoration: underline;}p span {  color: green;  text-decoration: none;}
Copy after login

The underscore here is for the p element.

p {  color: red;  text-decoration: underline;}p span {  color: green;  text-decoration: underline;}
Copy after login

Here the underline of span covers the underline of p.

p {  color: red;  text-decoration: underline;}p span {  color: green;  text-decoration: overline;}
Copy after login

Since the extension of text decoration attributes will cause some compatibility issues,

The best way is to set the text-decoration attribute separately for the text that needs to be modified.

text-indent

You can use this attribute to indent the first line of each paragraph by 2 characters instead of using spaces,

p {  text-indent: 2em;}
Copy after login

You can also specify a negative value to produce the effect of the first row hanging,

text-indent: -3em;
Copy after login

Let Hanging quotation marks is also a common practice,

Indent the text to a place far enough that the text disappears,

text-indent: -9999px;
Copy after login

Therefore, the usual method of replacing text on images,

.logo {  background: url(logo.png) no-repeat;  display: inline-block;  height: 36px;  text-indent: -9999px;  width: 72px;}
Copy after login

IE6/7 does not really support inline-block, and in some cases may cause the .logo to disappear. You can Use float or block instead of inline-block, but both will change the layout, and other methods can also be used.

text-overflow

Normally, a long string of URL addresses will overflow when it exceeds the container. We can set it to be omitted when the text overflows the containing container. Symbols,

li {  overflow: hidden;  text-overflow: ellipsis;}
Copy after login

must be used together with overflow: hidden;; sometimes you may have to add a width, such as IE 6,

For those texts that do not overflow by default, you need to force them to be displayed in one line in order to have an effect.

li {  overflow: hidden;  text-overflow: ellipsis;  <strong>white-space</strong>: <strong>nowrap</strong>;  width: 100%; /* for IE 6 */ }
Copy after login

Use white-space: pre; or word-break: keep-all; can also force the text to be displayed on one line, but each has its own problems.

In IE 8/9, sometimes you will find that it has no effect. It may be that one of the ancestor elements has word-wrap: break-word; and this attribute will perform better than white-space : nowrap; is more powerful, so sometimes you have to add the following code,

word-wrap: normal;
Copy after login

text-shadow

in non-white You can achieve beautiful inline effects on dark text on the background.

text-shadow: 0 1px 0 rgba(255,255,255,.75);
Copy after login

You can add multiple shadows to the text, separated by commas,

text-shadow: 0 1px 0 #fff, 0 2px 0 #ddd, 0 3px 0 #ddd, 0 4px 0 #ddd;
Copy after login

white-space

The white-space attribute sets how to handle whitespace within the element.

Set the value to nowrap so that the text can continue on the same line and will not wrap when it encounters a boundary until it encounters the
tag,

white-space: nowrap;
Copy after login

Sometimes we want to retain spaces and line breaks in the text. For example, when displaying computer source code,

 will be used, and the value of the white-space attribute of <pre class="brush:php;toolbar:false"> is pre, </p> <p class="sycode"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>pre {  white-space: pre;}
Copy after login

pre is not so satisfactory. It will not automatically wrap when encountering a boundary, so CSS 2.1 adds pre-wrap,

pre {  white-space: pre;  white-space: pre-wrap;}
Copy after login

In this way, the content in the pre element can not only maintain the original format, but also automatically wrap when the content exceeds the boundary.

Since not all browsers support pre-wrap, you need to force those browsers that do not support line breaks,

pre {  white-space: pre;  white-space: pre-wrap;  <strong>word-wrap</strong>:<strong> break-word</strong>;}
Copy after login

Of course, maybe you don’t If you want line breaks and don’t mind horizontal scroll bars, you can use horizontal scroll bars to replace line breaks,

pre {  overflow: auto;}
Copy after login

word-break

Currently, the webkit family (including Google Chrome, Safari, Android Browser, etc.) does not support the keep-all value, so the only one that can be used is break-all,

word-break: break-all;
Copy after login

However, it will cause difficulties in reading English text and should be used with caution,

word-wrap

这是比 word-break 更好的实现文本换行的方式,

word-wrap: break-word;
Copy after login

再添加一个溢出隐藏,避免一些恶意的连续字符,

overflow: hidden;
Copy after login

 

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)

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

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.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to view the date of bootstrap How to view the date of bootstrap Apr 07, 2025 pm 03:03 PM

Answer: You can use the date picker component of Bootstrap to view dates in the page. Steps: Introduce the Bootstrap framework. Create a date selector input box in HTML. Bootstrap will automatically add styles to the selector. Use JavaScript to get the selected date.

See all articles