What You'll Learn
Introduction to CSS
CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of HTML documents. It controls colors, fonts, layouts, spacing, and more!
💡 What is CSS?
CSS is the language that makes websites beautiful! While HTML provides structure, CSS provides style. Think of HTML as the skeleton and CSS as the skin, clothes, and makeup!
- 🎨 Create beautiful designs
- 📱 Build responsive websites
- âš¡ Add animations and effects
- 🎯 Control every visual aspect
- 🚀 Essential for web development
Three Ways to Add CSS
Inline: Direct in HTML element
Internal: In <style> tag in <head>
External: Separate .css file (recommended)
<!-- 1. Inline CSS --> <h1 style="color: blue; font-size: 32px;">Hello</h1> <!-- 2. Internal CSS --> <head> <style> h1 { color: blue; font-size: 32px; } </style> </head> <!-- 3. External CSS (Best Practice) --> <head> <link rel="stylesheet" href="style.css"> </head>
Syntax & Selectors
CSS Syntax
/* CSS Syntax Structure */ selector { property: value; property: value; } /* Example */ h1 { color: blue; font-size: 24px; text-align: center; }
CSS Selectors
/* 1. Universal Selector */ * { margin: 0; padding: 0; } /* 2. Element Selector */ p { color: black; } /* 3. Class Selector */ .container { width: 100%; } /* 4. ID Selector */ #header { background: blue; } /* 5. Multiple Selectors */ h1, h2, h3 { font-family: 'Arial'; } /* 6. Descendant Selector */ div p { color: red; } /* 7. Child Selector */ div > p { color: green; } /* 8. Adjacent Sibling Selector */ h1 + p { margin-top: 0; } /* 9. Attribute Selector */ input[type="text"] { border: 1px solid gray; } /* 10. Pseudo-classes */ a:hover { color: red; } input:focus { outline: 2px solid blue; } li:first-child { font-weight: bold; } li:nth-child(2n) { background: #f0f0f0; } /* 11. Pseudo-elements */ p::first-line { font-weight: bold; } p::before { content: "📌 "; } p::after { content: " ✓"; }
💡 Selector Specificity
When multiple rules apply, CSS uses specificity to determine which rule wins:
Inline styles (1000) > IDs (100) > Classes (10) > Elements (1)
Colors & Backgrounds
Color Properties
/* Color Formats */ /* Named Colors */ h1 { color: red; } /* Hexadecimal */ h2 { color: #ff0000; color: #f00; /* Shorthand */ } /* RGB */ h3 { color: rgb(255, 0, 0); } /* RGBA (with transparency) */ h4 { color: rgba(255, 0, 0, 0.5); } /* HSL (Hue, Saturation, Lightness) */ h5 { color: hsl(0, 100%, 50%); } /* HSLA (with transparency) */ h6 { color: hsla(0, 100%, 50%, 0.5); }
Background Properties
/* Background Color */ .box { background-color: #f0f0f0; } /* Background Image */ .hero { background-image: url('image.jpg'); background-size: cover; background-position: center; background-repeat: no-repeat; background-attachment: fixed; } /* Shorthand */ .banner { background: #fff url('bg.jpg') center/cover no-repeat; } /* Gradient Backgrounds */ .gradient-linear { background: linear-gradient(to right, #667eea, #764ba2); } .gradient-radial { background: radial-gradient(circle, #667eea, #764ba2); } .gradient-multi { background: linear-gradient( 135deg, #667eea 0%, #764ba2 50%, #f093fb 100% ); } /* Multiple Backgrounds */ .multi-bg { background: url('pattern.png'), linear-gradient(to bottom, #fff, #f0f0f0); }
Text & Fonts
Text Properties
/* Text Styling */ p { /* Color */ color: #333; /* Alignment */ text-align: left; /* left, right, center, justify */ /* Decoration */ text-decoration: none; /* none, underline, overline, line-through */ /* Transform */ text-transform: capitalize; /* uppercase, lowercase, capitalize */ /* Spacing */ letter-spacing: 2px; word-spacing: 5px; line-height: 1.6; /* Indent */ text-indent: 20px; /* Shadow */ text-shadow: 2px 2px 4px rgba(0,0,0,0.3); /* Overflow */ white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
Font Properties
/* Font Properties */ body { /* Font Family */ font-family: 'Arial', sans-serif; /* Font Size */ font-size: 16px; /* px, em, rem, %, vw */ /* Font Weight */ font-weight: normal; /* normal, bold, 100-900 */ /* Font Style */ font-style: normal; /* normal, italic, oblique */ } /* Google Fonts */ @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap'); h1 { font-family: 'Poppins', sans-serif; font-weight: 700; } /* Custom Fonts (@font-face) */ @font-face { font-family: 'MyFont'; src: url('myfont.woff2') format('woff2'); font-weight: normal; font-style: normal; } /* Font Shorthand */ p { font: italic 600 18px/1.6 'Arial', sans-serif; /* style weight size/line-height family */ }
✅ Font Size Units
px - Fixed pixels
em - Relative to parent font size
rem - Relative to root font size (recommended)
% - Percentage of parent
vw/vh - Viewport width/height
Box Model
Understanding the Box Model
Every HTML element is a rectangular box consisting of: Content → Padding → Border → Margin
/* Box Model Properties */ .box { /* Width & Height */ width: 300px; height: 200px; /* Padding (space inside) */ padding: 20px; /* all sides */ padding: 10px 20px; /* top/bottom left/right */ padding: 10px 15px 20px 25px; /* top right bottom left */ /* Individual padding */ padding-top: 10px; padding-right: 20px; padding-bottom: 10px; padding-left: 20px; /* Border */ border: 2px solid #333; border-width: 2px; border-style: solid; /* solid, dashed, dotted, double */ border-color: #333; border-radius: 10px; /* rounded corners */ /* Margin (space outside) */ margin: 20px; margin: 10px auto; /* center horizontally */ /* Individual margins */ margin-top: 10px; margin-right: 20px; margin-bottom: 10px; margin-left: 20px; } /* Box Sizing */ * { box-sizing: border-box; /* includes padding & border in width */ } /* Display */ .block { display: block; } .inline { display: inline; } .inline-block { display: inline-block; } .none { display: none; } /* Overflow */ .overflow-box { overflow: hidden; /* hidden, scroll, auto, visible */ overflow-x: auto; overflow-y: scroll; }
💡 Box Sizing
Always use box-sizing: border-box; on all elements. This makes width/height
calculations easier as padding and border are included in the total width!
Flexbox Layout
Flexbox - Modern Layout System
Flexbox makes it easy to create flexible, responsive layouts without floats or positioning!
/* Flex Container */ .container { display: flex; /* Direction */ flex-direction: row; /* row, row-reverse, column, column-reverse */ /* Wrap */ flex-wrap: wrap; /* nowrap, wrap, wrap-reverse */ /* Main Axis Alignment (horizontal for row) */ justify-content: center; /* flex-start, flex-end, center, space-between, space-around, space-evenly */ /* Cross Axis Alignment (vertical for row) */ align-items: center; /* flex-start, flex-end, center, stretch, baseline */ /* Multi-line Alignment */ align-content: space-between; /* Gap between items */ gap: 20px; row-gap: 20px; column-gap: 20px; } /* Flex Items */ .item { /* Grow */ flex-grow: 1; /* how much item will grow */ /* Shrink */ flex-shrink: 1; /* how much item will shrink */ /* Basis (initial size) */ flex-basis: 200px; /* Shorthand */ flex: 1 1 200px; /* grow shrink basis */ flex: 1; /* Common: grow 1, shrink 1, basis auto */ /* Individual Alignment */ align-self: flex-end; /* Order */ order: 2; /* default is 0 */ } /* Common Patterns */ /* Center Everything */ .center-all { display: flex; justify-content: center; align-items: center; } /* Navigation Bar */ .navbar { display: flex; justify-content: space-between; align-items: center; } /* Equal Width Columns */ .columns { display: flex; gap: 20px; } .column { flex: 1; }
Flexbox Visual Demo
Grid Layout
CSS Grid - 2D Layout System
CSS Grid is the most powerful layout system. Perfect for creating complex 2D layouts!
/* Grid Container */ .grid-container { display: grid; /* Define Columns */ grid-template-columns: 200px 200px 200px; grid-template-columns: 1fr 1fr 1fr; /* flexible */ grid-template-columns: repeat(3, 1fr); /* shorthand */ grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); /* responsive */ /* Define Rows */ grid-template-rows: 100px 200px 100px; grid-template-rows: repeat(3, 150px); /* Gap */ gap: 20px; row-gap: 20px; column-gap: 20px; /* Alignment */ justify-items: center; /* align items horizontally */ align-items: center; /* align items vertically */ justify-content: center; /* align grid horizontally */ align-content: center; /* align grid vertically */ } /* Grid Items */ .grid-item { /* Span columns */ grid-column: 1 / 3; /* from column 1 to 3 */ grid-column: span 2; /* span 2 columns */ /* Span rows */ grid-row: 1 / 3; grid-row: span 2; /* Shorthand */ grid-area: 1 / 1 / 3 / 3; /* row-start / col-start / row-end / col-end */ /* Individual Alignment */ justify-self: end; align-self: start; } /* Grid Template Areas */ .layout { display: grid; grid-template-columns: 200px 1fr 200px; grid-template-rows: 80px 1fr 60px; grid-template-areas: "header header header" "sidebar main aside" "footer footer footer"; gap: 20px; } .header { grid-area: header; } .sidebar { grid-area: sidebar; } .main { grid-area: main; } .aside { grid-area: aside; } .footer { grid-area: footer; } /* Responsive Grid */ .responsive-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 20px; }
Grid Visual Demo
Positioning
CSS Positioning
/* Static (default) */ .static { position: static; } /* Relative - relative to its normal position */ .relative { position: relative; top: 20px; left: 30px; } /* Absolute - relative to nearest positioned parent */ .absolute { position: absolute; top: 0; right: 0; } /* Fixed - relative to viewport */ .fixed { position: fixed; bottom: 20px; right: 20px; } /* Sticky - toggles between relative and fixed */ .sticky { position: sticky; top: 0; } /* Z-Index (stacking order) */ .layer-1 { position: relative; z-index: 1; } .layer-10 { position: relative; z-index: 10; /* appears on top */ } /* Center with Absolute */ .center-absolute { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
💡 Positioning Types
static: Normal flow (default)
relative: Relative to normal position
absolute: Relative to positioned parent
fixed: Relative to viewport (stays on scroll)
sticky: Relative until scroll threshold, then fixed
Responsive Design
Media Queries
/* Mobile First Approach (Recommended) */ /* Base styles (mobile) */ .container { width: 100%; padding: 15px; } /* Tablet (768px and up) */ @media (min-width: 768px) { .container { width: 750px; margin: 0 auto; } } /* Desktop (1024px and up) */ @media (min-width: 1024px) { .container { width: 960px; } } /* Large Desktop (1280px and up) */ @media (min-width: 1280px) { .container { width: 1200px; } } /* Desktop First Approach */ @media (max-width: 768px) { /* Tablet and mobile styles */ } /* Specific Range */ @media (min-width: 768px) and (max-width: 1024px) { /* Tablet only */ } /* Orientation */ @media (orientation: landscape) { /* Landscape mode */ } @media (orientation: portrait) { /* Portrait mode */ } /* Print Styles */ @media print { .no-print { display: none; } } /* Common Breakpoints */ /* Mobile: 320px - 767px Tablet: 768px - 1023px Desktop: 1024px - 1279px Large: 1280px+ */
✅ Responsive Best Practices
✓ Use mobile-first approach
✓ Use relative units (rem, em, %, vw, vh)
✓ Make images responsive: max-width: 100%; height: auto;
✓ Use Flexbox/Grid for layouts
✓ Test on real devices
✓ Add viewport meta tag in HTML
Animations & Transitions
CSS Transitions
/* Basic Transition */ .button { background: blue; transition: background 0.3s ease; } .button:hover { background: darkblue; } /* Multiple Properties */ .box { transition: background 0.3s ease, transform 0.3s ease, opacity 0.3s ease; } /* All Properties */ .box { transition: all 0.3s ease; } /* Timing Functions */ .ease { transition: all 0.3s ease; } .linear { transition: all 0.3s linear; } .ease-in { transition: all 0.3s ease-in; } .ease-out { transition: all 0.3s ease-out; } .ease-in-out { transition: all 0.3s ease-in-out; } /* Custom Cubic Bezier */ .custom { transition: all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55); } /* Delay */ .delayed { transition: all 0.3s ease 0.1s; /* 0.1s delay */ }
CSS Animations
/* Define Keyframes */ @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } } /* Percentage Keyframes */ @keyframes slide { 0% { transform: translateX(0); } 50% { transform: translateX(100px); } 100% { transform: translateX(0); } } /* Apply Animation */ .animated { animation-name: fadeIn; animation-duration: 1s; animation-timing-function: ease; animation-delay: 0s; animation-iteration-count: infinite; /* or number */ animation-direction: normal; /* normal, reverse, alternate */ animation-fill-mode: forwards; /* none, forwards, backwards, both */ animation-play-state: running; /* running, paused */ } /* Shorthand */ .box { animation: fadeIn 1s ease 0s infinite normal forwards; } /* Common Animations */ /* Spin */ @keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } /* Bounce */ @keyframes bounce { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-20px); } } /* Pulse */ @keyframes pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.1); } } /* Shake */ @keyframes shake { 0%, 100% { transform: translateX(0); } 25% { transform: translateX(-10px); } 75% { transform: translateX(10px); } }
Animation Demo
Transforms
/* 2D Transforms */ /* Translate (Move) */ .translate { transform: translateX(50px); transform: translateY(50px); transform: translate(50px, 50px); } /* Scale */ .scale { transform: scale(1.5); /* 150% */ transform: scale(2, 1); /* width, height */ } /* Rotate */ .rotate { transform: rotate(45deg); } /* Skew */ .skew { transform: skewX(20deg); transform: skewY(20deg); transform: skew(20deg, 10deg); } /* Multiple Transforms */ .combined { transform: translate(50px, 50px) rotate(45deg) scale(1.2); } /* Transform Origin */ .origin { transform-origin: center center; /* default */ transform-origin: top left; transform-origin: 50% 50%; } /* 3D Transforms */ .perspective { perspective: 1000px; } .rotate3d { transform: rotateX(45deg); transform: rotateY(45deg); transform: rotateZ(45deg); } .translate3d { transform: translate3d(50px, 50px, 50px); }
Advanced CSS
CSS Variables (Custom Properties)
/* Define Variables */ :root { /* Colors */ --primary-color: #667eea; --secondary-color: #764ba2; --text-color: #333; /* Spacing */ --spacing-sm: 8px; --spacing-md: 16px; --spacing-lg: 24px; /* Fonts */ --font-primary: 'Inter', sans-serif; --font-size-base: 16px; } /* Use Variables */ body { color: var(--text-color); font-family: var(--font-primary); } .button { background: var(--primary-color); padding: var(--spacing-md); } /* Fallback Value */ .box { color: var(--undefined-color, black); } /* Override in Specific Context */ .dark-theme { --primary-color: #ff6b6b; --text-color: #fff; }
CSS Filters & Effects
/* CSS Filters */ img { /* Blur */ filter: blur(5px); /* Brightness */ filter: brightness(150%); /* Contrast */ filter: contrast(200%); /* Grayscale */ filter: grayscale(100%); /* Hue Rotate */ filter: hue-rotate(90deg); /* Invert */ filter: invert(100%); /* Saturate */ filter: saturate(200%); /* Sepia */ filter: sepia(100%); /* Drop Shadow */ filter: drop-shadow(0 4px 6px rgba(0,0,0,0.3)); /* Multiple Filters */ filter: brightness(110%) contrast(120%) saturate(130%); } /* Backdrop Filter (blur background) */ .glass { background: rgba(255, 255, 255, 0.1); backdrop-filter: blur(10px); border: 1px solid rgba(255, 255, 255, 0.2); } /* Box Shadow */ .shadow { box-shadow: 0 4px 6px rgba(0,0,0,0.1); /* Multiple Shadows */ box-shadow: 0 2px 4px rgba(0,0,0,0.1), 0 8px 16px rgba(0,0,0,0.1); /* Inset Shadow */ box-shadow: inset 0 2px 4px rgba(0,0,0,0.1); }
Best Practices
CSS Best Practices
Use External Stylesheets
Keep CSS in separate .css files for better organization and caching
Use CSS Reset or Normalize
Ensure consistent rendering across browsers
Follow BEM Naming Convention
Block__Element--Modifier for clear, maintainable class names
Use CSS Variables
Define reusable values for colors, spacing, and fonts
Mobile-First Approach
Design for mobile first, then add complexity for larger screens
Avoid !important
Use proper specificity instead of forcing styles with !important
Use Flexbox and Grid
Modern layout systems are more powerful than floats and positioning
Optimize Performance
Minify CSS, reduce selector complexity, use efficient selectors
/* BEM Naming Convention */ .card { } /* Block */ .card__title { } /* Element */ .card__image { } /* Element */ .card--featured { } /* Modifier */ /* Good Selector Practice */ /* ✓ GOOD - Low specificity, reusable */ .button { } .button-primary { } /* ✗ BAD - Overly specific */ div.container section.main article.post p.text span.highlight { } /* Performance Tips */ /* ✓ GOOD - Efficient */ .menu-item { } /* ✗ BAD - Inefficient */ * html body div#container ul.menu li.item a { } /* Organize CSS */ /* 1. Variables 2. Reset/Normalize 3. Base styles 4. Layout 5. Components 6. Utilities 7. Media queries */
✅ CSS Organization Checklist
✓ Use meaningful class names
✓ Group related styles together
✓ Add comments for complex code
✓ Keep specificity low
✓ Use shorthand properties when possible
✓ Validate your CSS
✓ Test across browsers
✓ Use a CSS preprocessor (Sass/Less) for large projects
🎉 Congratulations!
You've mastered CSS from zero to hero! You now have the skills to create beautiful, responsive, and modern web designs. Keep practicing and building amazing projects!