Text styling
Text styling is a key aspect of web design that involves adjusting the appearance of a text to enhance readability, aesthetics, and overall user experience. CSS provides a variety of properties to style text, this includes
- color
- text-align
- text-decoration,
- transformation,
- line-height
- letter-spacing.
Text Color
Section titled “Text Color”The color property is used to set the color of the text. It can be specified using color names, hexadecimal values, RGB, RGBA, HSL, or HSLA values.
/* Using a color name */.text-example { color: red;}
/* Using a hexadecimal value */.text-example { color: #ff5733;}
/* Using RGB */.text-example { color: rgb(255, 87, 51);}
/* Using RGBA */.text-example { color: rgba(255, 87, 51, 0.8);}
/* Using HSL */.text-example { color: hsl(9, 100%, 60%);}
/* Using HSLA */.text-example { color: hsla(9, 100%, 60%, 0.8);}Text Alignment
Section titled “Text Alignment”The text-align property is used to set the horizontal alignment of a text within an element.
Allowing us to align text to
leftaligns the text to the left.rightaligns the text to the right.centercenters the text.justifystretches the lines so that each line has equal width, and the left and right edges are aligned.
.text-example { text-align: left;}
.text-example { text-align: right;}
.text-example { text-align: center;}
.text-example { text-align: justify;}Text Decoration
Section titled “Text Decoration”The text-decoration property is used to set the decoration of a text, such as underlines, overlines, line-throughs, and color of the decoration.
Values:
noneno decoration.underlineunderlines the text.overlinedraws a line above the text.line-throughdraws a line through the text.underline overlineunderlines and overlines the text.
Examples:
.text-example { text-decoration: underline;}
.text-example { text-decoration: overline;}
.text-example { text-decoration: line-through;}
.text-example { text-decoration: underline overline;}
/* Text-decoration color */.text-example { text-decoration: underline; text-decoration-color: red;}Text Transformation
Section titled “Text Transformation”The text-transform property is used to control the capitalization of text.
text-transform values can be any of the following,
noneno transformation.capitalizecapitalizes the first letter of each word.uppercaseconverts all characters to uppercase.lowercaseconverts all characters to lowercase.
Examples:
.text-example { text-transform: none;}.text-example { text-transform: capitalize;}
.text-example { text-transform: uppercase;}
.text-example { text-transform: lowercase;}Line Height
Section titled “Line Height”The line-height property is used to set the amount of space between lines of text.
It can take
- numeric values
- percentages(
%) - lengths (
px) normal
/* Numeric value (relative to the font size) */.text-example { line-height: 1.5; /* 1.5 times the font size */}
/* Length value */.text-example { line-height: 24px;}
/* Percentage */.text-example { line-height: 150%;}
/* Keyword */.text-example { line-height: normal;}Letter Spacing
Section titled “Letter Spacing”The letter-spacing property is used to control the amount of space between characters in a text.
/* Positive value */.text-example { letter-spacing: 2px;}
/* Negative value */.text-example { letter-spacing: -1px;}