Open In App

CSS Text Effects

Last Updated : 30 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

CSS (Cascading Style Sheets) is the mechanism used to add style to web documents. Through CSS, we can apply various effects on text, enhancing its appearance and improving user experience on web pages.

In this article, we’ll cover several key CSS properties that can be used to apply text effects, including:

  1. text-overflow
  2. word-wrap
  3. word-break
  4. writing-mode

Let’s learn about each of these in detail:

1. Text-Overflow

The text-overflow property in CSS helps handle text that exceeds the width of its container. It defines how the overflowed content should be represented when the text doesn’t fit within its container.

Syntax:

element {
    text-overflow: clip | ellipsis;
    //CSS Property
}

Values:

  1. clip: This is the default value. It truncates the text at the edge of the content area, even if it cuts off characters in the middle.
  2. ellipsis: Replaces the clipped text with an ellipsis (…). The ellipsis is displayed inside the content area and reduces the amount of visible text.

Example 1: text-overflow: clip

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        div.geek {
            white-space: nowrap;
            width: 200px;
            overflow: hidden;
            border: 1px solid #000000;
            font-size: 20px;
            text-overflow: clip;
        }

        div.geek:hover {
            overflow: visible;
        }
    </style>
</head>

<body style="text-align: center">
    <h1 style="color:green">
        GeeksforGeeks
    </h1>

    <h2>
        text-overflow: clip
    </h2>

    <div class="geek">
        A Computer Science portal for geeks.
    </div>
</body>

</html>

Output:

textclip

Example 2: text-overflow: ellipsis

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        div.geek {
            white-space: nowrap;
            width: 200px;
            overflow: hidden;
            border: 1px solid #000000;
            font-size: 20px;
            text-overflow: ellipsis;
        }

        div.geek:hover {
            overflow: visible;
        }
    </style>
</head>

<body style="text-align: center">
    <h1 style="color:green">
        GeeksforGeeks
    </h1>

    <h2>
        text-overflow: ellipsis
    </h2>

    <div class="geek">
        A Computer Science portal for geeks.
    </div>
</body>

</html>

Output:

textellip

2. Word Wrap

The CSS word-wrap property defines whether the browser is allowed to line break within words when a word is too long to fit within its parent container. If a word is too long to fit within an area, it expands outside: Syntax:

element {
    word-wrap: break-word;
    //CSS Property
}

Example:

html
<!DOCTYPE html>
<html>

<head>
    <title>word wrap</title>
    <style>
        p {
            width: 11em;
            border: 1px solid #000000;
            text-align: left;
            font-size: 20px;
        }

        p.test {
            word-wrap: break-word;
        }
    </style>
</head>

<body style="text-align: center;">
    <h2>Without word-wrap</h2>

    <p>
        This paragraph contains a very long word:
        geeksforgeeksforgeeksforgeeksforgeeks
    </p>

    <h2>With word-wrap</h2>

    <p class="test">
        This paragraph contains a very long word: geeks
        forgeeksforgeeksforgeeksforgeeks
    </p>
</body>

</html>

Output:

wordwrap


3. Word Breaking

The word-break CSS property sets whether line breaks appear wherever the text would otherwise overflow its content box. It specifies line breaking rules.

Syntax:

element {
    word-break: keep-all | break-all;
    //CSS Property
}
  1. break-all: Breaks words at arbitrary points to prevent overflow.
  2. keep-all: Prevents breaking words unless there are explicit line breaks.

Example: word-break: break-all

HTML
<!DOCTYPE html>
<html>

<head>
    <title>word-break: break-all</title>
    <style>
        p.geek {
            width: 140px;
            border: 1px solid #000000;
            word-break: break-all;
            text-align: left;
            font-size: 20px;
        }
    </style>
</head>

<body style="text-align: center;">
    <h2>word-break: break-all</h2>

    <p class="geek">
        A computer science portal for geeks
    </p>
</body>

</html>

Output:

break-all

Example: word-break: keep-all

HTML
<!DOCTYPE html>
<html>

<head>
    <title>word-break: keep-all</title>
    <style>
        p.geek {
            width: 140px;
            border: 1px solid #000000;
            word-break: keep-all;
            text-align: left;
            font-size: 20px;
        }
    </style>
</head>

<body style="text-align: center;">
    <h2>word-break: keep-all</h2>

    <p class="geek">
        A computer science portal for geeks
    </p>
</body>

</html>

Output:

keep-up

4. Writing Mode

The writing-mode property defines whether text is displayed horizontally (default) or vertically.

Syntax:

The writing-mode property defines whether text is displayed horizontally (default) or vertically.element {
     writing-mode: horizontal-tb | vertical-rl;
    //CSS Property
}
  1. horizontal-tb: This is the default value of the property i.e text is read from left to right and top to bottom. The next horizontal line is positioned below the previous line.
  2. vertical-rl: In this property the text is read from right to left and top to bottom. The next vertical line is positioned to the left of the previous line.

Example: writing-mode: horizontal-tb

HTML
<!DOCTYPE html>
<html>

<head>
    <title>writing-mode: horizontal-tb</title>
    <style>
        p.geek {
            writing-mode: horizontal-tb;
            font-size: 18px;
        }
    </style>
</head>

<body style="text-align: center;">
    <h1>writing-mode: horizontal-tb</h1>

    <p class="geek">
        A computer science portal for geeks.
    </p>
</body>

</html>

Output:

horizontal-tb

Example: writing-mode: vertical-rl

HTML
<!DOCTYPE html>
<html>

<head>
    <title>writing-mode: vertical-rl</title>
    <style>
        span.test2 {
            writing-mode: vertical-rl;
            font-size: 18px;
        }
    </style>
</head>

<body style="text-align: center;">
    <h1>writing-mode: vertical-rl</h1>
    <p class="geek"></p>
    <p>
        computer science <span class="test2">portal </span>
        for geeks.
    </p>
</body>

</html>

Output:

vertical-rl

Next Article

Similar Reads