There are three ways to add CSS to an HTML page.
1. External CSS (best)
Write styles in a separate .css file and link it in the <head>:
<link rel="stylesheet" href="style.css">
One file can style the whole website.
2. Internal CSS
Write styles inside a <style> tag in the <head>. Good for a single page.
3. Inline CSS
Write styles in the style attribute of one element: <p style="color:red">. Use it rarely — it is hard to maintain.
Which one wins?
If the same property is set in many places, inline beats internal and external styles. Between internal and external, the rule written last wins.
Example
Example — Internal and inline CSS
HTML
<!DOCTYPE html>
<html>
<head>
<style>
h2 { color: green; }
</style>
</head>
<body>
<h2>Styled by internal CSS</h2>
<h2 style="color: crimson;">Inline CSS wins here</h2>
</body>
</html>
The second heading uses an inline style, which overrides the internal rule.
Try it Now »