What is CSS?
CSS — Cascading Style Sheets — is the language used to style HTML elements. It controls colors, fonts, spacing, layouts and animations.
/* selector { property: value; } */ h1 { color: crimson; font-size: 48px; font-weight: bold; } p { color: #666; line-height: 1.6; max-width: 600px; }
🎨 Styling
Colors, fonts, sizes, borders — every visual detail is controlled by CSS.
📐 Layout
Flexbox and Grid let you build any layout — from simple rows to complex dashboards.
✨ Animation
Transitions and keyframe animations bring your pages to life without JavaScript.
Selectors
Selectors are how CSS targets specific HTML elements. Choosing the right selector is the key to clean, maintainable styles.
/* Element selector — targets all <p> tags */ p { color: white; } /* Class selector — targets elements with class="card" */ .card { background: #222; border-radius: 10px; } /* ID selector — targets the element with id="header" */ #header { position: fixed; top: 0; } /* Descendant — targets <a> inside .navbar */ .navbar a { text-decoration: none; } /* Pseudo-class — styles element on hover */ button:hover { background: crimson; transform: scale(1.05); } /* Pseudo-element — styles first line of text */ p::first-line { font-weight: bold; }
The Box Model
Every HTML element is a rectangular box. The box model describes the layers around its content: padding, border, and margin.
LIVE DIAGRAM
.card { width: 300px; /* content width */ padding: 20px; /* space inside the border */ border: 2px solid #264de4; /* visible outline */ margin: 16px auto; /* space outside, auto=center */ box-sizing: border-box; /* padding included in width */ }
Colors & Typography
CSS supports multiple color formats and rich font control. Google Fonts gives you thousands of free typefaces.
/* Color formats */ .example { color: crimson; /* named */ background: #264de4; /* hex */ border-color: rgb(38,77,228); /* rgb */ box-shadow: 0 0 20px rgba(38,77,228, 0.4); } /* CSS Variables — define once, use everywhere */ :root { --primary: #264de4; --text: #e8e8f0; } h1 { color: var(--primary); } /* Typography */ body { font-family: 'Inter', sans-serif; font-size: 16px; line-height: 1.6; letter-spacing: 0.02em; }
COLOR PALETTE EXAMPLE
Flexbox
Flexbox is a one-dimensional layout system. It arranges items in a row or column and gives you powerful alignment control.
.container { display: flex; justify-content: space-between; /* horizontal alignment */ align-items: center; /* vertical alignment */ gap: 20px; /* space between items */ flex-wrap: wrap; /* wrap to next line */ } .item { flex: 1; /* grow equally */ }
FLEX DEMO — justify-content: space-between
CSS Grid
CSS Grid is a two-dimensional layout system — perfect for building complex page layouts with rows AND columns at the same time.
.grid { display: grid; grid-template-columns: repeat(3, 1fr); /* 3 equal columns */ gap: 20px; } /* Make an item span 2 columns */ .featured { grid-column: span 2; }
GRID DEMO — 3 columns
Transitions & Animations
CSS animations bring pages to life. Transitions handle simple state changes; @keyframes handle complex sequences.
.btn { background: #264de4; transition: all 0.3s ease; /* smooth change on hover */ } .btn:hover { background: #2965f1; transform: translateY(-3px); box-shadow: 0 8px 20px rgba(38,77,228, 0.4); }
@keyframes fadeIn { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } } .card { animation: fadeIn 0.6s ease forwards; }
Responsive Design
Media queries let you apply different styles based on screen size — making your site look great on phones, tablets and desktops.
/* Default — for all screens */ .grid { grid-template-columns: repeat(3, 1fr); } /* Tablet — screens up to 768px */ @media (max-width: 768px) { .grid { grid-template-columns: 1fr 1fr; } .navbar ul { display: none; } /* hide menu */ } /* Mobile — screens up to 480px */ @media (max-width: 480px) { .grid { grid-template-columns: 1fr; } h1 { font-size: 28px; } }
🎨 Project: Style the Profile Card
Take the HTML profile card from the previous lesson and make it look great with CSS. Here's a complete styled version:
* { margin: 0; padding: 0; box-sizing: border-box; } body { background: #0f0f0f; display: flex; justify-content: center; align-items: center; min-height: 100vh; font-family: Arial, sans-serif; } .card { background: #181818; border: 1px solid #264de4; border-radius: 16px; padding: 36px; text-align: center; width: 320px; box-shadow: 0 0 30px rgba(38,77,228, 0.2); animation: fadeIn 0.6s ease; }