HTML Styles
HTML styles are the visual rules, written in CSS, that control how HTML elements look: colours, fonts, spacing, sizes and more. HTML gives you three places to put those rules, the style attribute on an element, a <style> element in the page, or a separate stylesheet file. The style attribute is the quickest to try, so it is a good place to start before comparing all three.
The style attribute
Any element can take a style attribute containing one or more CSS declarations, each written as property: value and separated by semicolons. These are called inline styles.
<h2 style="color: darkgreen; text-align: center;">Admissions Open</h2>
<p style="font-size: 18px; background-color: #fff8e1; padding: 12px;">
Classes start next Monday.
</p>
<p>Fees are <span style="font-weight: bold; color: crimson;">non-refundable</span>.</p>
There is no selector and there are no curly braces; the style applies only to that one element. It is handy for quick experiments, but it does not scale well.
Properties you will use most often
| Property | Changes | Example value |
|---|---|---|
color | Text colour | navy, #333 |
background-color | Background behind the element | lightyellow |
font-family | Typeface | Arial, sans-serif |
font-size | Text size | 1.2rem, 18px |
text-align | Horizontal alignment | left, center |
Older tutorials sometimes show tags like <font> and <center> or attributes like bgcolor. These are obsolete; use CSS properties instead.
Three ways to add CSS, compared
- Inline, with the
styleattribute: affects one element. - Internal, with a
<style>block in the<head>: affects one page and uses normal selectors. - External, with
<link rel="stylesheet" href="styles.css">: one file shared by every page of the site.
External stylesheets are the normal choice for real sites. The browser downloads the file once and caches it, every page looks consistent, and changing a brand colour means editing one line instead of hundreds of tags.
<head>
<link rel="stylesheet" href="styles.css">
<style>
.notice { background-color: #fff8e1; padding: 12px; }
</style>
</head>
Why inline styles cause trouble later
Inline styles have very high priority in the cascade. They beat rules from your stylesheet unless those rules use !important. This leads to a frustrating situation: you change the colour in styles.css, refresh, and nothing happens, because an inline style on the element is still winning.
They also mix content with presentation, so the same declarations get copied onto many elements, and they cannot express hover states, media queries for mobile screens, or pseudo-elements. When you notice the same inline style on two elements, move it into a class.
A small syntax slip to watch: writing style="color=red" with an equals sign instead of a colon. The browser ignores the invalid declaration silently, so double-check the colon and semicolon.
When inline styles are reasonable
Inline styles also make pages heavier. A declaration repeated on fifty elements is sent fifty times, while a class in a cached stylesheet is downloaded once.
They still have uses: values calculated by JavaScript, such as setting a progress bar's width, one-off tests while debugging, and HTML emails, where many mail apps ignore external stylesheets.
In practice: learn properties with the style attribute, but put real site styles in an external stylesheet and target elements with classes.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.