Learn CSS Styling
Master CSS styling techniques to create visually appealing and responsive web designs.
CSS Basics & Selectors
Understanding CSS Syntax
CSS (Cascading Style Sheets) controls the visual presentation of HTML elements. The basic syntax consists of a selector and a declaration block:
selector {
property: value;
property: value;
}Types of Selectors
Element Selectors
Target HTML elements directly:
p { color: blue; }Class Selectors
Target elements with a specific class (prefix with .):
.highlight { background-color: yellow; }ID Selectors
Target a unique element (prefix with #):
#header { font-size: 24px; }Specificity & Cascade
Specificity determines which rule applies when conflicts exist. Order of precedence:
- 1Inline styles (highest)
- 2IDs
- 3Classes, attributes, pseudo-classes
- 4Elements (lowest)
The cascade means later rules override earlier ones with equal specificity.
Use classes for reusable styles and IDs sparingly for unique elements.
Box Model & Spacing
The CSS Box Model
Every HTML element is a box with four layers:
βββββββββββββββββββββββββββ
β Margin β
β βββββββββββββββββββββ β
β β Border β β
β β βββββββββββββββββ β β
β β β Padding β β β
β β β βββββββββββββ β β β
β β β β Content β β β β
β β β βββββββββββββ β β β
β β βββββββββββββββββ β β
β βββββββββββββββββββββ β
βββββββββββββββββββββββββββ
Properties Explained
| Property | Purpose | Example |
|---|---|---|
| Content | Actual element size | width, height |
| Padding | Space inside border | padding: 20px; |
| Border | Outline around padding | border: 2px solid black; |
| Margin | Space outside border | margin: 10px; |
Box-Sizing Property
By default, width and height only include content. Use box-sizing: border-box; to include padding and border:
* {
box-sizing: border-box;
}Always set `box-sizing: border-box;` on all elements for predictable sizing.
Recommended Videos

CSS Flexbox in 100 Seconds
Fireship
Quick overview of CSS Flexbox fundamentals and practical examples for layout.

CSS Grid Layout Tutorial
Traversy Media
Comprehensive guide to CSS Grid with real-world examples and best practices.

CSS Animations & Transitions
Web Dev Simplified
Learn how to create smooth animations and transitions to enhance user experience.
Colors, Fonts & Text Styling
Color Formats
CSS supports multiple color formats:
/* Named colors */
color: red;
/* Hexadecimal */
color: #FF0000;
/* RGB */
color: rgb(255, 0, 0);
/* RGBA (with transparency) */
color: rgba(255, 0, 0, 0.5);
/* HSL */
color: hsl(0, 100%, 50%);Font Properties
body {
font-family: 'Arial', sans-serif;
font-size: 16px;
font-weight: bold; /* 400-700 or normal/bold */
font-style: italic; /* normal or italic */
line-height: 1.6; /* Space between lines */
letter-spacing: 2px; /* Space between characters */
}Text Styling
.text {
text-align: center; /* left, right, center, justify */
text-decoration: underline; /* underline, overline, line-through */
text-transform: uppercase; /* uppercase, lowercase, capitalize */
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}Use `line-height: 1.5-1.6` for better readability in body text.
CSS Styling Cheatsheet & Mini-Project
Quick Reference Table
| Feature | Syntax | Example |
|---|---|---|
| Selector | Element, .class, #id | p { }, .btn { }, #header { } |
| Colors | hex, rgb, rgba, hsl | #FF0000, rgb(255,0,0), rgba(255,0,0,0.5) |
| Box Model | margin, padding, border | padding: 20px; margin: 10px; |
| Fonts | font-family, font-size, font-weight | font-family: Arial; font-size: 16px; |
| Layout | display, flex, grid | display: flex; justify-content: center; |
| Effects | transform, transition, animation | transform: scale(1.1); transition: 0.3s; |
Mini-Project: Styled Card Component
<div class="card">
<img src="image.jpg" alt="Card image">
<h2>Card Title</h2>
<p>Card description goes here.</p>
<button class="btn">Learn More</button>
</div>.card {
width: 300px;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
overflow: hidden;
transition: transform 0.3s ease;
}
.card:hover {
transform: translateY(-5px);
box-shadow: 0 8px 12px rgba(0,0,0,0.2);
}
.card img {
width: 100%;
height: 200px;
object-fit: cover;
}
.card h2 {
padding: 20px 20px 10px;
margin: 0;
}
.card p {
padding: 0 20px 20px;
color: #666;
}
.btn {
margin: 0 20px 20px;
padding: 10px 20px;
background: #3498db;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background 0.3s;
}
.btn:hover {
background: #2980b9;
}This card component demonstrates selectors, box model, colors, transitions, and hover effectsβcore CSS skills!
Layout Techniques
Display Property
The display property controls how elements are rendered:
display: block; /* Full width, new line */
display: inline; /* Only takes needed width */
display: inline-block; /* Inline but respects width/height */
display: none; /* Hidden from layout */Flexbox
Flexbox creates flexible, one-dimensional layouts:
.container {
display: flex;
justify-content: center; /* Horizontal alignment */
align-items: center; /* Vertical alignment */
gap: 20px; /* Space between items */
}CSS Grid
Grid creates two-dimensional layouts:
.grid {
display: grid;
grid-template-columns: 1fr 1fr 1fr; /* 3 equal columns */
grid-gap: 20px;
}Positioning
position: static; /* Default */
position: relative; /* Relative to normal position */
position: absolute; /* Relative to positioned parent */
position: fixed; /* Relative to viewport */
position: sticky; /* Hybrid of relative and fixed */Responsive Design Basics
@media (max-width: 768px) {
.container { flex-direction: column; }
}Flexbox for 1D layouts, Grid for 2D layouts. Use media queries for responsive design.
Advanced Styling & Effects
Transitions
Smooth animations between property changes:
.button {
background: blue;
transition: background 0.3s ease;
}
.button:hover {
background: darkblue;
}Animations
Complex, keyframe-based animations:
@keyframes slide {
0% { transform: translateX(0); }
100% { transform: translateX(100px); }
}
.box {
animation: slide 2s ease infinite;
}Transforms
Manipulate element appearance:
transform: rotate(45deg);
transform: scale(1.5);
transform: translateX(20px);
transform: skew(10deg);Shadows & Gradients
/* Box shadow */
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
/* Text shadow */
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
/* Linear gradient */
background: linear-gradient(90deg, #ff0000, #0000ff);
/* Radial gradient */
background: radial-gradient(circle, #ff0000, #0000ff);Pseudo-Classes
a:hover { color: red; } /* Mouse over */
input:focus { border: 2px solid blue; } /* Focused */
li:nth-child(2n) { background: #f0f0f0; } /* Even items */Use animations sparingly to avoid performance issues and accessibility concerns.
Top Interview Questions
Missing something?
Suggest a topic or concept to add to this tutorial. AI will review and expand the content.