CSS 文本

130 阅读2分钟

文本颜色

对于 W3C compliant CSS:如果您定义了 color 属性,则还必须定义 background-color 属性。

body {
  background-color: lightgrey;
  color: blue;
}

文本对齐

text-align 属性用于设置文本的水平对齐方式。

文本可以左对齐或右对齐,或居中对齐。

下例展示了居中对齐以及左右对齐的文本(如果文本方向是从左到右,则默认为左对齐;如果文本方向是从右到左,则默认是右对齐):

center 居中
left 靠左
right 靠右
justify 拉伸 使每行宽度一样

文本方向

direction 和 unicode-bidi 属性可用于更改元素的文本方向:

p.ex1 {
  direction: rtl;
  unicode-bidi: bidi-override;
}
<p>This is the default text direction.</p>

<p class="ex1">This is right-to-left text direction.</p>

效果:

image.png

垂直对齐

vertical-align 属性设置元素的垂直对齐方式。

img.top {
  vertical-align: top;
}

img.middle {
  vertical-align: middle;
}

img.bottom {
  vertical-align: bottom;
}

<p>一幅 <img src="/i/logo/w3logo-3.png" alt="W3School" width="180" height="167"> 默认对齐方式的图像。</p><br>

<p>一幅 <img class="top" src="/i/logo/w3logo-3.png" alt="W3School" width="180" height="167"> 上对齐的图像。</p><br>

<p>一幅 <img class="middle" src="/i/logo/w3logo-3.png" alt="W3School" width="180" height="167"> 居中对齐的图像。</p><br>

<p>一幅 <img class="bottom" src="/i/logo/w3logo-3.png" alt="W3School" width="180" height="167"> 下对齐的图像。</p>

效果:

image.png

文字装饰

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

text-decoration: none; 通常用于从链接上删除下划线:

<style>
a {
  text-decoration: none;
}
</style>

<p>没有下划线的链接:<a href="https://www.w3school.com.cn">W3School.com.cn</a></p>

其他样式:

<style>
h1 {
  text-decoration: overline;
}

h2 {
  text-decoration: line-through;
}

h3 {
  text-decoration: underline;
}
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<h3>This is heading 3</h3>

效果

image.png

文本转换

text-transform 属性用于指定文本中的大写和小写字母。

它可用于将所有内容转换为大写或小写字母,或将每个单词的首字母大写:

<style>
p.uppercase {
  text-transform: uppercase;
}

p.lowercase {
  text-transform: lowercase;
}

p.capitalize {
  text-transform: capitalize;
}
</style>

<p class="uppercase">This is some text.</p>
<p class="lowercase">This is some text.</p>
<p class="capitalize">This is some text.</p>

效果:

image.png

文字间距

文字缩进

<style>
p {
  text-indent: 50px;
}
</style>

<p>In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.'</p>
image.png

字母间距

letter-spacing 属性用于指定文本中字符之间的间距。

下例演示如何增加或减少字符之间的间距:

<style>
h1 {
  letter-spacing: 3px;
}

h2 {
  letter-spacing: -3px;
}
</style>

<h1>This is heading 1</h1>
<h2>This is heading 2</h2>

效果;

image.png

行高

line-height 属性用于指定行之间的间距:

p.small {
  line-height: 0.7;
}

p.big {
  line-height: 1.8;
}
</style>

<p class="small">
这是行高更小的段落。<br>
这是行高更小的段落。<br>
</p>

<p class="big">
这是行高更大的段落。<br>
这是行高更大的段落。<br>
</p>

效果:
image.png

字间距

word-spacing 属性用于指定文本中单词之间的间距。

下例演示如何增加或减少单词之间的间距:

<style>
h1 {
  word-spacing: 10px;
}

h2 {
  word-spacing: -5px;
}
</style>
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>

效果:

image.png

空白

white-space 属性指定元素内部空白的处理方式。
此例演示如何禁用元素内的文本换行:

p {
  white-space: nowrap;
}

文本阴影

text-shadow 属性为文本添加阴影。 最简单的用法是只指定水平阴影(2px)和垂直阴影(2px):

h1 {
  //水平阴影(2px)和垂直阴影(2px),模糊效果(5px)阴影颜色(red)
  text-shadow: 2px 2px 5px red;
}

效果:

image.png