Typography is more than just font choices. The colors you apply to text, the way you style it with different weights and decorations, and the background colors they appear against, all play a crucial role in readability, hierarchy, and the overall aesthetic of your web pages.
The most fundamental property for text color is color
. It sets the foreground color of an element's text content. You can specify colors using various formats:
#RRGGBB
or #RGB
(e.g., #007bff
for a shade of blue, #f00
for red).rgb(red, green, blue)
or rgba(red, green, blue, alpha)
(e.g., rgb(0, 123, 255)
, rgba(0, 0, 0, 0.5)
for semi-transparent black).hsl(hue, saturation, lightness)
or hsla(hue, saturation, lightness, alpha)
(e.g., hsl(210, 100%, 50%)
).red
, blue
, green
, black
, white
, etc.Let's look at some common color applications:
The background-color
property sets the background color of an element. This is essential for creating contrast and highlighting text or sections. When applied to text, it can create distinct visual blocks or highlights.
Beyond color, controlling the visual weight and style of text is crucial for creating emphasis and structure:
font-weight
: Controls the boldness of text. Common values include normal
, bold
, and numeric values like 100
to 900
.font-style
: Controls the italicization of text. Values include normal
, italic
, and oblique
.text-decoration
: Adds decorations like underline
, overline
, line-through
, or none
. It's commonly used for links but can be applied to any text for stylistic purposes.Here's how you can use these properties:
This is normal text. This is bold text, and this is italic text. A link often has an underline, like this example link. Sometimes we need to strike through text, like this deleted item.
The effective use of color and style properties allows you to guide the user's eye, convey meaning, and enhance the overall user experience. Experiment with different combinations to achieve the desired look and feel for your content.
Consider these CSS snippets:
/* Styling for important headings */
h2 {
color: var(--primary-color);
font-weight: bold;
border-bottom: 2px solid var(--primary-color);
padding-bottom: 0.3em;
}
/* Styling for error messages */
.error-message {
color: var(--danger-color);
font-weight: bold;
background-color: var(--light-color);
padding: var(--spacing-unit);
border: 1px dashed var(--danger-color);
border-radius: 5px;
}
/* Styling for code blocks */
code {
background-color: var(--code-background);
color: #c7254e; /* A common code color */
padding: 0.2em 0.4em;
border-radius: 3px;
}