On this page11 sections
Lecture 006: The style Attribute
| Category | 2: Text & Styles |
| Lecture | 006 of 048 |
| Estimated video length | 10 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
Every HTML element can carry a
styleattribute 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
styleattribute withproperty: 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
<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
<p style="color: purple;">This paragraph is purple.</p>2. Background Colour
Example
<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
<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
<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
<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:
<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
- Which attribute changes how a single element looks?
- Write a paragraph with green text.
- Which property sets the background colour?
- How do you make a heading's text centred?
- Write a paragraph in Arial at 18px.
- Why do bigger sites avoid the
styleattribute?
Practice Answers
style<p style="color: green;">Green text</p>background-color<h1 style="text-align: center;">…</h1><p style="font-family: Arial, sans-serif; font-size: 18px;">Text</p>- 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-sizeandtext-aligncover most quick changes.- Separate pairs with
;and use:between property and value. - For real sites, move styles into CSS rules (Lecture 009).