HTML Styles

You can change the look of an HTML element with the style attribute. This is called inline CSS. The value is made of CSS property and value pairs.

<tagname style="property: value;">

Text colour and background

<h2 style="color: blue;">Blue heading</h2>
<p style="background-color: lightyellow;">Paragraph with a light yellow background.</p>
Output in the browser

Blue heading

Paragraph with a light yellow background.


Font family and size

<p style="font-family: Verdana, sans-serif; font-size: 20px;">This text is in Verdana, 20 pixels.</p>
Output in the browser

This text is in Verdana, 20 pixels.


Text alignment

<p style="text-align: center;">Centered text</p>
<p style="text-align: right;">Right aligned text</p>
Output in the browser

Centered text

Right aligned text


Border

<p style="border: 2px solid green; padding: 10px;">Paragraph with a green border.</p>
Output in the browser

Paragraph with a green border.


Several styles together

Separate the styles with a semicolon.

<p style="color: white; background-color: darkred; padding: 10px;">White text on dark red.</p>

Old styling tags are removed

In old HTML you might see tags and attributes like <font color="red">, <center> and bgcolor. They are not valid in HTML5. Use CSS instead.

Note: Inline styles are fine for learning and quick tests. In real websites put the CSS in a separate .css file or in a <style> tag, and use classes.

Summary

  • Use the style attribute with CSS properties to change the look.
  • Separate several styles with semicolons.
  • Do not use the old font and center tags.
  • Next: HTML Formatting.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
Program to see difference between paragraphs & normal text with line break
Write a program to create a webpage to print values 1 to 5
Write a program to create a webpage to print your city name in red color.
Write a program to print a paragraph with different font and color.
Write an HTML program to display all six heading levels
Write an HTML program for text formatting (bold, italic, subscript, superscript)
Interview Questions & Answers
How to add comments in HTML?
How to add the copyright symbol to a web page?
What is the difference between strong and b tag in HTML?
What are HTML entities?
What is the difference between inline, internal and external CSS?