CSS Text Formatting

CSS Text Formatting refers to applying styles to text elements to control appearance and layout. This includes properties for color, alignment, decoration, indentation, justification, shadows, spacing, and direction. These properties enhance readability and aesthetics, improving the presentation of textual content on web pages.

Syntax:

The Syntax to write this property:

Selector {
    property-name : /*value*/
}

CSS Text Formatting Properties: 

These are the following text formatting properties.

PropertyDescription
text-colorSets the color of the text using color name, hex value, or RGB value.
text-alignSpecifies horizontal alignment of text in a block or table-cell element.
text-align-lastSets alignment of last lines occurring in an element.
text-decorationDecorates text content.
text-decoration-colorSets color of text decorations like overlines, underlines, and line-throughs.
text-decoration-lineSets various text decorations like underline, overline, line-through.
text-decoration-styleCombines text-decoration-line and text-decoration-color properties.
text-indentIndents first line of paragraph.
text-justifyJustifies text by spreading words into complete lines.
text-overflowSpecifies hidden overflow text.
text-transformControls capitalization of text.
text-shadowAdds shadow to text.
letter-spacingSpecifies space between characters of text.
line-heightSets space between lines.
directionSets text direction.
word-spacingSpecifies space between words of line.

Examples of CSS Text Formatting

Example: In this example we demonstrates basic text formatting using CSS. It includes paragraphs with different styles: changing color, aligning center, adding underline, setting indentation, and adjusting letter spacing for improved readability and aesthetics.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0">
    <title>Basic Text Formatting</title>
    <style>
        .text-color {
            color: blue;
        }

        .text-align-center {
            text-align: center;
        }

        .text-decoration {
            text-decoration: underline;
        }

        .text-indent {
            text-indent: 20px;
        }

        .letter-spacing {
            letter-spacing: 2px;
        }
    </style>
</head>

<body>
    <p class="text-color">Changing Text Color</p>
    <p class="text-align-center">Aligning Text</p>
    <p class="text-decoration">Adding Text Decoration</p>
    <p class="text-indent">Setting Text Indentation</p>
    <p class="letter-spacing">Adjusting Letter Spacing</p>
</body>

</html>

Output:

CSS Text Formatting Example Output

Example: In this example we are showing advanced text formatting using CSS. It applies styles such as adjusting line height, adding text shadow, transforming text to uppercase, setting word spacing, and specifying text direction for diverse visual effects.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0">
    <title>Advanced Text Formatting</title>
    <style>
        .line-height {
            line-height: 1.5;
        }

        .text-shadow {
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
        }

        .text-transform {
            text-transform: uppercase;
        }

        .word-spacing {
            word-spacing: 5px;
        }

        .text-direction {
            direction: rtl;
        }
    </style>
</head>

<body>
    <p class="line-height">Changing Line Height</p>
    <p class="text-shadow">Applying Text Shadow</p>
    <p class="text-transform">Controlling Text Transformation</p>
    <p class="word-spacing">Setting Word Spacing</p>
    <p class="text-direction">Specifying Text Direction</p>
</body>

</html>

Output:

CSS Text Formatting Example Output

Supported Browser:



Contact Us