This written guide is designed to be concise yet comprehensive, covering the fundamentals of CSS (Cascading Style Sheets) for beginners or those needing a quick refresher. It assumes basic HTML knowledge. You’ll learn how to style web pages effectively in a short time.
Table of Contents
- Introduction to CSS
- Syntax and Selectors
- Colors and Backgrounds
- Box Model
- Typography
- Layouts (Flexbox and Grid)
- Responsive Design
- Pseudo-Classes and Pseudo-Elements
- Transitions and Animations
- Best Practices and Next Steps
1. Introduction to CSS
- What is CSS? CSS styles HTML elements to control layout, colors, fonts, and more.
How to Include CSS:
- Inline: <p style=”color: blue;”>Text</p>
- Internal: <style> p { color: blue; } </style> in <head>
- External: <link rel=”stylesheet” href=”styles.css”>
Example:
<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Hello, CSS!</h1>
</body>
</html>/* styles.css */
h1 {
color: navy;
}
2. Syntax and Selectors
Syntax:
selector { property: value; }Types of Selectors:
- Element: p { font-size: 16px; }
- Class: .class-name { color: red; }
- ID: #id-name { background: yellow; }
- Universal: * { margin: 0; }
- Attribute: [type=”text”] { border: 1px solid; }
Combinators:
- Descendant: div p { color: green; }
- Child: ul > li { list-style: none; }
Example:
.highlight {
background: yellow;
}
#header {
text-align: center;
}
3. Colors and Backgrounds
Color Formats:
- Names: red
- Hex: #FF0000
- RGB: rgb(255, 0, 0)
- RGBA: rgba(255, 0, 0, 0.5)
Background Properties:
- background-color: background-color: blue;
- background-image: background-image: url(‘image.jpg’);
- background-size: cover, contain
- background-position: center
Example:
.box {
background: linear-gradient(to right, blue, red);
color: white;
}
4. Box Model
Components:
- Content: Text/images
- Padding: Space inside border (padding: 10px;)
- Border: Surrounds padding (border: 1px solid black;)
- Margin: Space outside border (margin: 20px;)
Box-Sizing:
- Default: box-sizing: content-box
- Recommended: box-sizing: border-box
Example:
.container {
width: 200px;
padding: 15px;
border: 2px solid gray;
margin: 10px;
box-sizing: border-box;
}
5. Typography
Font Properties:
- font-family: “Arial”, sans-serif
- font-size: 16px, 1rem
- font-weight: 400, bold
- line-height: 1.5
- text-align: center, left
Text Decoration:
- text-decoration: underline, none
Example:
p {
font-family: "Helvetica", sans-serif;
font-size: 1.2rem;
line-height: 1.6;
text-align: justify;
}
6. Layouts (Flexbox and Grid)
Flexbox:
- Container: display: flex;
- Properties:
- justify-content: space-between, center
- align-items: center, stretch
- flex-direction: row, column
- Item: flex: 1;, order: 2;
Grid:
- Container: display: grid;
- Properties:
- grid-template-columns: repeat(3, 1fr)
- gap: 10px
Example:
.flex-container {
display: flex;
justify-content: space-around;
align-items: center;
}
.grid-container {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
}
7. Responsive Design
Units:
- Relative: vw, vh, rem, em, %
- Fixed: px
Media Queries:
@media (max-width: 600px) {
body {
font-size: 14px;
}
}
Example:
.responsive-box {
width: 100%;
max-width: 800px;
margin: 0 auto;
}
8. Pseudo-Classes and Pseudo-Elements
Pseudo-Classes:
- :hover, :active, :focus
- Example: a:hover { color: red; }
Pseudo-Elements:
- ::before, ::after
Example:
.box::before {
content: "★";
color: gold;
}
Example:
button:hover {
background: darkblue;
color: white;
}
9. Transitions and Animations
Transitions:
- transition: property duration timing-function
Example:
.box {
width: 100px;
transition: width 0.3s ease;
}
.box:hover {
width: 200px;
}
Animations:
- @keyframes:
Example:
@keyframes slide {
0% { transform: translateX(0); }
100% { transform: translateX(100px); }
}
.animate {
animation: slide 2s infinite;
}
10. Best Practices and Next Steps
Best Practices:
- Use external stylesheets.
- Keep selectors specific but not overly complex.
- Use box-sizing: border-box globally.
- Test responsiveness across devices.
Next Steps:
- Explore CSS frameworks (Bootstrap, Tailwind).
- Learn CSS preprocessors (Sass, Less).
- Practice with projects (e.g., build a portfolio site).
Resources:
- MDN Web Docs: CSS
- CSS-Tricks
- FreeCodeCamp
Practice Project
Create a simple webpage with:
- A centered header with a gradient background.
- A flexbox layout for three cards.
- Responsive design for mobile.
- Hover effects on buttons.
HTML
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<h1>CSS Crash Course</h1>
</header>
<main>
<div class="card">Card 1</div>
<div class="card">Card 2</div>
<div class="card">Card 3</div>
</main>
</body>
</html>
CSS
* {
box-sizing: border-box;
margin: 0;
}
header {
background: linear-gradient(to right, #ff6b6b, #4ecdc4);
text-align: center;
padding: 20px;
color: white;
}
main {
display: flex;
justify-content: center;
gap: 20px;
padding: 20px;
}
.card {
background: #f4f4f4;
padding: 15px;
border-radius: 8px;
transition: transform 0.3s;
}
.card:hover {
transform: scale(1.05);
}
@media (max-width: 600px) {
main {
flex-direction: column;
align-items: center;
}
}