Learn CSS with Real Code Examples
Updated Nov 21, 2025
Code Sample Descriptions
1
CSS Flexbox Centering
body, html {
height: 100%;
margin: 0;
}
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
background: #f0f0f0;
}
.box {
width: 200px;
height: 200px;
background: #007bff;
color: white;
display: flex;
justify-content: center;
align-items: center;
border-radius: 8px;
}
Centers content horizontally and vertically using Flexbox.
2
CSS Grid Gallery
.gallery {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 1rem;
padding: 1rem;
}
.gallery img {
width: 100%;
border-radius: 8px;
transition: transform 0.3s ease;
}
.gallery img:hover {
transform: scale(1.05);
}
Creates a responsive image gallery using CSS Grid.
3
CSS Variables and Theming
:root {
--primary-color: #ff6347;
--secondary-color: #4caf50;
}
body {
background: var(--secondary-color);
color: white;
font-family: sans-serif;
}
.button {
background: var(--primary-color);
padding: 0.5rem 1rem;
border: none;
border-radius: 4px;
color: white;
cursor: pointer;
transition: background 0.3s ease;
}
.button:hover {
background: darken(var(--primary-color), 10%);
}
Demonstrates using CSS custom properties for color theming.
4
CSS Card Hover Animation
.card {
background: white;
padding: 1rem;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.card:hover {
transform: translateY(-5px);
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
}
Adds a smooth scale-up effect on card hover using transitions.
5
CSS Button Ripple Effect
.ripple-button {
position: relative;
overflow: hidden;
padding: 1rem 2rem;
border: none;
border-radius: 8px;
background: #007bff;
color: white;
cursor: pointer;
}
.ripple-button::after {
content: '';
position: absolute;
width: 100%;
height: 100%;
background: rgba(255,255,255,0.3);
top: 0;
left: 0;
transform: scale(0);
border-radius: 50%;
transition: transform 0.5s, opacity 1s;
}
.ripple-button:active::after {
transform: scale(2);
opacity: 0;
transition: 0s;
}
Creates a ripple effect on button click using pseudo-elements and transitions.
6
CSS Responsive Typography
h1 {
font-size: clamp(1.5rem, 2vw + 1rem, 3rem);
}
p {
font-size: clamp(1rem, 1.5vw + 0.5rem, 1.5rem);
}
Adjusts font sizes dynamically using clamp() for responsive text.
7
CSS Flexbox Card Layout
.cards {
display: flex;
flex-wrap: wrap;
gap: 1rem;
justify-content: center;
}
.card {
flex: 1 1 200px;
background: white;
padding: 1rem;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
Arranges multiple cards in a responsive Flexbox layout.
8
CSS Animated Loader
.loader {
width: 50px;
height: 50px;
border: 5px solid #f3f3f3;
border-top: 5px solid #007bff;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
Creates a spinning loader using keyframes animation.
9
CSS Gradient Background
body {
background: linear-gradient(135deg, #007bff, #6c757d);
color: white;
font-family: sans-serif;
}
.radial-box {
width: 200px;
height: 200px;
background: radial-gradient(circle, #ff6347, #ffeb3b);
display: flex;
justify-content: center;
align-items: center;
border-radius: 50%;
}
Uses linear and radial gradients for backgrounds.
10
CSS Sticky Header
header {
position: sticky;
top: 0;
background: #007bff;
color: white;
padding: 1rem;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
z-index: 1000;
}
Demonstrates a sticky header that stays visible while scrolling.