CSS3 Text Effects


CSS3 text effects

Several new text features are included in CSS3.

In this chapter you will learn about the following text properties:

  • text-shadow

  • box-shadow

  • text-overflow

  • word-wrap

  • word-break


Browser support

Propertyimage.pngimage.pngimage.pngimage.pngimage.png
text-shadow4.010.03.54.09.5
box-shadow10.0
4.0 -webkit-
9.04.0
3.5 -moz-
5.1
3.1 -webkit-
10.5
text-overflow4.06.07.03.111.0
9.0 -o-
word-wrap23.05.53.56.112.1
word-break4.05.515.03.115.0

CSS3 text shadow

In CSS3, the text-shadow property applies to text shadows.

Shadow effect!

You specify the horizontal shadow, vertical shadow, blur distance, and shadow color:

OperaSafariChromeFirefoxInternet Explorer

Example

Add shadow to title:

h1
{
    text-shadow: 5px 5px 5px #FF0000;
}

 Try It! 


CSS3 box-shadow property

The CSS3 box-shadow property in CSS3 applies to box shadows

Example

div {
    box-shadow: 10px 10px 5px #888888;
}

Try It! 


Next, add color to the shadow

Example

div {
    box-shadow: 10px 10px grey;
}

Try It! 


Next, add a blur effect to the shadow

Example

div {
    box-shadow: 10px 10px 5px grey;
}

Try It! 


You can also add shadow effects to the ::before and ::after pseudo-elements

Example

#boxshadow {
    position: relative;
    b ox-shadow: 1px 2px 4px rgba(0, 0, 0, .5);
    pa dding: 10px;
    bac kground: 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;
}

Try It! 


A special case of using 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;
}

 Word Cards       Image Cards


CSS3 Text Overflow property

The CSS3 text-overflow property specifies how overflowing content should be displayed 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; 
}

 Try It! 


CSS3 line breaks

If a word is too long to fit inside a region, it expands outside:

In CSS3, the word-wrap property allows you to force text to wrap - even if that means splitting it a word in the middle:

The CSS code is as follows:

OperaSafariChromeFirefoxInternet Explorer

Example

Allow long text to wrap:

p {word-wrap:break-word;}

Try It! 


CSS3 word split line break

The CSS3 word-splitting newline property specifies the newline rule:

The CSS code is as follows:

Example

p.test1 {
    word-break: keep-all;
}
 
p.test2 {
    word-break: break-all;
}

Try It! 


New text properties

PropertyDescriptionCSS
hanging-punctuationSpecifies whether punctuation characters are outside the wireframe.3
punctuation-trimSpecifies whether to trim punctuation characters.3
text-align-lastSets how to align the last line or the line immediately before the forced newline.3
text-emphasisApplies an accent marker to the element's text along with the accent marker's foreground color.3
text-justifySpecifies the alignment method to use when text-align is set to "justify".3
text-outlineSpecifies the outline of the text.3
text-overflowSpecifies what happens when text overflows the containing element.3
text-shadowAdd shadow to text.3
text-wrapSpecifies the newline rules for text.3
word-breakSpecifies the newline rule for non-CJK texts.3
word-wrapAllows long inseparable words to be split and wrapped to the next line.3