CSS3 Text Effects
CSS3 Text Effects
CSS3 includes several new text features.
In this chapter, you will learn about the following text properties:
- text-shadow
- box-shadow
- text-overflow
- word-wrap
- word-break
Browser Support
Property | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
text-shadow | 4.0 | 10.0 | 3.5 | 4.0 | 9.5 |
box-shadow | 10.0 <br>4.0 -webkit- | 9.0 | 4.0 <br>3.5 -moz- | 5.1 <br>3.1 -webkit- | 10.5 |
text-overflow | 4.0 | 6.0 | 7.0 | 3.1 | 11.0 <br>9.0 -o- |
word-wrap | 23.0 | 5.5 | 3.5 | 6.1 | 12.1 |
word-break | 4.0 | 5.5 | 15.0 | 3.1 | 15.0 |
CSS3 Text Shadow
In CSS3, the text-shadow
property applies shadow to text.
You specify the horizontal shadow, vertical shadow, blur distance, and shadow color:
Example
Add shadow to the heading:
h1 {
text-shadow: 5px 5px 5px #FF0000;
}
CSS3 Box-Shadow Property
In CSS3, the box-shadow
property applies shadow to boxes.
Example
div {
box-shadow: 10px 10px 5px #888888;
}
Adding Color to the Shadow
Example
div {
box-shadow: 10px 10px grey;
}
Adding a Blur Effect to the Shadow
Example
div {
box-shadow: 10px 10px 5px grey;
}
You Can Also Add Shadow Effects in ::before
and ::after
Pseudo-Elements
Example
#boxshadow {
position: relative;
box-shadow: 1px 2px 4px rgba(0, 0, 0, .5);
padding: 10px;
background: white;
}
#boxshadow img {
width: 100%;
border: 1px solid #8a4419;
border-style: inset;
}
#boxshadow::after {
content: '';
position: absolute;
z-index: -1; /* hide shadow behind image */
box-shadow: 0 15px 20px rgba(0, 0, 0, 0.3);
width: 70%;
left: 15%; /* one half of the remaining 30% */
height: 100px;
bottom: 0;
}
A Special Use Case for Shadows is the Card Effect
Example
div.card {
width: 250px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
text-align: center;
}
CSS3 Text Overflow Property
The CSS3 text overflow property specifies how overflowed content that is not displayed should be signaled to the user.
Example
p.test1 {
white-space: nowrap;
width: 200px;
border: 1px solid #000000;
overflow: hidden;
text-overflow: clip;
}
p.test2 {
white-space: nowrap;
width: 200px;
border: 1px solid #000000;
overflow: hidden;
text-overflow: ellipsis;
}
CSS3 Word Wrapping
If a word is too long, it extends outside of the area:
In CSS3, the word wrap property allows you to force text to wrap—even if that means splitting it in the middle of a word:
Example
Allow long text to wrap:
p {
word-wrap: break-word;
}
CSS3 Word Break and Line Wrapping
The CSS3 word break and line wrapping properties specify the rules for line breaks:
CSS code is as follows:
Example
p.test1 {
word-break: keep-all;
}
p.test2 {
word-break: break-all;
}
New Text Properties
Property | Description | CSS |
---|---|---|
hanging-punctuation | Specifies whether punctuation characters may be placed outside the line box. | 3 |
punctuation-trim | Specifies whether punctuation characters should be trimmed. | 3 |
text-align-last | Sets how the last line or the line before a forced line break is aligned. | 3 |
text-emphasis | Applies emphasis marks to text elements and specifies the color of the emphasis marks. | 3 |
text-justify | Specifies the justification method used when text-align is set to "justify". | 3 |
text-outline | Defines the outline of the text. | 3 |
text-overflow | Specifies what happens when text overflows the containing element. | 3 |
text-shadow | Adds shadow to text. | 3 |
text-wrap | Specifies line breaking rules for text. | 3 |
word-break | Specifies line breaking rules for non-CJK scripts. | 3 |
word-wrap | Allows long, unbreakable words to be broken and wrapped onto the next line. | 3 |