CSS Fundamentals
CSS (Cascading Style Sheets) controls how HTML elements look. It separates presentation from structure — HTML defines content, CSS defines appearance.
What is CSS?
- HTML = Structure (what things are)
- CSS = Styling (how things look)
- JavaScript = Behavior (what things do)
CSS Syntax
Every CSS rule has a selector and declarations:
selector {
property: value;
}
Example:
h1 {
color: blue;
font-size: 24px;
}
h1— Selector (targets the element)color— Property (what to change)blue— Value (what to change it to)
Three Ways to Add CSS
1. External Stylesheet (Recommended)
Link a separate CSS file:
<head>
<link rel="stylesheet" href="styles.css" />
</head>
/* styles.css */
h1 {
color: blue;
}
Benefits:
- Single file for entire site
- Cached by browser (faster)
- Easy to maintain
2. Internal Style Block
Add CSS in the <head>:
<head>
<style>
h1 {
color: blue;
}
</style>
</head>
Use for:
- Single-page sites
- Page-specific styles
3. Inline Styles
Add CSS directly to elements:
<h1 style="color: blue;">Hello</h1>
Avoid when possible:
- Hard to maintain
- Overrides other styles
- Mixes structure and presentation
Comments
Add comments to explain your code:
/* This is a CSS comment */
h1 {
/* Blue color for headings */
color: blue;
}
Basic Selectors
Element Selector
Targets all elements of a type:
p {
color: black;
}
h1 {
font-size: 32px;
}
Class Selector
Targets elements with a class attribute:
<p class="highlight">Yellow background</p>
<p>Normal paragraph</p>
.highlight {
background-color: yellow;
}
Note: Classes start with .
ID Selector
Targets a single element with an ID:
<div id="header">Header content</div>
#header {
background-color: #333;
}
Note: IDs start with #
Properties and Values
Common Properties
/* Text */
h1 {
color: blue;
font-size: 24px;
font-weight: bold;
text-align: center;
}
/* Background */
body {
background-color: #f5f5f5;
background-image: url('bg.jpg');
}
/* Box Model */
div {
margin: 10px;
padding: 20px;
border: 1px solid black;
}
/* Width/Height */
img {
width: 100%;
height: auto;
}
Value Types
Colors
/* Named colors */
color: red;
/* Hexadecimal */
color: #ff0000;
color: #f00;
/* RGB */
color: rgb(255, 0, 0);
/* RGBA (with opacity) */
color: rgba(255, 0, 0, 0.5);
/* HSL */
color: hsl(0, 100%, 50%);
Lengths
/* Pixels */
font-size: 16px;
/* Em (relative to parent) */
font-size: 1.5em;
/* Rem (relative to root) */
font-size: 1.5rem;
/* Percentage */
width: 50%;
/* Viewport units */
width: 100vw;
height: 100vh;
Complete Example
Here’s a complete CSS file:
/* styles.css */
/* Reset some defaults */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Body styling */
body {
font-family: Arial, sans-serif;
line-height: 1.6;
color: #333;
background-color: #f5f5f5;
}
/* Headings */
h1 {
font-size: 2.5rem;
color: #2c3e50;
margin-bottom: 1rem;
}
h2 {
font-size: 2rem;
color: #34495e;
margin-bottom: 0.75rem;
}
/* Paragraphs */
p {
font-size: 1rem;
margin-bottom: 1rem;
line-height: 1.8;
}
/* Links */
a {
color: #3498db;
text-decoration: none;
}
a:hover {
color: #2980b9;
text-decoration: underline;
}
/* Buttons */
.button {
display: inline-block;
padding: 10px 20px;
background-color: #3498db;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
.button:hover {
background-color: #2980b9;
}
/* Container */
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
Complete HTML with CSS
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>CSS Fundamentals</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div class="container">
<h1>CSS Fundamentals</h1>
<p>
CSS controls how HTML elements look.
<a href="#">Learn more</a>
</p>
<button class="button">Click Me</button>
</div>
</body>
</html>
Best Practices
- Use external stylesheets — Easier to maintain
- Use classes over IDs — More reusable
- Use meaningful names —
.nav-itemnot.blue - Keep it organized — Group related styles
- Use comments — Explain complex styles
Common Mistakes
- Missing semicolon — Each declaration needs
; - Missing curly braces — Properties must be inside
{} - Using IDs for styling — Use classes instead
- Inline styles everywhere — Hard to maintain
- Not using box-sizing — Causes layout issues