Web Development
HTML Course
CSS Course
JavaScript Course
PHP Course
Python Course
SQL Course
SEO Course

Styling Text with CSS

With the basic elements already learned, we now focus on text styling, one of the most common tasks in CSS.

Basic Elements in Text and Font Styling

We already know that the text inside an element is displayed within the element's content box. Text starts at the top-left of the content area (or top-right in RTL layouts) and flows until it reaches the allocated space, then moves down to the next line, and continues until all the text fits inside the box.

Thus, text behaves like a series of inline elements, placed on adjacent lines that are only broken by using a line-break element – <br>.

The properties used to style text are divided into two main categories:

Remember that the text inside an element is treated as a single entity. You cannot select a portion of text unless that portion is wrapped in an element that allows it – such as span or strong, or via a pseudo-element like ::first-line (which selects the first line of an element), or another pseudo-element like ::selection, which allows highlighting text selected by the cursor.

Fonts

With the help of an example, we will see in action some of the properties applicable to fonts.

🔧 Example of Text Styling

Color

The color property defines the color of the content of the selected element (usually text).

  p {
    color: red;
  }

This rule will make the paragraph text red.

Font Families

To set a specific font, we use the font-family property. This allows specifying a font or a list of fonts, applied only if they are available on the visitor's computer. Otherwise, the browser will apply a default font.

  p {
    font-family: Arial;
  }

Web Safe Fonts

There is a limited number of fonts generally available across most systems, so these can be used safely.

The list of safe fonts is updated as operating systems evolve, and you can find it online.

CSS Font Stack maintains a list of web safe fonts.

Default Fonts

CSS defines five generic font names:

These font types are very generic, and the font generated when used depends greatly on the browser and operating system.

Font Stacks

Because the availability of a specific font is uncertain, it is recommended to use a comma-separated list of fonts from which the browser will choose the available one:

  p {
    font-family: "Trebuchet MS", Verdana, sans-serif;
  }

The list starts with the first targeted font, and if it is not available, the browser moves to the second font, then the third, and so on. The list ends with a generic font, so the browser will select any available sans-serif font on the system.

Font names that contain more than one word must be enclosed in quotes.

Font Size

The most common units used for text sizing are:

A good approach for text sizing is to set the document's font size (html) to 10px, making subsequent calculations simpler and grouping sizes easier to manage.

📐 Font-size with clamp()

Font Style

Used to enable or disable italic text, with the following values:

Font Weight

Sets how thick the text will appear.

Text-transform

Allows transforming text as follows:

Text Decorations – text-decoration

Enables or disables text decorations. It is often used to remove the default underline from links.

text-decoration can accept multiple values simultaneously and is a shorthand for text-decoration-line, text-decoration-style, and text-decoration-color. You can use combinations to create interesting effects.

Text with Shadow Effects – Text drop shadows

You can apply shadow effects to text using the text-shadow property:

text-shadow: 3px 4px 5px red;

The first value represents the horizontal offset (left/right) of the shadow relative to the text.

The second value represents the vertical offset (up/down).

The third value represents the blur radius. The color can be any valid CSS color.

Positive values move the shadow right and down, but negative values can also be used to move the shadow left and up, e.g., -1px -1px.

🌫️ Example of text-shadow and transformations

Text Layout

Text Alignment

The text-align property is used to control how text is aligned within its content box.

Line Height

This property defines the height of each line of text. Any unit can be used, or a unitless value acting as a multiplier, which is often the best choice. The font size is multiplied to obtain the line height. Typically, line height ranges between 1.5–2 (double spacing).

line-height: 1.5;

Letter and Word Spacing

These are used less frequently, mainly to improve readability of dense fonts, allowing you to set spacing between letters and/or words.

p::first-line {
  letter-spacing: 2px;
  word-spacing: 4px;
}

The properties studied so far are among the most commonly used, but there are many other properties you can occasionally use:

Font Styling:

Text Alignment:

📐 Example of alignment and line height

Font Shorthand

Many font properties can be written in a single line, in a specific order: font-style, font-variant, font-weight, font-stretch, font-size, line-height, and font-family.

Of all the properties above, only font-size and font-family are required when using the shorthand form.

The font-size and line-height properties must be separated by a slash (/).

font: italic normal bold normal 3em/1.5 Helvetica, Arial, sans-serif;

font: 400 15px/1.4 "Open Sans", sans-serif;

font: italic small-caps normal 13px/150% Arial, Helvetica, sans-serif;

Handling Text Direction

In recent years, CSS has evolved to better support different content directions, including right-to-left and top-to-bottom (as in Japanese writing).

These different directions are called writing modes.

A writing mode in CSS refers to the flow of text – horizontal or vertical. The writing-mode property allows us to change the writing mode.

You don't need to change writing mode only for a specific language. You can also use it creatively to create more interesting layouts for users.

h6 {
  writing-mode: vertical-rl;
}
VERTICAL

This is a short paragraph.

There are three possible values:

In practice, the writing-mode property sets the block's display direction, which affects text flow. Changing the writing mode also changes the display orientation from block to inline or vice versa.

🧭 Example of writing-mode and font shorthand

🧠 Final Quiz – CSS Text Styling

Top