CSS Basics: A Complete Beginner's Guide
CSS Basics: A Complete Beginner's Guide
Welcome to the world of Cascading Style Sheets (CSS)! This guide will teach you everything you need to know to start styling web pages like a pro.
What is CSS?
CSS stands for Cascading Style Sheets. It's a language that describes how HTML elements should look and behave on a webpage. Think of it as the "skin" or "style" of your website.
Why Use CSS?
- Separates content (HTML) from presentation (design)
- Makes websites look beautiful
- Allows for consistent styling across multiple pages
- Enables responsive design that works on all devices
Getting Started with CSS
CSS code is written in rules. Each rule has two parts:
- A selector - which HTML elements to style
- A declaration block - what styles to apply (inside curly braces)
/* Basic CSS syntax */
selector {
property: value;
}
Simple Example
Here's a basic example that makes all heading text red:
h1 {
color: red;
}
This rule says:
- Target all
<h1>elements - Make their text color red
Basic CSS Properties
Colors
p {
color: blue; /* Named color */
color: #ff0000; /* Hex color */
color: rgb(255, 0, 0); /* RGB color */
}
Text Styling
h1 {
font-size: 24px;
font-family: Arial, sans-serif;
text-align: center;
font-weight: bold;
}
CSS Selectors
Selectors tell CSS which elements to style. Here are the most common ones:
Element Selectors
p { color: blue; } /* Targets all <p> elements */
h1 { font-size: 28px; } /* Targets all <h1> elements */
div { margin: 10px; } /* Targets all <div> elements */
Class Selectors (Most Useful!)
/* Define a class selector with a dot */
.highlight {
background-color: yellow;
font-weight: bold;
}
/* Apply to HTML elements */
<p class="highlight">This paragraph will be highlighted</p>
ID Selectors
/* Define an ID selector with hash */
#main-title {
color: purple;
text-align: center;
}
<!-- Apply to HTML element -->
<h1 id="main-title">This is the main title</h1>
CSS Comments
Comments help you document your code and make it easier to understand:
/* This is a comment */
p {
color: blue; /* This sets text color to blue */
}
Your First Complete HTML + CSS Page
Here's how everything works together:
HTML File (index.html):
<!DOCTYPE html>
<html>
<head>
<title>My First Styled Page</title>
<!-- Link your CSS file -->
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1 class="main-title">Welcome to My Website</h1>
<p>This is a paragraph with some text.</p>
<div class="container">
<p class="highlight">This paragraph has special styling!</p>
</div>
</body>
</html>
CSS File (style.css):
/* Style all headings */
h1 {
color: #333;
text-align: center;
}
/* Style paragraphs */
p {
font-size: 16px;
line-height: 1.5;
margin-bottom: 10px;
}
/* Define a class for special styling */
.highlight {
background-color: yellow;
padding: 10px;
border-radius: 5px;
}
/* Container with some spacing */
.container {
margin: 20px;
padding: 15px;
border: 1px solid #ccc;
}
Common CSS Properties
Layout Properties
div {
width: 300px; /* Set width */
height: 200px; /* Set height */
margin: 10px; /* Space around element */
padding: 15px; /* Space inside element */
}
Background Properties
div {
background-color: #f0f0f0; /* Background color */
background-image: url('image.jpg'); /* Add background image */
}
Best Practices for Beginners
- Always save your CSS in a separate file (like
style.css) - Use meaningful class names like
.header-navinstead of.nav1 - Comment your code to explain what complex styles do
- Test often - make small changes and see the results immediately
Next Steps
Now you know:
- How CSS works with HTML
- Basic syntax and structure
- Common selectors (element, class, ID)
- Essential properties for styling text and layout
To continue learning CSS, explore topics like:
- Flexbox (for modern layouts)
- Grid (powerful two-dimensional layouts)
- Responsive design (mobile-friendly sites)
- Advanced selectors and pseudo-elements
- Animations and transitions
Quick Reference Card
/* Selectors */
element { } /* Any HTML element */
.class-name { } /* Elements with specific class */
#id-name { } /* Element with specific ID */
/* Common Properties */
color: blue; /* Text color */
background-color: red; /* Background color */
font-size: 16px; /* Font size */
text-align: center; /* Text alignment */
margin: 10px; /* Margin (space outside) */
padding: 15px; /* Padding (space inside) */
width: 300px; /* Width */
height: 200px; /* Height */
Practice Exercise
Try creating your own simple webpage:
- Create an HTML file with headings, paragraphs, and a list
- Create a separate CSS file
- Style at least one element using each of these selectors:
- Element selector
- Class selector
- ID selector
Great job! You're now ready to start styling web pages effectively. Keep practicing and experimenting with different properties - the more you code, the more comfortable you'll become with CSS!
Remember: CSS is all about practice. Don't be afraid to experiment and make mistakes. The best way to learn is by doing!