- Home
- /
- Tutorials
- /
- CSS Tutorial
- /
- How to Add CSS
CSS Introduction
How to Add CSS
Compare inline, internal, and external CSS, then choose the method that keeps styles reusable, cacheable, and maintainable.
There are three places to put CSS: in a style attribute on one element, in a style block in the page's head, or in a separate .css file linked to every page. Real websites use the third one almost always, and here is why.
Whichever you choose, the CSS itself is written the same way, as explained in CSS Syntax. The simplest rules target elements by name, as in CSS Element Selector.
Syntax
1. Inline CSS
Written straight on one element, in its style attribute.
html
<p style="color: red;">This is a paragraph.</p>Quick for testing, but it styles only that one element, mixes design into your HTML, and beats almost every other rule, which makes it hard to override later.
2. Internal CSS
Written inside a <style> element in the <head>. It styles the whole page, but only this page.
html
<style>
p {
color: blue;
}
</style>3. External CSS
Written in its own .css file and connected with a <link> tag in the <head> of every page.
html
<link rel="stylesheet" href="styles.css">styles.css file
css
p {
color: green;
}One file styles the whole site. Change a color once and every page updates, and the browser downloads the file once and reuses it from its cache on every other page.
Which one to use
| Method | Where it goes | Use it for |
|---|---|---|
| Inline | The style attribute of one element | Quick tests, HTML emails, values set by JavaScript |
| Internal | A <style> block in the <head> | A single page, a demo, or a small amount of critical CSS |
| External | A .css file linked with <link> | Every real website |
Complete Example
html
<!DOCTYPE html>
<html>
<head>
<title>How to Add CSS</title>
<!-- Internal CSS -->
<style>
h1 {
color: blue;
}
</style>
<!-- External CSS -->
<link rel="stylesheet" href="/samples/styles.css">
</head>
<body>
<!-- Inline CSS -->
<p style="color: red;">This is inline CSS</p>
<h1>This is internal CSS</h1>
<p>This is external CSS</p>
</body>
</html>The first paragraph is red from its inline style, the heading is blue from the internal <style> block, and the last paragraph would be green from the external file. In the Try It editor there is no styles.css, so that last line stays in the default color, which is a good reminder that a wrong file path silently does nothing.
Browser Support
Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
| CSS stylesheets | 1 | 12 | 1 | 1 |
Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8.
When styles clash
If the same element gets different colors from these methods, the normal cascade rules decide. Inline styles win over the others, and between internal and external, the rule that comes later in the page wins when selectors are equal. The full rules are in the cascade.
Good to know
- Check the
hrefpath if your external CSS "does nothing". The Network panel in DevTools shows a red 404 when the file is not found. - Put the
<link>in the<head>, so the page is styled before it appears. - More on writing styles in HTML in HTML Styles.
Exercises: How to Add CSS
A rule in the stylesheet
The CSS panel is the stylesheet of this page. Make the heading navy from it.
The HTML your code works on
<h1>Shop</h1>
<p id="note" style="color: red">Closed on Monday.</p>
<p class="tip">Bring a bag.</p>Result
