Skip to main content

The HTML Style Attribute

The style attribute adds styles to an element, such as color, font, and size. You set it on the element's start tag, giving it a CSS property and a CSS value (see the syntax below). You will learn more about CSS later in this tutorial.

Example

Paragraphs styled with color and size using the style attribute.

Prompt
A page with three paragraphs styled inline with the style attribute: one red ("I am Red"), one blue ("I am Blue"), and one large ("I am Big" using font-size).
Output: complete HTML page

Style syntax

The shape of the style attribute.

index.html
<tagname style="property:value;">

Background Color

The CSS background-color property sets the background color of an element. Put it on the <body> to color the whole page, or on a single element to color just that element.

Whole page

Setting the page background to powderblue.

Prompt
A page that sets the whole page background to powderblue using a style attribute on the body, containing an h1 heading and a paragraph.
Output: complete HTML page

Single elements

Different background colors on a heading and a paragraph.

Prompt
A page that sets background color on two individual elements: an h1 with a powderblue background and a paragraph with a tomato background.
Output: complete HTML page

Text Color

The CSS color property sets the text color of an element.

Example

A blue heading and a red paragraph.

Prompt
A page using the CSS color property in style attributes: a blue h1 heading and a red paragraph.
Output: complete HTML page

Fonts

The CSS font-family property sets the font used for an element.

Example

A heading in verdana and a paragraph in courier.

Prompt
A page using the CSS font-family property in style attributes: an h1 in verdana and a paragraph in courier.
Output: complete HTML page

Text Size

The CSS font-size property sets the text size of an element.

Example

A large heading and a slightly enlarged paragraph.

Prompt
A page using the CSS font-size property in style attributes: an h1 at 300% and a paragraph at 160%.
Output: complete HTML page

Text Alignment

The CSS text-align property sets the horizontal alignment of an element's text.

Example

A centered heading and paragraph.

Prompt
A page using the CSS text-align property in style attributes to center an h1 heading and a paragraph.
Output: complete HTML page

Chapter Summary

To recap: use the style attribute to style elements, background-color for background color, color for text color, font-family for fonts, font-size for text size, and text-align for text alignment.