Skip to main content

HTML Basics

HTML Styles

Written by Updated HTML Styles exercises

HTML decides what is on the page: a heading, a paragraph, a button. Styles decide how it looks: the color, the font, the spacing. You add styles with CSS, and there are three places to put it.

What Are HTML Styles?

HTML gives a page its structure, and styles give it its appearance. The quickest way to see this is the style attribute:

HTML Style Example

html

<p style="color: blue; font-size: 18px;">This is a styled paragraph.</p>

Inside the quotes is ordinary CSS: color sets the text color and font-size sets its size. A semicolon separates one property from the next.

Types of Styling in HTML

a) Inline Styles

Written straight on one element with the style attribute. It is quick for testing, but you have to repeat it on every element.

Inline Style Example

html

<h1 style="color: #29AB87; text-align: center;">Welcome!</h1>

b) Internal Styles

Written inside a <style> element in the <head>. The rules apply to the whole page, but only that page.

Internal Style Example

html

<head>
  <style>
    p {
      color: red;
      font-size: 16px;
    }
  </style>
</head>
<body>
  <p>This paragraph is styled using internal CSS.</p>
</body>

c) External Styles

Written in a separate .css file and connected with a <link> tag in the <head>. One file can style the whole website.

External Style Example

html

<head>
  <link rel="stylesheet" href="styles.css">
</head>

styles.css:

Style Example

css

body {
  background-color: #f0f0f0;
}
h1 {
  color: #29AB87;
}

Which Way of Styling HTML Should You Use?

MethodWhere it goesBest for
InlineThe style attributeOne small exception, HTML email
InternalA <style> element in the headA single page or a demo
ExternalA .css file joined with <link>Almost every website

Try it: which style wins?

The same paragraph is styled in two places. The inline style wins over a normal rule in the style element. Remove the style attribute and run it again.

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Example</title>
  <style>
    p { color: green; }
  </style>
</head>
<body>
  <p style="color: crimson;">Which color am I?</p>
  <p>I only have the internal style.</p>
</body>
</html>

The style attribute and the style element

They look alike but do different jobs. The style attribute affects one element and takes only properties: style="color: red;". The style element holds full rules with selectors.

Common Style Properties

PropertyDescriptionExample
colorText colorcolor: blue;
background-colorBackground colorbackground-color: yellow;
font-sizeText sizefont-size: 18px;
font-familyFont stylefont-family: Arial, sans-serif;
text-alignText alignmenttext-align: center;
marginSpace outside elementmargin: 10px;
paddingSpace inside elementpadding: 5px;

Example: Combining Styles

Combined Style Example

html

<p style="color: white; background-color: #29AB87; font-size: 18px; padding: 10px; text-align: center;">
Styled paragraph with multiple properties.
</p>

That one paragraph gets white text, a green background, a bigger font, padding and centered text, all from one style attribute. It works, but imagine typing that on fifty paragraphs.

Use a class instead of repeating yourself

Give the elements a class, and write the style once:

Example HTML

html

<p class="highlight">This is highlighted text.</p>

Example CSS

css

.highlight {
  color: #29AB87;
  font-weight: bold;
}

Change the color in one place and every highlighted paragraph changes with it. That is the main reason real websites keep inline styles to a minimum.

Common HTML Styling Mistakes

  • Leaving out the semicolon between two properties: color: red font-size: 20px.
  • Writing font-color. The property is color.
  • Forgetting the unit. Write 20px, not 20.
  • Using old attributes such as bgcolor and align. Use CSS instead.

Next: How to Add CSS, then the full CSS tutorial. Practice with the HTML styles exercises.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

HTML Styles exercises

Style one paragraph

Write the paragraph Alert and make its text red with an inline style.

Result

Check your answer to see it here.