CSS line break tags
CSS Line Break Tags
In web development, you often need to control text wrapping. CSS provides multiple ways to achieve text wrapping. This article will introduce the CSS line break tags in detail.
1. Using the white-space
Property
The white-space
property controls how whitespace is handled within an element. The pre
value preserves line breaks and spaces. Here is a sample code:
<!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:
In the example above, white-space: pre;
preserves line breaks, and the text wraps after geek-docs.com
.
2. Using the word-wrap
Property
The word-wrap
property specifies whether to allow line breaks within words. When set to break-word
, line breaks are also allowed within words. The sample code is as follows:
<!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:
In the example above, word-wrap: break-word;
allows word wrapping within a word, and the text wraps after geek-docs.com
.
3. Using the word-break
Property
word-break
property is used to specify how words are broken into lines. When set to break-all
, words can be broken into newlines. Example code is as follows:
<!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:
In the example above, word-break: break-all;
allows words to be broken into lines, and the text wraps after geek-docs.com
.
4. Using the overflow-wrap
Property
The overflow-wrap
property specifies whether to allow line breaks within words. When set to break-word
, line breaks are also allowed within words. The sample code is as follows:
<!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:
In the example above, overflow-wrap: The break-word property allows line breaks within words, and the text will wrap after <code>geek-docs.com
.
5. Using the text-wrap
Property
The text-wrap
property specifies how text is wrapped. When set to balance
, text wraps between words. The sample code is as follows:
<!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:
In the above example, text-wrap: The <code>balance;
property wraps the text between words. The text wraps after geek-docs.com
.
6. Using the word-spacing
Property
The word-spacing
property specifies the amount of space between words. The sample code is as follows:
<!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:
In the example above, word-spacing: 10px;
sets the spacing between words to 10px, and the text wraps after geek-docs.com
.
7. Using the line-break
Property
The line-break
property specifies how line breaks are handled. When set to strict
, line breaks are preserved. The sample code is as follows:
<!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:
In the above example, line-break:
preserves line breaks, and the text wraps after geek-docs.com
.
8. Using the text-align
Property
The text-align
property specifies the alignment of text. The sample code is as follows:
<!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:
In the above example, text-align: <code>center;
sets the text to center alignment, with the text wrapping after geek-docs.com
.
9. Using the text-indent
Property
The text-indent
property specifies the indentation of the first line of text. The sample code is as follows:
<!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:
In the example above, text-indent: 20px;
sets the first line of text to 20px, causing the text to wrap after geek-docs.com
.
10. Using the text-overflow
Property
The text-overflow
property specifies how to handle text overflow. The sample code is as follows:
<!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:
In the example above, white-space: nowrap;
prevents the text from wrapping, overflow: hidden;
hides the overflow, and text-overflow: ellipsis;
displays an ellipsis when the overflow occurs. The ellipsis appears after the text geek-docs.com
.
11. Using the word-spacing
Property
The word-spacing
property specifies the spacing between words. The sample code is as follows:
<!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:
In the example above, word-spacing: 10px;
sets the spacing between words to 10px, and the text wraps after geek-docs.com
.
12. Using the text-transform
Property
The text-transform
property specifies how text is case-inverted. The sample code is as follows:
<!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:
In the example above, text-transform: uppercase;
converts the text to uppercase, and the text wraps after geek-docs.com
.
13. Using the letter-spacing
Property
The letter-spacing
property specifies the amount of space between characters. The sample code is as follows:
<!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:
In the example above, letter-spacing: 2px;
sets the spacing between characters to 2px, and the text wraps after geek-docs.com
.
14. Using the text-decoration
Property
The text-decoration
property is used to specify decorative effects for text. The sample code is as follows:
<!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:
In the example above, text-decoration: underline;
adds an underline effect to the text, causing it to wrap after geek-docs.com
.
15. Using the text-shadow
Property
The text-shadow
property is used to specify a shadow effect for text. The sample code is as follows:
<!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:
In the example above, text-shadow: 2px 2px 2px #000;
adds a 2px offset black shadow to the text, causing the text to wrap after geek-docs.com
.
16. Using the text-justify
Property
The text-justify
property specifies the alignment of text. The sample code is as follows:
<!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:
In the above example, text-align: <code>justify;
sets the text justification, with the text wrapping after geek-docs.com
.
17. Using the text-orientation
Property
The text-orientation
property specifies the orientation of text. The sample code is as follows:
<!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:
In the example above, text-orientation: upright;
sets the text to display vertically, and the text wraps after geek-docs.com
.
18. Using the text-underline-position
Property
The text-underline-position
property is used to specify the position of the underline. The sample code is as follows:
<!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:
In the example above, text-underline-position: under;
sets the underline below the text, and the text wraps after geek-docs.com
.
19. Using the text-align-last
Property
The text-align-last
property specifies the alignment of the last line of text. The sample code is as follows:
<!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:
In the example above, text-align-last: center;
centers the last line of text, causing the text to wrap after geek-docs.com
.
20. Using the text-overflow-wrap
Property
The text-overflow-wrap
property specifies how to handle text overflow. The sample code is as follows:
<!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:
In the example above, white-space: nowrap;
prevents the text from wrapping, and overflow-wrap: break-word;
allows word wrapping. The text wraps after geek-docs.com
.