Learn CSS3 to style your web pages

Know what is CSS, its uses and how to add? CSS Style for Text, Paragraphs, Tables, Links and Forms. Background image with CSS, Borders styles in CSS, Set margin and positions in CSS, etc.

CSS3 Text


CSS3 contained several extra features, which is added later on

  • text-overflow
  • word-wrap
  • word-break

There are following most commonly used property in CSS3

Values Description
text-align-last Used to align the last line of the text
text-emphasis Used to emphasis text and color
text-overflow used to determines how overflowed content that is not displayed is signaled to users
word-break Used to break the line based on word
word-wrap Used to break the line and wrap onto next line

Text-overflow

The text-overflow property determines how overflowed content that is not displayed is signaled to users. the sample example of text overflow is shown as follows −

See this example:

<html>
<head>

<style>
p.text1 {
white-space: nowrap;
width: 200px;
border: 1px solid #000000;
overflow: hidden;
text-overflow: clip;
}
p.text2 {
white-space: nowrap;
width: 200px;
border: 1px solid #000000;
overflow: hidden;
text-overflow: ellipsis;
}
</style>

</head>
<body>

<b>Original Text:</b>

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam libero tortor, convallis eu pulvinar at, pellentesque sit amet lectus. In gravida sapien id lorem cursus interdum. Cras congue libero sed sem lobortis, sit amet aliquet metus tempor. Aenean iaculis fringilla dictum. In et odio imperdiet, pellentesque leo id, pretium dui. </p>

<b>Text overflow:clip:</b>

<p class="text1">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam libero tortor, convallis eu pulvinar at, pellentesque sit amet lectus. In gravida sapien id lorem cursus interdum. Cras congue libero sed sem lobortis, sit amet aliquet metus tempor.</p>

<b>Text overflow:ellipsis</b>

<p class="text2">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam libero tortor, convallis eu pulvinar at, pellentesque sit amet lectus. In gravida sapien id lorem cursus interdum. Cras congue libero sed sem lobortis, sit amet aliquet metus tempor. Aenean iaculis fringilla dictum. In et odio imperdiet, pellentesque leo id, pretium dui. </p>

</body>
</html>

Output:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam libero tortor, convallis eu pulvinar at, pellentesque sit amet lectus. In gravida sapien id lorem cursus interdum. Cras congue libero sed sem lobortis, sit amet aliquet metus tempor. Aenean iaculis fringilla dictum. In et odio imperdiet, pellentesque leo id, pretium dui.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam libero tortor, convallis eu pulvinar at, pellentesque sit amet lectus. In gravida sapien id lorem cursus interdum. Cras congue libero sed sem lobortis, sit amet aliquet metus tempor. Aenean iaculis fringilla dictum. In et odio imperdiet, pellentesque leo id, pretium dui.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam libero tortor, convallis eu pulvinar at, pellentesque sit amet lectus. In gravida sapien id lorem cursus interdum. Cras congue libero sed sem lobortis, sit amet aliquet metus tempor. Aenean iaculis fringilla dictum. In et odio imperdiet, pellentesque leo id, pretium dui.

CSS3 Word Breaking

Used to break the line, following code shows the sample code of word breaking

See this example:

<html>
<head>

<style>
p.text1 {
width: 140px;
border: 1px solid #000000;
word-break: keep-all;
}
p.text2 {
width: 140px;
border: 1px solid #000000;
word-break: break-all;
}
</style>

</head>
<body>

<b>line break at hyphens:</b>
<p class="text1">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam libero tortor, convallis eu pulvinar at, pellentesque sit amet lectus. In gravida sapien id lorem cursus interdum. Cras congue libero sed sem lobortis, sit amet aliquet metus tempor. Aenean iaculis fringilla dictum. In et odio imperdiet, pellentesque leo id, pretium dui. </p>

<b>line break at any character</b>

<p class="text2">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam libero tortor, convallis eu pulvinar at, pellentesque sit amet lectus. In gravida sapien id lorem cursus interdum. Cras congue libero sed sem lobortis, sit amet aliquet metus tempor. Aenean iaculis fringilla dictum. In et odio imperdiet, pellentesque leo id, pretium dui. </p>

</body>
</html>

Output:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam libero tortor, convallis eu pulvinar at, pellentesque sit amet lectus. In gravida sapien id lorem cursus interdum.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam libero tortor, convallis eu pulvinar at, pellentesque sit amet lectus. In gravida sapien id lorem cursus interdum.

CSS word wrapping

Word wrapping is used to break the line and wrap onto next line.the following code will have sample syntax

p {
    word-wrap: break-word;
}