search
HomeWeb Front-endHTML Tutorial第 21 章 CSS3 文本效果 - 水之原

学习要点:

1.文本阴影

2.文本裁剪

3.文本描边

4.文本填充

 

主讲教师:李炎恢

 

本章主要探讨 HTML5 中 CSS3 中文本效果,其中也包含一些之前讲过的 CSS3 文本属性。

 

一.文本阴影

  CSS3 提供了 text-shadow 文本阴影效果,这个属性在之前讲过,只是没有涉及浏览器支持情况。


 text-shadow

 

Opera

Firefox

Chrome

IE

Safari

9.5+

3.5+

4+

10+

3.1+

  这里有几个注意点:1.text-shadow 在 CSS2 的时候出现过,但各大浏览器碍于消耗大量的资源,迟迟未支持,然后在 CSS2.1 中剔除了。现在,CSS3 已经全面支持了;2.最低支持版本上,不同手册和教材上都不太同,但版本年代久远,无伤大雅。最准确的可以查询这个网站:http://caniuse.com。最需要注意的只有 IE10 才支持,IE9 不支持的;3.目前的浏览器不需要给这个属性加上任何前缀,具体查询前缀版本可以访问刚才提供的地址。

//正值阴影

<span style="color: #800000;">text-shadow: 1px 1px 1px red;</span>

//负值阴影

<span style="color: #800000;">text-shadow: -1px -1px 1px red;</span>

//多重阴影叠加

<span style="color: #800000;">text-shadow:0px 0px 0 rgb(188,178,188), 
            1px 0px 0 rgb(173,163,173), 
            2px 0px 0 rgb(157,147,157), 
            3px 0px 0 rgb(142,132,142), 
            4px 0px 0 rgb(126,116,126), 
            5px 0px 0 rgb(111,101,111), 
            6px 0px 0 rgb(95,85,95), 
            7px 0px 0 rgb(79,69,79),
            8px 0px 7px rgba(0,0,0,0.35), 
            8px 0px 1px rgba(0,0,0,0.5), 
            0px 0px 7px rgba(0,0,0,0.2);</span>

 

二.文本裁剪

  CSS3 提供了 text-overflow 属性来控制文本的溢出部分,它的作用是对溢出的部分裁剪掉,然后判定是否添加省略号。首先我们先看下样式表的属性,如下:

属性值

说明

clip

默认值,裁剪文本时不添加“...”省略号

ellipsis

裁剪文本时添加“...”省略号

//必须不换号和使用 overflow 控制溢出 

<span style="color: #800000;">p </span>{<span style="color: #ff0000;">
    width</span>:<span style="color: #0000ff;"> 160px</span>;<span style="color: #ff0000;">
    white-space</span>:<span style="color: #0000ff;"> nowrap</span>;<span style="color: #ff0000;">
    background</span>:<span style="color: #0000ff;"> silver</span>; <span style="color: #008000;">/*</span><span style="color: #008000;">text-overflow: clip;</span><span style="color: #008000;">*/</span><span style="color: #ff0000;">
    text-overflow</span>:<span style="color: #0000ff;"> ellipsis</span>;<span style="color: #ff0000;">
    overflow</span>:<span style="color: #0000ff;"> hidden</span>;
}

对于 text-overflow 的支持度,是根据它的属性值来判定的,不同的属性值浏览器支持度不同。

属性值

Opera

 

Firefox

Chrome

IE

Safari

clip

9.63+

 

2.0+

1.0+

6.0+

3.1+

ellipsis

10.5+

 

6.0+

1.0+

6.0+

3.1+

//在 Opera 早期版本 10.0 ~ 10.1 中,需要使用带前缀的-o-。

<span style="color: #800000;">p </span>{<span style="color: #ff0000;">
    -o-text-overflow</span>:<span style="color: #0000ff;"> ellipsis</span>;<span style="color: #ff0000;">
    text-overflow</span>:<span style="color: #0000ff;"> ellipsis</span>;
}

