HTML Styles

Beginner
10 min read
Updated: Nov 2026
6 examples
style=“…”

Definition and Usage

HTML styles change how elements look—color, size, font, alignment, and more—using CSS. The HTML style attribute adds CSS to one element. You can also put CSS in a <style> element or link an external stylesheet.

What does the style attribute do?

It holds one or more CSS declarations: property: value; pairs. Example: style="color:blue;font-size:18px;".

Three ways to add styles

Inline (style attribute), internal (<style> in <head>), and external (<link rel="stylesheet">). For a deeper walkthrough of all three, see How to Add CSS to HTML.

When should you use each?

Use the style attribute for quick Try It demos. Prefer a <style> block while learning in one file. Use an external CSS file for real multi-page sites.

CSS property = look.

HTML describes structure; CSS describes presentation. Styles tutorials teach the bridge between them.

Syntax reminder.

<tagname style="property: value;"> — property and value are CSS, not HTML attribute names.

Do not overuse inline styles.

They are hard to reuse and override. Move shared rules into CSS classes as soon as patterns repeat.

HTML Style Syntax

The style attribute form:

style-attr.html
<tagname style="property: value;">
HTML
<body style="background-color:powderblue;">
  <h1 style="color:blue;">This is a heading</h1>
  <p style="font-family:verdana;font-size:16px;">This is a paragraph.</p>
</body>

Separate multiple declarations with semicolons. Property names use hyphens in CSS (font-size, background-color, text-align).

Core HTML Concepts

Starter CSS properties used with HTML:

PropertyControlsExample value
background-colorBackground fillpowderblue
colorText colorblue
font-familyTypefaceverdana
font-sizeText size20px or 150%
text-alignHorizontal alignmentcenter

Inline vs internal vs external

MethodWhereBest for
Inlinestyle attributeOne-off demos
Internal<style> in <head>Single-page practice
External<link rel="stylesheet">Real websites

How to Style HTML

A simple path from attribute to stylesheet.

  1. Start with the style attribute on a heading or paragraph.
  2. Try background-color, color, font-family, font-size, and text-align.
  3. Move repeated rules into a <style> block with selectors (h1, .note).
  4. When the page grows, extract CSS to a .css file and link it.
  5. Keep HTML for structure; keep visual rules in CSS.
  6. Read How to Add CSS to HTML for selectors, classes, and id.
GoalPattern
Page background<body style="background-color:powderblue;">
Blue heading<h1 style="color:blue;">
Centered titlestyle="text-align:center;"
Shared theme<link rel="stylesheet" href="styles.css">

Live Preview

Pick a style and apply it to the sample heading.

Mirrors common first CSS properties used with the style attribute.

Rendered page

This is a heading

<h1 style="background-color:powderblue;">This is a heading</h1>

HTML Examples

Six patterns: background, text color, fonts, size, alignment, and style element plus external CSS overview.

Example 1 — Background Color

HTML
<body style="background-color:powderblue;">
  <h1>This is a heading</h1>
  <p>This is a paragraph.</p>
</body>
Try It Yourself

How It Works

  1. background-color paints the element’s background.
  2. body styles often affect the whole page canvas.
  3. Named colors work here—see also HTML Colors.
  4. Inline style is fine for learning this property quickly.

Example 2 — Text Color

HTML
<h1 style="color:blue;">This is a heading</h1>
<p style="color:red;">This is a paragraph.</p>
Try It Yourself

How It Works

  1. color sets the foreground (text) color.
  2. Different elements can use different colors.
  3. Contrast against the background still matters.
  4. Not the old <font color> tag—use CSS.

Example 3 — Fonts

HTML
<h1 style="font-family:verdana;">This is a heading</h1>
<p style="font-family:courier;">This is a paragraph.</p>
Try It Yourself

How It Works

  1. font-family chooses the typeface.
  2. Fallback stacks (e.g. Georgia, serif) are better for production.
  3. Generic families include serif, sans-serif, and monospace.
  4. Web fonts come later via CSS @font-face or font services.

Example 4 — Text Size

