CSS换行标签
在网页开发中,经常会遇到需要控制文本换行的情况。CSS提供了多种方式来实现文本换行,本文将详细介绍CSS中的换行标签。
1. 使用white-space
属性
white-space
属性用于控制元素内文本的空白符处理方式,其中pre
值可以保留换行符和空格。下面是一个示例代码:
<!DOCTYPE html>
<html>
<head>
<style>
.pre {
white-space: pre;
}
</style>
</head>
<body>
<div class="pre">geek-docs.com
geek-docs.com</div>
</body>
</html>
Output:
在上面的示例中,white-space: pre;
保留了换行符,文本在geek-docs.com
后换行显示。
2. 使用word-wrap
属性
word-wrap
属性用于指定是否允许单词内换行。当设置为break-word
时,单词内也可以换行。示例代码如下:
<!DOCTYPE html>
<html>
<head>
<style>
.break-word {
word-wrap: break-word;
}
</style>
</head>
<body>
<div class="break-word">geek-docs.com geek-docs.com</div>
</body>
</html>
Output:
在上面的示例中,word-wrap: break-word;
允许单词内换行,文本在geek-docs.com
后换行显示。
3. 使用word-break
属性
word-break
属性用于指定单词如何被分割换行。当设置为break-all
时,单词可以被分割换行。示例代码如下:
<!DOCTYPE html>
<html>
<head>
<style>
.break-all {
word-break: break-all;
}
</style>
</head>
<body>
<div class="break-all">geek-docs.com geek-docs.com</div>
</body>
</html>
Output:
在上面的示例中,word-break: break-all;
允许单词被分割换行,文本在geek-docs.com
后换行显示。
4. 使用overflow-wrap
属性
overflow-wrap
属性用于指定是否允许单词内换行。当设置为break-word
时,单词内也可以换行。示例代码如下:
<!DOCTYPE html>
<html>
<head>
<style>
.break-word {
overflow-wrap: break-word;
}
</style>
</head>
<body>
<div class="break-word">geek-docs.com geek-docs.com</div>
</body>
</html>
Output:
在上面的示例中,overflow-wrap: break-word;
允许单词内换行,文本在geek-docs.com
后换行显示。
5. 使用text-wrap
属性
text-wrap
属性用于指定文本如何被换行。当设置为balance
时,文本会在单词之间换行。示例代码如下:
<!DOCTYPE html>
<html>
<head>
<style>
.balance {
text-wrap: balance;
}
</style>
</head>
<body>
<div class="balance">geek-docs.com geek-docs.com</div>
</body>
</html>
Output:
在上面的示例中,text-wrap: balance;
使文本在单词之间换行,文本在geek-docs.com
后换行显示。
6. 使用word-spacing
属性
word-spacing
属性用于指定单词之间的间距。示例代码如下:
<!DOCTYPE html>
<html>
<head>
<style>
.spacing {
word-spacing: 10px;
}
</style>
</head>
<body>
<div class="spacing">geek-docs.com geek-docs.com</div>
</body>
</html>
Output:
在上面的示例中,word-spacing: 10px;
设置单词之间的间距为10px,文本在geek-docs.com
后换行显示。
7. 使用line-break
属性
line-break
属性用于指定如何处理换行符。当设置为strict
时,换行符会被保留。示例代码如下:
<!DOCTYPE html>
<html>
<head>
<style>
.strict {
line-break: strict;
}
</style>
</head>
<body>
<div class="strict">geek-docs.com
geek-docs.com</div>
</body>
</html>
Output:
在上面的示例中,line-break: strict;
保留了换行符,文本在geek-docs.com
后换行显示。
8. 使用text-align
属性
text-align
属性用于指定文本的对齐方式。示例代码如下:
<!DOCTYPE html>
<html>
<head>
<style>
.align {
text-align: center;
}
</style>
</head>
<body>
<div class="align">geek-docs.com geek-docs.com</div>
</body>
</html>
Output:
在上面的示例中,text-align: center;
设置文本居中对齐,文本在geek-docs.com
后换行显示。
9. 使用text-indent
属性
text-indent
属性用于指定文本的首行缩进。示例代码如下:
<!DOCTYPE html>
<html>
<head>
<style>
.indent {
text-indent: 20px;
}
</style>
</head>
<body>
<div class="indent">geek-docs.com geek-docs.com</div>
</body>
</html>
Output:
在上面的示例中,text-indent: 20px;
设置文本首行缩进为20px,文本在geek-docs.com
后换行显示。
10. 使用text-overflow
属性
text-overflow
属性用于指定文本溢出时的处理方式。示例代码如下:
<!DOCTYPE html>
<html>
<head>
<style>
.overflow {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
width: 100px;
}
</style>
</head>
<body>
<div class="overflow">geek-docs.com geek-docs.com geek-docs.com geek-docs.com geek-docs.com</div>
</body>
</html>
Output:
在上面的示例中,white-space: nowrap;
使文本不换行,overflow: hidden;
隐藏溢出部分,text-overflow: ellipsis;
在溢出时显示省略号,文本在geek-docs.com
后显示省略号。
11. 使用word-spacing
属性
word-spacing
属性用于指定单词之间的间距。示例代码如下:
<!DOCTYPE html>
<html>
<head>
<style>
.spacing {
word-spacing: 10px;
}
</style>
</head>
<body>
<div class="spacing">geek-docs.com geek-docs.com</div>
</body>
</html>
Output:
在上面的示例中,word-spacing: 10px;
设置单词之间的间距为10px,文本在geek-docs.com
后换行显示。
12. 使用text-transform
属性
text-transform
属性用于指定文本的大小写转换方式。示例代码如下:
<!DOCTYPE html>
<html>
<head>
<style>
.transform {
text-transform: uppercase;
}
</style>
</head>
<body>
<div class="transform">geek-docs.com geek-docs.com</div>
</body>
</html>
Output:
在上面的示例中,text-transform: uppercase;
将文本转换为大写,文本在geek-docs.com
后换行显示。
13. 使用letter-spacing
属性
letter-spacing
属性用于指定字符之间的间距。示例代码如下:
<!DOCTYPE html>
<html>
<head>
<style>
.spacing {
letter-spacing: 2px;
}
</style>
</head>
<body>
<div class="spacing">geek-docs.com geek-docs.com</div>
</body>
</html>
Output:
在上面的示例中,letter-spacing: 2px;
设置字符之间的间距为2px,文本在geek-docs.com
后换行显示。
14. 使用text-decoration
属性
text-decoration
属性用于指定文本的装饰效果。示例代码如下:
<!DOCTYPE html>
<html>
<head>
<style>
.decoration {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="decoration">geek-docs.com geek-docs.com</div>
</body>
</html>
Output:
在上面的示例中,text-decoration: underline;
给文本添加下划线效果,文本在geek-docs.com
后换行显示。
15. 使用text-shadow
属性
text-shadow
属性用于指定文本的阴影效果。示例代码如下:
<!DOCTYPE html>
<html>
<head>
<style>
.shadow {
text-shadow: 2px 2px 2px #000;
}
</style>
</head>
<body>
<div class="shadow">geek-docs.com geek-docs.com</div>
</body>
</html>
Output:
在上面的示例中,text-shadow: 2px 2px 2px #000;
给文本添加2px偏移的黑色阴影效果,文本在geek-docs.com
后换行显示。
16. 使用text-justify
属性
text-justify
属性用于指定文本的对齐方式。示例代码如下:
<!DOCTYPE html>
<html>
<head>
<style>
.justify {
text-align: justify;
}
</style>
</head>
<body>
<div class="justify">geek-docs.com geek-docs.com</div>
</body>
</html>
Output:
在上面的示例中,text-align: justify;
设置文本两端对齐,文本在geek-docs.com
后换行显示。
17. 使用text-orientation
属性
text-orientation
属性用于指定文本的方向。示例代码如下:
<!DOCTYPE html>
<html>
<head>
<style>
.orientation {
text-orientation: upright;
}
</style>
</head>
<body>
<div class="orientation">geek-docs.com geek-docs.com</div>
</body>
</html>
Output:
在上面的示例中,text-orientation: upright;
设置文本垂直显示,文本在geek-docs.com
后换行显示。
18. 使用text-underline-position
属性
text-underline-position
属性用于指定下划线的位置。示例代码如下:
<!DOCTYPE html>
<html>
<head>
<style>
.underline {
text-underline-position: under;
}
</style>
</head>
<body>
<div class="underline">geek-docs.com geek-docs.com</div>
</body>
</html>
Output:
在上面的示例中,text-underline-position: under;
设置下划线在文本下方,文本在geek-docs.com
后换行显示。
19. 使用text-align-last
属性
text-align-last
属性用于指定文本最后一行的对齐方式。示例代码如下:
<!DOCTYPE html>
<html>
<head>
<style>
.align-last {
text-align-last: center;
}
</style>
</head>
<body>
<div class="align-last">geek-docs.com geek-docs.com</div>
</body>
</html>
Output:
在上面的示例中,text-align-last: center;
设置文本最后一行居中对齐,文本在geek-docs.com
后换行显示。
20. 使用text-overflow-wrap
属性
text-overflow-wrap
属性用于指定文本溢出时的处理方式。示例代码如下:
<!DOCTYPE html>
<html>
<head>
<style>
.overflow-wrap {
white-space: nowrap;
overflow-wrap: break-word;
}
</style>
</head>
<body>
<div class="overflow-wrap">geek-docs.com geek-docs.com geek-docs.com geek-docs.com geek-docs.com</div>
</body>
</html>
Output:
在上面的示例中,white-space: nowrap;
使文本不换行,overflow-wrap: break-word;
允许单词内换行,文本在geek-docs.com
后换行显示。