菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻

CSS Text(文本)

原创
05/13 14:22 更新

CSS Text 文本格式

通过 CSS 的 Text 属性,你可以改变页面中文本的颜色、字符间距、对齐文本、装饰文本、对文本进行缩进等等,你可以观察下述的一段简单的应用了 CSS 文本格式的段落。

图片.png

Text Color

颜色属性被用来设置文字的颜色。

颜色是通过CSS最经常的指定:

  • 十六进制值 - 如"#FF0000"
  • 一个RGB值 - “RGB(255,0,0)”
  • 颜色的名称 - 如"红"

一个网页的背景颜色是指在主体内的选择:

实例

body {color:blue;}  
h1 {color:#00ff00;}  
h2 {color:rgb(255,0,0);}

对于 W3C 标准的 CSS:如果你定义了颜色属性,你还必须定义背景色属性。


文本的对齐方式

文本排列属性是用来设置文本的水平对齐方式。

文本可居中或对齐到左或右,两端对齐.

text-align 设置为justify,每一行被展开为宽度相等,左,右外边距是对齐(如杂志和报纸)。

实例

h1 {text-align:center;}  
p.date {text-align:right;}  
p.main {text-align:justify;}

提示: 如果想把一个行内元素的第一行“缩进”,可以用左内边距或外边距创造这种效果。


文本修饰

text-decoration属性用来设置或删除文本的装饰。

从设计的角度看 text-decoration 属性主要是用来删除链接的下划线:

实例

a {text-decoration:none;}

也可以这样装饰文字:

实例

h1 {text-decoration:overline;}  
h2 {text-decoration:line-through;}  
h3 {text-decoration:underline;}

我们不建议强调指出不是链接的文本,因为这常常混淆用户。


文本转换

文本转换属性是用来指定在一个文本中的大写和小写字母。

可用于所有字句变成大写或小写字母,或每个单词的首字母大写。

实例

p.uppercase {text-transform:uppercase;}  
p.lowercase {text-transform:lowercase;}  
p.capitalize {text-transform:capitalize;}

文本缩进

文本缩进属性是用来指定文本的第一行的缩进。

CSS 提供了 text-indent 属性,该属性可以方便地实现文本缩进。

通过使用 text-indent 属性,所有元素的第一行都可以缩进一个给定的长度。

实例

p {text-indent:50px;}

文本间隔

word-spacing 属性可以改变字(单词)之间的标准间隔。其默认值 normal 与设置值为 0 是一样的。

实例

指定段字之间的空间,应该是30像素:

p  
{  
word-spacing:30px;  
}

更多实例

指定字符之间的空间]
这个例子演示了如何增加或减少字符之间的空间。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>学习猿地</title> 
<style>
    h1 {letter-spacing:2px;}
    h2 {letter-spacing:-3px;}
</style>
</head>

<body>
    <h1>这是标题 1</h1>
    <h2>这是标题 2</h2>
</body>
</html>

指定行与行之间的空间
这个例子演示了如何指定在一个段落中行之间的空间

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>学习猿地</title> 
<style>
    p.small {line-height:70%;}
    p.big {line-height:200%;}
</style>
</head>

<body>
    <p>
    This is a paragraph with a standard line-height.<br>
    This is a paragraph with a standard line-height.<br>
    The default line height in most browsers is about 110% to 120%.<br>
    </p>
    
    <p class="small">
    This is a paragraph with a smaller line-height.<br>
    This is a paragraph with a smaller line-height.<br>
    This is a paragraph with a smaller line-height.<br>
    This is a paragraph with a smaller line-height.<br>
    </p>
    
    <p class="big">
    This is a paragraph with a bigger line-height.<br>
    This is a paragraph with a bigger line-height.<br>
    This is a paragraph with a bigger line-height.<br>
    This is a paragraph with a bigger line-height.<br>
    </p>

</body>
</html>

设置元素的文本方向
这个例子演示了如何改变元素的文本方向。

<html>
<head>
<meta charset="utf-8"> 
<title>学习猿地</title> 
<style type="text/css">
    div.ex1 {direction:rtl;}
</style>
</head>
<body>
    
    <div>一些文本。 默认书写方向</div>
    <div class="ex1">一些文本。从右到左的书写方向。</div>

</body>
</html>

增加单词之间的空白空间
这个例子演示了如何增加一个段落中的单词之间的空白空间。

<html>
<head>
<meta charset="utf-8"> 
<title>学习猿地</title> 
<style type="text/css">
    p
    { 
        word-spacing:30px;
    }
    h4{
        word-spacing: -0.5em;
    }
</style>
</head>
<body>

    <p>
    This is some text. This is some text.
    </p>
    <h4>
    This is some text. This is some text.
    </h4>
</body>
</html>

在元素内禁用文字环绕
这个例子演示了如何禁用一个元素内的文字环绕。

<html>
<head>
<meta charset="utf-8"> 
<title>学习猿地</title> 
<style type="text/css">
    p
    {
    white-space:nowrap;
    }
</style>
</head>
<body>

    <p>
    这是一些文本。这是一些文本。这是一些文本。这是一些文本。这是一些文本。
    这是一些文本。这是一些文本。这是一些文本。这是一些文本。这是一些文本。
    这是一些文本。这是一些文本。这是一些文本。这是一些文本。这是一些文本。
    这是一些文本。这是一些文本。这是一些文本。这是一些文本。这是一些文本。
    这是一些文本。这是一些文本。这是一些文本。这是一些文本。这是一些文本。
    </p>

</body>
</html>

垂直对齐图像
这个例子演示了如何设置文本的垂直对齐图像。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>学习猿地</title> 
<style>
    img.top {vertical-align:text-top;}
    img.bottom {vertical-align:text-bottom;}
</style>
</head>

<body>
    <p>一个 <img src="/_nuxt/img/logo.2a324bb.png" alt="lmonkey" width="121" height="74" /> 具有默认对齐方式的图像.</p> 
    <p>一个 <img class="top" src="/_nuxt/img/logo.2a324bb.png" alt="lmonkey" width="121" height="75" /> 文本顶部对齐的图像.</p> 
    <p>一个 <img class="bottom" src="/_nuxt/img/logo.2a324bb.png" alt="lmonkey" width="121" height="75" /> 图像与文本底部对齐.</p>
</body>
</html>

添加文本阴影
这个例子演示了如何设置文本阴影。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>学习猿地</title> 
<style>
h1 {text-shadow:2px 2px #FF0000;}
</style>
</head>
<body>

<h1>Text-shadow effect</h1>

<p><b>Note:</b> Internet Explorer 9 and earlier do not support the text-shadow property.</p>

</body>
</html>

所有CSS文本属性。

属性 描述
color 设置文本颜色
direction 设置文本方向。
letter-spacing 设置字符间距
line-height 设置行高
text-align 对齐元素中的文本
text-decoration 向文本添加修饰
text-indent 缩进元素中文本的首行
text-shadow 设置文本阴影
text-transform 控制元素中的字母
unicode-bidi 设置或返回文本是否被重写
vertical-align 设置元素的垂直对齐
white-space 设置元素中空白的处理方式
word-spacing 设置字间距
综合评分:9.9 评分 请对本文进行纠错,及学习过程中有困难疑惑可在此进行讨论