HTML
<h1 style="font-size:300%;">This is a heading</h1>
<p style="font-size:160%;">This is a paragraph.</p>
Try It Yourself

How It Works

  1. font-size accepts px, %, em, rem, and more.
  2. Percentages scale relative to the parent’s font size.
  3. Headings already have default sizes—CSS overrides them.
  4. Keep body text readable (avoid tiny sizes).

Example 5 — Text Alignment

HTML
<h1 style="text-align:center;">Centered Heading</h1>
<p style="text-align:center;">Centered paragraph.</p>
Try It Yourself

How It Works

  1. text-align aligns inline content inside a block.
  2. Values include left, right, center, and justify.
  3. Works on headings, paragraphs, and other block containers.
  4. Not the same as CSS Flexbox/Grid centering for whole layouts.

Example 6 — Style Element & External CSS Overview

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>HTML Styles Example</title>
  <!-- External CSS (preferred for real sites) -->
  <link rel="stylesheet" href="styles.css">
  <!-- Internal CSS (great while learning) -->
  <style>
    h1 { color: navy; text-align: center; }
    p { font-family: verdana; font-size: 16px; }
  </style>
</head>
<body>
  <!-- Inline CSS (quick demo / override) -->
  <h1>Styled with CSS</h1>
  <p style="color:teal;">This paragraph uses an inline override.</p>
  <p>This paragraph uses the style element rules.</p>
</body>
</html>
Try It Yourself

How It Works

  1. link loads an external stylesheet when the file exists.
  2. style embeds CSS in the same HTML file.
  3. Inline can override shared rules on one element.
  4. Deep dive: How to Add CSS to HTML.

Edge Cases & Pitfalls

Most style bugs come from typos, missing semicolons, or fighting specificity.

syntax

Missing colon or semicolon

Write color: blue; not color blue. Invalid declarations are ignored.

quotes

Quotes inside the attribute

If the attribute uses double quotes, use single quotes for CSS font names with spaces: font-family: 'Times New Roman';.

cascade

Inline wins often

An inline style can override your stylesheet. Remove or reduce inline styles when debugging.

obsolete

Presentational tags

Avoid <font>, <center>, and bgcolor. Use CSS instead.

path

Broken stylesheet path

If href is wrong, external CSS never loads. Check the Network panel.

a11y

Style-only meaning

Do not rely on color alone for meaning. Pair color with text or icons.

Browser Support

The style attribute, style element, and link stylesheet pattern work in all browsers.

✓ Baseline · Widely available

HTML styles / CSS

Every browser that renders HTML applies CSS from inline styles, style elements, and linked stylesheets.

100% style methods
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
HTML styling Universal

Bottom line: Use the style attribute to learn, then graduate to style blocks and external CSS.

Key Takeaways

  • style="property: value;" applies CSS to one element.
  • Starter properties: background-color, color, font-family, font-size, text-align.
  • style element + link scale better than inline-only CSS.
  • Next: Add CSS to HTML for the full workflow.

One line: Style HTML with CSS via the style attribute, a style element, or an external stylesheet—structure stays in HTML, look stays in CSS.

Frequently Asked Questions

The style attribute adds CSS declarations to one element, such as color, font-size, or background-color. Syntax: style="property: value;". Use it for quick demos; prefer a stylesheet for real sites.
Beginners usually start with background-color, color, font-family, font-size, and text-align. Each property takes a CSS value (for example color: blue; or font-size: 20px;).
The style element holds CSS rules inside the HTML document, usually in head. Example: <style> h1 { color: navy; } </style>. It styles many elements without repeating the style attribute.
In head, add <link rel="stylesheet" href="styles.css">. The browser loads that file and applies its rules. See the Add CSS to HTML tutorial for full details.
External CSS is best for most websites. Use a style block while learning in one file. Reserve the style attribute for one-off overrides or tiny examples.
Usually yes—inline style has high specificity and often wins over linked or internal CSS, unless the stylesheet uses !important (avoid that as a beginner).

Practice in the editor

Open a Try It demo and change colors, fonts, and alignment.

Open Try It editor →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful