On this page11 sections
  1. Learning Objectives
  2. 1. How the style Attribute Works
  3. 2. Background Colour
  4. 3. Text Colour
  5. 4. Fonts and Text Size
  6. 5. Text Alignment
  7. 6. Several Properties at Once
  8. Common Beginner Mistakes
  9. Practice Questions
  10. Practice Answers
  11. Key Takeaways

Lecture 006: The style Attribute

Category2: Text & Styles
Lecture006 of 048
Estimated video length10 min
TargetHTML5 · any modern browser
LevelBeginner

Every HTML element can carry a style attribute that changes how it looks: its colour, background, font, size or alignment. It's the quickest way to see styling in action, and your first taste of CSS.


Learning Objectives

  • Write the style attribute with property: value; pairs
  • Change background colour, text colour, font, size and alignment
  • Know why bigger sites move styles out of the HTML

1. How the style Attribute Works

output
<tagname style="property: value; property: value;">

Each property is a thing to change, and each value is what to change it to. Separate pairs with a semicolon.

Example

html
<p style="color: purple;">This paragraph is purple.</p>

2. Background Colour

Example

html
<body style="background-color: #fff9db;">
  <h1 style="background-color: #ffd43b;">A yellow heading</h1>
  <p style="background-color: #d3f9d8;">A green paragraph.</p>
</body>

3. Text Colour

Example

html
<h2 style="color: crimson;">Red heading</h2>
<p style="color: #1971c2;">Blue paragraph.</p>

4. Fonts and Text Size

font-family chooses the typeface, and font-size sets how big the text is.

Example

html
<h2 style="font-family: Georgia, serif;">A Georgia heading</h2>
<p style="font-family: 'Courier New', monospace;">Typewriter-style text.</p>
<p style="font-size: 24px;">Big 24px text.</p>
<p style="font-size: 12px;">Small 12px text.</p>

5. Text Alignment

Example

html
<h2 style="text-align: center;">Centred heading</h2>
<p style="text-align: right;">Right-aligned paragraph.</p>

6. Several Properties at Once

Example: a simple notice box:

html
<p style="background-color: #e7f5ff; color: #1864ab; padding: 12px; border-left: 4px solid #1c7ed6; font-family: Arial, sans-serif;">
  The library is closed on Sunday.
</p>

This works, but it's long and hard to reuse. If ten notices on a page need the same look, you'd repeat that line ten times. Lecture 009 shows the better way: a CSS rule written once.


Common Beginner Mistakes

1. Using = instead of :. Write style="color: red;", not style="color=red".

2. Missing semicolons between pairs. color: red font-size: 20px breaks both. Separate them with ;.

3. Styling everything inline. Fine for a quick test, but painful to change later.

4. Old styling tags. <font> and <center> are outdated. Use style or CSS instead.


Practice Questions

  1. Which attribute changes how a single element looks?
  2. Write a paragraph with green text.
  3. Which property sets the background colour?
  4. How do you make a heading's text centred?
  5. Write a paragraph in Arial at 18px.
  6. Why do bigger sites avoid the style attribute?

Practice Answers

  1. style
  2. <p style="color: green;">Green text</p>
  3. background-color
  4. <h1 style="text-align: center;">…</h1>
  5. <p style="font-family: Arial, sans-serif; font-size: 18px;">Text</p>
  6. The same styles must be repeated on every element, so changing the design means editing every one. CSS rules avoid that.

Key Takeaways

  • style="property: value;" styles one element.
  • background-color, color, font-family, font-size and text-align cover most quick changes.
  • Separate pairs with ; and use : between property and value.
  • For real sites, move styles into CSS rules (Lecture 009).