CSS controls how HTML elements look:
Without CSS, websites would be plain text with default browser styling.
External (recommended)
<link rel="stylesheet" href="styles.css">
Internal (in <head>)
<head>
<style> h1 { color: blue; } </style>
Inline (avoid!)
<p style="color: red;">Text</p>
When multiple styles conflict, CSS follows this priority:
More specific selectors also win over less specific ones.
selector { property: value; property: value; }
Example:
h1 { color: blue; font-size: 24px; text-align: center; }
p { }
.intro { }
#header { }
* { }
/* All paragraphs */ p { color: gray; line-height: 1.6; } /* All headings */ h1, h2, h3 { font-family: Arial, sans-serif; }
HTML:
<p class="highlight">Important text</p> <p>Regular text</p> <p class="highlight">Also important</p>
CSS:
.highlight { background-color: yellow; font-weight: bold; }
Classes can be used on multiple elements.
<header id="main-header">...</header>
#main-header { background-color: navy; color: white; }
IDs should be unique - only one per page!
/* Element with class */ p.intro { } /* Element with ID */ div#sidebar { } /* Multiple classes */ .btn.primary { } /* Descendant (inside) */ nav a { } /* Direct child */ ul > li { }
From lowest to highest priority:
p
div
h1
.intro
.btn
#header
#main
style="..."
When in doubt, use classes!
/* Named colors */ color: red; background-color: lightblue; /* Hex colors */ color: #ff0000; background-color: #3498db; /* RGB / RGBA */ color: rgb(255, 0, 0); background-color: rgba(0, 0, 0, 0.5); /* 50% transparent */
font-family: Arial, sans-serif; font-size: 16px; font-weight: bold; /* or 400, 700, etc. */ font-style: italic; text-align: center; /* left, right, justify */ text-decoration: underline; line-height: 1.5;
<!-- In <head> --> <link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
body { font-family: 'Roboto', sans-serif; }
.box { /* Content size */ width: 200px; height: 100px; /* Padding (inside) */ padding: 20px; /* Border */ border: 2px solid black; /* Margin (outside) */ margin: 10px; }
/* All four sides */ padding: 10px; /* Vertical | Horizontal */ padding: 10px 20px; /* Top | Right | Bottom | Left (clockwise) */ padding: 10px 20px 10px 20px; /* Individual sides */ padding-top: 10px; margin-left: 20px;
/* Default: width doesn't include padding/border */ box-sizing: content-box; /* Better: width includes padding/border */ box-sizing: border-box; /* Apply to all elements (recommended) */ * { box-sizing: border-box; }
/* Block: takes full width, starts on new line */ display: block; /* div, p, h1 default */ /* Inline: flows with text, no width/height */ display: inline; /* span, a, strong default */ /* Inline-block: flows inline but accepts width/height */ display: inline-block; /* None: hides element completely */ display: none;
span
<div>
<div class="card"> <h2>Title</h2> <p>Content here...</p> </div>
.card { background: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
<span>
<p>The price is <span class="price">$99</span> today!</p>
.price { color: green; font-weight: bold; font-size: 1.2em; }