Skip to content
Tutorials Center
My learning

Topics

Lesson 57 of 122 ยท CSS and JavaScript in HTML

The style Attribute

Learn the HTML style attribute and CSS declaration syntax, try common properties for text, color, spacing and borders, and see when inline styles are the wrong tool.

HTML describes what your content is. CSS (Cascading Style Sheets) describes how it looks: colors, fonts, spacing, borders and layout. The quickest way to try CSS is the style attribute, which lets you write styling rules directly on a single element.

In this lesson you will learn the syntax of CSS declarations, meet the properties you will use most often, and find out why professional sites keep inline styles to a minimum. It is the first step of this chapter; the next lessons cover colors and the better ways of adding CSS to HTML.

The style attribute

style is a global attribute, so it works on any element. Its value is a list of CSS declarations:

HTML
<p style="color: darkgreen; font-size: 20px;">Fresh herbs, picked this morning.</p>

Each declaration has three parts:

Part Example Meaning
Property color What you want to change.
Colon : Separates the property from its value.
Value darkgreen How you want it changed.

Declarations are separated by semicolons. The last semicolon is optional, but adding it is a good habit โ€” you will not forget it when you add another declaration later.

Your first inline styles

Syntax mistakes to watch for

CSS is forgiving in a quiet way: when a declaration is invalid, the browser simply ignores that one declaration and keeps going. There is no error message, so typos can be hard to spot.

HTML
<!-- Wrong: equals sign instead of a colon -->
<p style="color = red;">โ€ฆ</p>

<!-- Wrong: missing semicolon, so both declarations are ignored -->
<p style="color: red font-size: 20px;">โ€ฆ</p>

<!-- Wrong: a space between the number and its unit -->
<p style="font-size: 20 px;">โ€ฆ</p>

<!-- Right -->
<p style="color: red; font-size: 20px;">โ€ฆ</p>

Debugging styles? Right-click the element and choose Inspect. The Styles panel shows every declaration the browser applied, and crosses out the ones it rejected. See Viewing Source and Using DevTools.

Common properties

There are hundreds of CSS properties, but a small set covers most everyday styling.

Text

Property Example values What it does
color navy, #333 Text color.
font-family Georgia, serif Typeface, with fallbacks separated by commas.
font-size 18px, 1.25rem Text size.
font-weight bold, 400, 700 Thickness of the letters.
font-style italic, normal Slanted or upright text.
text-align left, center, right Horizontal alignment of lines.
line-height 1.5 Space between lines; a plain number multiplies the font size.
text-decoration underline, none Underlines and strike-throughs.

Boxes and spacing

Property Example values What it does
background-color lavender Fills the element's box.
padding 12px, 8px 16px Space inside the border.
margin 24px 0 Space outside the border.
border 1px solid gray Width, style and color in one go.
border-radius 8px Rounded corners.
width, max-width 300px, 100% Size of the box.
Text and box properties

Notice that long style values can be split across several lines. That helps a little, but already you can see how cluttered the HTML is getting.

Units in one minute

  • px โ€” CSS pixels. Predictable, good for borders and small details.
  • rem โ€” relative to the root font size (usually 16px). 1.5rem is 1.5 times that. Good for text, because it respects the reader's font settings.
  • em โ€” relative to the current element's font size.
  • % โ€” relative to the parent, for example width: 50%.

A value of 0 needs no unit: margin: 0.

Inline styles and the cascade

Styles can come from several places: the browser's defaults, style sheets and style attributes. When two rules disagree, CSS uses a set of priority rules known as the cascade. Inline styles win over normal rules in a stylesheet, because they target exactly one element.

Inline styles override the stylesheet

Only the conflicting declaration is overridden. The second paragraph is still 18px because the inline style says nothing about size. You will learn more about how the cascade decides in Adding CSS to HTML.

The limits of inline styles

Inline styles are handy for experiments, but they cause real problems on a full website:

  • Repetition. Twenty product cards means writing the same declarations twenty times. Changing the brand color means editing every one of them.
  • Hard to read. The HTML gets buried under presentation details, which makes the structure harder to see.
  • Hard to override. Because inline styles beat normal stylesheet rules, a site-wide stylesheet can't easily adjust them.
  • No states or screen sizes. A style attribute can't express :hover, :focus or media queries. You can't make a link change color on hover with inline CSS alone.
  • Security policies. Sites with a strict Content Security Policy often block inline styles entirely.

Here is the same idea written once, with a class, instead of three times inline:

HTML
<style>
  .price { color: #2f6b3a; font-weight: bold; }
</style>

<p>Apples: <span class="price">$2.40</span></p>
<p>Pears: <span class="price">$2.90</span></p>
<p>Plums: <span class="price">$3.10</span></p>

Changing the price color now takes one edit. That is why classes and stylesheets are the normal way to style a site.

When inline styles are fine

  • Quick experiments and prototypes (like the examples on this page).
  • HTML emails, where many email programs ignore <style> blocks.
  • Values set by JavaScript, such as the position of an element being dragged.
  • One-off values that come from data, for example a progress bar's width or a CSS custom property like style="--accent: teal" that a stylesheet then uses.

Style is not meaning. Making a paragraph big and bold with style does not turn it into a heading. Pick elements for their meaning โ€” <h2>, <strong>, <em> โ€” and use CSS only to change how they look.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which style value is written correctly?

    Each declaration is property: value, and declarations are separated by semicolons.
  2. 2. A stylesheet says p { color: green; }. A paragraph has style="color: red;". What color is its text?

    In the cascade, a declaration in a style attribute overrides normal declarations from stylesheets.
  3. 3. You write style="font-size: 20 px;". What happens?

    20 px with a space is not a valid length, so the browser silently drops that declaration.
  4. 4. Which of these can you NOT do with a style attribute alone?

    States like :hover and media queries need a selector, which only exists in a stylesheet.

Summary

  • CSS controls how HTML looks; the style attribute applies CSS to a single element.
  • A declaration is property: value; separate declarations with semicolons.
  • Invalid declarations are silently ignored, so check DevTools when something does not apply.
  • Inline styles override normal stylesheet rules, but they repeat easily and can't handle :hover or media queries.
  • Use them for experiments, emails and script-set values; use classes and stylesheets for real sites.

Next lesson: Colors โ€” all the ways to write a color in CSS.