而在 Opera 主流版本中,引擎已经靠拢 webkit,则不需要-o-了。目前来说,也不需要兼容-o-了。

 

三.文本描边

  CSS3 提供了描边属性,即 text-stroke、text-stroke-width、text-stroke-color。目前只有 webkit 引擎的浏览器支持,并且必须加上-webkit-前缀才能有效。

属性

Opera

Firefox

Chrome

IE

Safari

text-stroke 系列

15.0+

不支持

4.0+

不支持

3.1+

//实验阶段的产物,了解即可

<span style="color: #800000;">p </span>{<span style="color: #ff0000;">
    font-size</span>:<span style="color: #0000ff;"> 50px</span>;<span style="color: #ff0000;">
    -webkit-text-stroke</span>:<span style="color: #0000ff;"> 1px red</span>;
}

//修改描边的颜色和厚度 

<span style="color: #800000;">p </span>{<span style="color: #ff0000;">
    font-size</span>:<span style="color: #0000ff;"> 50px</span>;<span style="color: #ff0000;">
    -webkit-text-stroke</span>:<span style="color: #0000ff;"> 1px red</span>;<span style="color: #ff0000;">
    -webkit-text-stroke-color</span>:<span style="color: #0000ff;"> orange</span>;<span style="color: #ff0000;">
    -webkit-text-stroke-width</span>:<span style="color: #0000ff;"> 2px</span>;
}

 

四.文本填充

  CSS3 提供了一个文本颜色填充功能:text-fill-color,感觉和 color 属性很像。其实在配合其他属性才能达到不一样的效果。

属性

Opera

Firefox

Chrome

IE

Safari

text-fill-color

15.0+

不支持

4.0+

不支持

3.1+

//不配合背景样式时,和 color 属性没区别 

<span style="color: #800000;">p </span>{<span style="color: #ff0000;">
    font-size</span>:<span style="color: #0000ff;"> 150px</span>;<span style="color: #ff0000;">
    -webkit-text-fill-color</span>:<span style="color: #0000ff;"> red</span>;
}

//和 CSS3 背景的新特性搭配产生渐变文字 

<span style="color: #800000;">p </span>{<span style="color: #ff0000;">
    font-size</span>:<span style="color: #0000ff;"> 150px</span>;<span style="color: #ff0000;">
    font-family</span>:<span style="color: #0000ff;"> 黑体</span>;<span style="color: #ff0000;">
    background</span>:<span style="color: #0000ff;"> -webkit-linear-gradient(top,#eee,#aaa 50%,#333 51%,#000)</span>;<span style="color: #ff0000;">
    -webkit-background-clip</span>:<span style="color: #0000ff;"> text</span>;<span style="color: #ff0000;">
    -webkit-text-fill-color</span>:<span style="color: #0000ff;"> transparent</span>;
}
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
Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

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

The Role of HTML: Structuring Web ContentThe Role of HTML: Structuring Web ContentApr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTML and Code: A Closer Look at the TerminologyHTML and Code: A Closer Look at the TerminologyApr 10, 2025 am 09:28 AM

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS, and JavaScript: Essential Tools for Web DevelopersHTML, CSS, and JavaScript: Essential Tools for Web DevelopersApr 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.

The Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesThe Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesApr 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.

Is HTML easy to learn for beginners?Is HTML easy to learn for beginners?Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

What is an example of a starting tag in HTML?What is an example of a starting tag in HTML?Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

How to use CSS's Flexbox layout to achieve centering alignment of dotted line segmentation effect in menu?How to use CSS's Flexbox layout to achieve centering alignment of dotted line segmentation effect in menu?Apr 05, 2025 pm 01:24 PM

How to design the dotted line segmentation effect in the menu? When designing menus, it is usually not difficult to align left and right between the dish name and price, but how about the dotted line or point in the middle...

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

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version