01. 🎨 CSS Fundamentals & Syntax
CSS (Cascading Style Sheets) is the language used to style HTML documents. It controls the visual presentation of web pages, including layout, colors, fonts, and responsiveness.
What is CSS?
CSS stands for Cascading Style Sheets. It's called "cascading" because styles can be inherited and overridden in a hierarchical manner. CSS separates content (HTML) from presentation (styling), making websites more maintainable and accessible.
CSS Syntax Structure
- Selector: Targets HTML elements
- Declaration Block: Contains style rules
- Property: Aspect to style (color, font-size)
- Value: Specific styling instruction
- Declaration: Property-value pair
Example: Basic CSS Structure
/* This is a CSS comment */
/* Element selector */
p {
color: blue;
font-size: 16px;
margin: 10px 0;
}
/* Class selector */
.highlight {
background-color: yellow;
padding: 5px;
}
/* ID selector */
#main-header {
font-size: 24px;
color: #333;
}
/* Multiple selectors */
h1, h2, h3 {
font-family: 'Arial', sans-serif;
font-weight: bold;
}
Note: Always end declarations with a semicolon. The order of rules matters - later rules override earlier ones if they have the same specificity.
02. 🎯 CSS Selectors Mastery
Selectors are patterns used to select the elements you want to style. Understanding selectors is crucial for efficient CSS.
Types of Selectors
- Basic: Element, Class (.), ID (#)
- Attribute: [attribute], [attribute=value]
- Pseudo-classes: :hover, :focus, :nth-child()
- Pseudo-elements:: ::before, ::after, ::first-letter
- Combinators: Descendant, Child (>), Adjacent (+), General (~)
Selector Specificity
When multiple rules apply to an element, CSS uses specificity to determine which rule wins:
1. Inline styles: 1000 points
2. ID selectors: 100 points
3. Class/attribute/pseudo-class: 10 points
4. Element/pseudo-element: 1 point
Example: Advanced Selectors
/* Universal selector */
* {
box-sizing: border-box;
}
/* Attribute selector */
input[type="text"] {
border: 2px solid #ccc;
}
a[href^="https"] {
color: green;
}
/* Child selector */
nav > ul > li {
display: inline-block;
}
/* Adjacent sibling selector */
h2 + p {
margin-top: 0;
}
/* Pseudo-classes */
a:hover {
text-decoration: underline;
color: red;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
/* Pseudo-elements */
p::first-letter {
font-size: 200%;
color: #8A2BE2;
}
.blockquote::before {
content: "“";
font-size: 3em;
color: #ccc;
}
Regular paragraph (blue)
Paragraph with class (green - higher specificity)
Paragraph with ID (purple - highest specificity)
03. 📦 CSS Box Model Deep Dive
The CSS Box Model is fundamental to understanding layout. Every element is represented as a rectangular box with content, padding, border, and margin.
Box Model Visualization
Box Model Components
- Content: Actual content (text, images)
- Padding: Space between content and border
- Border: Edge around padding
- Margin: Space outside border
box-sizing: border-box to make width/height include padding and border. This makes layout calculations much easier!
Example: Box Model Properties
/* Traditional box model */
.box-traditional {
width: 200px;
padding: 20px;
border: 5px solid black;
margin: 10px;
/* Total width: 200 + 20*2 + 5*2 + 10*2 = 270px */
}
/* Border-box model (recommended) */
.box-border-box {
width: 200px;
padding: 20px;
border: 5px solid black;
margin: 10px;
box-sizing: border-box;
/* Total width: 200px (includes padding and border) */
}
/* Shorthand properties */
.element {
/* margin: top right bottom left */
margin: 10px 20px 15px 5px;
/* margin: vertical horizontal */
margin: 20px 10px;
/* margin: all sides */
margin: 15px;
}
/* Individual properties */
.detailed-box {
margin-top: 10px;
margin-right: 15px;
margin-bottom: 20px;
margin-left: 5px;
padding-top: 5px;
padding-right: 10px;
padding-bottom: 15px;
padding-left: 20px;
border-width: 2px;
border-style: solid;
border-color: #333;
/* Border shorthand */
border: 2px dashed #ff0000;
}
Actual: 270px
Actual: 200px
04. 🔠 Typography & Text Styling
Typography is crucial for readability and user experience. CSS provides extensive control over text appearance and layout.
Font Stack Strategy
Always provide fallback fonts in your font stack. The browser will use the first available font in the list.
Key Typography Properties
- Font Family: Typeface selection
- Font Size: Text size in px, em, rem, %
- Font Weight: Boldness (100-900)
- Line Height: Space between lines
- Letter Spacing: Space between characters
- Text Align: Horizontal alignment
- Text Decoration: Underlines, overlines
- Text Transform: Uppercase, lowercase, capitalize
Example: Advanced Typography
/* Font families with fallbacks */
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.heading {
font-family: 'Roboto', 'Helvetica Neue', Arial, sans-serif;
}
/* Responsive font sizes */
html {
font-size: 16px; /* Base font size */
}
h1 {
font-size: 2rem; /* 32px if html is 16px */
}
p {
font-size: 1rem; /* 16px */
line-height: 1.6; /* Unitless - multiplies font size */
}
/* Text properties */
.fancy-text {
font-weight: 700; /* Bold */
font-style: italic;
text-transform: uppercase;
letter-spacing: 2px;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
text-align: center;
}
/* Google Fonts implementation */
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;700&display=swap');
.google-font {
font-family: 'Roboto', sans-serif;
font-weight: 300; /* Light */
}
/* Text overflow handling */
.truncate {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
max-width: 200px;
}
Beautiful Typography
05. 🎨 Colors, Gradients & Backgrounds
Color is one of the most important aspects of web design. CSS offers various ways to specify colors and create complex backgrounds.
Color Notation Methods
- Keywords: red, blue, transparent
- Hexadecimal: #RRGGBB or #RGB
- RGB/RGBA: rgb(255,0,0) or rgba(255,0,0,0.5)
- HSL/HSLA: hsl(0,100%,50%) or hsla(0,100%,50%,0.5)
- CurrentColor: Uses current text color
Gradients
CSS gradients let you display smooth transitions between two or more colors. They're created using the background-image property.
Example: Colors and Backgrounds
/* Different color notations */
.color-examples {
color: red; /* Keyword */
color: #ff0000; /* Hex */
color: #f00; /* Short hex */
color: rgb(255, 0, 0); /* RGB */
color: rgba(255, 0, 0, 0.5); /* RGB with opacity */
color: hsl(0, 100%, 50%); /* HSL */
color: hsla(0, 100%, 50%, 0.5); /* HSL with opacity */
}
/* Background properties */
.background-example {
background-color: #f0f0f0;
background-image: url('pattern.png');
background-repeat: no-repeat;
background-position: center;
background-size: cover;
background-attachment: fixed; /* Parallax effect */
/* Background shorthand */
background: #f0f0f0 url('pattern.png') no-repeat center / cover fixed;
}
/* Linear gradient */
.gradient-linear {
background: linear-gradient(to right, #ff7e5f, #feb47b);
background: linear-gradient(45deg, #ff7e5f, #feb47b); /* Angle */
background: linear-gradient(to right, red, yellow 30%, blue); /* Color stops */
}
/* Radial gradient */
.gradient-radial {
background: radial-gradient(circle, #ff7e5f, #feb47b);
}
/* Multiple backgrounds */
.fancy-background {
background:
linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)),
url('hero-image.jpg') center/cover;
}
/* Background blend modes */
.blend-mode {
background-image: url('pattern.jpg'), linear-gradient(blue, red);
background-blend-mode: multiply;
}
06. 🏗️ Layout Fundamentals
CSS provides several layout models for positioning elements. Understanding these is key to creating complex page structures.
Display Property Values
- block: Takes full width, starts new line
- inline: Flows with text, width determined by content
- inline-block: Like inline but respects width/height
- none: Element not rendered
- flex: Flexbox layout
- grid: CSS Grid layout
Position Property
Controls how elements are positioned in the document flow:
static: Default, normal flow
relative: Positioned relative to itself
absolute: Positioned relative to nearest positioned ancestor
fixed: Positioned relative to viewport
sticky: Hybrid of relative and fixed
Example: Layout Techniques
/* Display property examples */
.block-element {
display: block;
width: 100%;
background: lightblue;
padding: 10px;
margin: 10px 0;
}
.inline-element {
display: inline;
background: lightgreen;
padding: 0 5px;
}
.inline-block-element {
display: inline-block;
width: 100px;
height: 100px;
background: lightcoral;
margin: 5px;
}
/* Positioning examples */
.relative-box {
position: relative;
top: 20px;
left: 20px;
background: lightyellow;
}
.absolute-box {
position: absolute;
top: 0;
right: 0;
background: lightpink;
}
.fixed-header {
position: fixed;
top: 0;
left: 0;
width: 100%;
background: white;
z-index: 1000;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.sticky-nav {
position: sticky;
top: 0;
background: #333;
color: white;
padding: 10px;
}
/* Float layout (legacy but still useful) */
.float-left {
float: left;
width: 50%;
background: lightblue;
}
.float-right {
float: right;
width: 50%;
background: lightgreen;
}
.clearfix::after {
content: "";
display: table;
clear: both;
}
07. 📐 Flexbox Layout Mastery
Flexbox is a one-dimensional layout model that offers space distribution between items and powerful alignment capabilities.
Flexbox Container
Flex Container Properties
- display: flex: Creates flex container
- flex-direction: row, row-reverse, column, column-reverse
- justify-content: Main axis alignment
- align-items: Cross axis alignment
- flex-wrap: nowrap, wrap, wrap-reverse
- gap: Space between items
Example: Flexbox Layouts
/* Basic flex container */
.flex-container {
display: flex;
flex-direction: row; /* Default */
justify-content: flex-start; /* Main axis alignment */
align-items: stretch; /* Cross axis alignment */
flex-wrap: nowrap; /* Default */
gap: 10px; /* Space between items */
}
/* Common flex patterns */
.center-all {
display: flex;
justify-content: center;
align-items: center;
height: 100vh; /* Full viewport height */
}
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem 2rem;
background: #333;
color: white;
}
.card-grid {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.card {
flex: 1 1 300px; /* flex-grow flex-shrink flex-basis */
min-width: 0; /* Fixes flex item minimum width issue */
}
/* Flex item properties */
.flex-item {
/* flex: flex-grow flex-shrink flex-basis */
flex: 1 0 auto;
/* Individual properties */
flex-grow: 0; /* Can item grow? */
flex-shrink: 1; /* Can item shrink? */
flex-basis: auto; /* Initial size */
/* Self alignment */
align-self: center;
order: 2; /* Display order (default 0) */
}
/* Responsive navigation */
.nav-menu {
display: flex;
flex-direction: column;
gap: 10px;
}
@media (min-width: 768px) {
.nav-menu {
flex-direction: row;
justify-content: space-around;
}
}
08. 🔲 CSS Grid Layout System
CSS Grid is a two-dimensional layout system that allows you to create complex layouts with rows and columns.
Grid Container
Grid Container Properties
- display: grid: Creates grid container
- grid-template-columns: Defines column tracks
- grid-template-rows: Defines row tracks
- gap: grid-gap (row-gap, column-gap)
- grid-template-areas: Named grid areas
- justify-items: Row axis alignment
- align-items: Column axis alignment
fr Unit
The fr unit represents a fraction of the available space in the grid container. It's specifically designed for CSS Grid and makes responsive layouts much easier.
Example: Grid Layouts
/* Basic grid */
.grid-container {
display: grid;
grid-template-columns: 1fr 2fr 1fr; /* Three columns */
grid-template-rows: 100px auto 100px; /* Three rows */
gap: 20px; /* Space between items */
}
/* Common grid patterns */
.three-column-layout {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
.responsive-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
}
/* Named grid areas */
.page-layout {
display: grid;
grid-template-areas:
"header header header"
"sidebar main main"
"footer footer footer";
grid-template-columns: 200px 1fr 1fr;
grid-template-rows: auto 1fr auto;
min-height: 100vh;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }
/* Grid item placement */
.grid-item {
/* Line-based placement */
grid-column-start: 1;
grid-column-end: 3;
grid-row-start: 1;
grid-row-end: 2;
/* Shorthand */
grid-column: 1 / 3;
grid-row: 1 / 2;
/* Area placement */
grid-area: 1 / 1 / 2 / 3;
}
/* Advanced grid features */
.advanced-grid {
display: grid;
grid-template-columns: [sidebar-start] 200px [sidebar-end main-start] 1fr [main-end];
grid-template-rows: [header-start] 80px [header-end content-start] auto [content-end footer-start] 60px [footer-end];
gap: 20px;
min-height: 100vh;
}
.header {
grid-column: sidebar-start / main-end;
grid-row: header-start / header-end;
}
Grid Features Demonstrated:
- Equal width columns with
1fr - Item spanning multiple columns
- Consistent gaps between items
09. 📱 Responsive Design Techniques
Responsive web design ensures your website looks great on all devices, from mobile phones to desktop computers.
Mobile-First Approach
Start with mobile styles as your base, then use media queries to add styles for larger screens. This approach results in cleaner, more performant code.
Key Responsive Techniques
- Media Queries: Conditionally apply CSS
- Relative Units: em, rem, %, vw, vh
- Flexible Images: max-width: 100%
- Viewport Meta Tag: Controls mobile viewport
- CSS Grid & Flexbox: Intrinsically responsive
Example: Responsive Design Patterns
/* Viewport meta tag (in HTML head) */
<meta name="viewport" content="width=device-width, initial-scale=1.0">
/* Mobile-first base styles */
.container {
padding: 1rem;
max-width: 100%;
}
/* Media queries for larger screens */
/* Small tablets */
@media (min-width: 576px) {
.container {
padding: 1.5rem;
}
.grid {
grid-template-columns: repeat(2, 1fr);
}
}
/* Tablets */
@media (min-width: 768px) {
.container {
max-width: 720px;
margin: 0 auto;
}
.grid {
grid-template-columns: repeat(3, 1fr);
}
.navbar {
flex-direction: row;
}
}
/* Desktop */
@media (min-width: 992px) {
.container {
max-width: 960px;
}
.grid {
grid-template-columns: repeat(4, 1fr);
}
}
/* Large desktop */
@media (min-width: 1200px) {
.container {
max-width: 1140px;
}
}
/* Common breakpoints (can vary based on project) */
/*
xs: < 576px (extra small)
sm: ≥ 576px (small)
md: ≥ 768px (medium)
lg: ≥ 992px (large)
xl: ≥ 1200px (extra large)
xxl: ≥ 1400px
*/
/* Responsive typography */
html {
font-size: 14px; /* Base for mobile */
}
@media (min-width: 768px) {
html {
font-size: 16px; /* Base for larger screens */
}
}
h1 {
font-size: 2rem; /* Scales with base font */
}
/* Responsive images */
img {
max-width: 100%;
height: auto; /* Maintain aspect ratio */
}
/* Picture element for art direction */
<picture>
<source media="(min-width: 1200px)" srcset="large.jpg">
<source media="(min-width: 768px)" srcset="medium.jpg">
<img src="small.jpg" alt="Description">
</picture>
/* Hide/show elements based on screen size */
.mobile-only {
display: block;
}
.desktop-only {
display: none;
}
@media (min-width: 768px) {
.mobile-only {
display: none;
}
.desktop-only {
display: block;
}
}
10. ✨ Transitions & Animations
CSS animations and transitions bring websites to life with smooth motion and interactive feedback.
Transitions vs Animations
- Transitions: Simple state changes (hover, focus)
- Animations: Complex, multi-step sequences
Performance Considerations
For smooth animations, use properties that the browser can optimize:
✅ Good: transform, opacity
❌ Avoid: width, height, margin, padding
Example: Animations & Transitions
/* CSS Transitions */
.button {
background: #3498db;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
transition: all 0.3s ease;
/* transition: property duration timing-function delay; */
}
.button:hover {
background: #2980b9;
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(0,0,0,0.3);
}
/* Multiple transitions */
.card {
transition: transform 0.3s ease, opacity 0.5s ease-in;
}
.card:hover {
transform: scale(1.05);
opacity: 0.9;
}
/* CSS Animations */
@keyframes slideIn {
from {
transform: translateX(-100%);
opacity: 0;
}
to {
transform: translateX(0);
opacity: 1;
}
}
/* Multiple keyframes */
@keyframes bounce {
0%, 20%, 50%, 80%, 100% {
transform: translateY(0);
}
40% {
transform: translateY(-30px);
}
60% {
transform: translateY(-15px);
}
}
.animated-element {
animation: slideIn 1s ease-out;
/* animation: name duration timing-function delay iteration-count direction fill-mode; */
}
.bouncing-element {
animation: bounce 2s infinite;
}
/* Animation properties */
.advanced-animation {
animation-name: slideIn;
animation-duration: 1s;
animation-timing-function: ease-in-out;
animation-delay: 0.5s;
animation-iteration-count: 2;
animation-direction: alternate;
animation-fill-mode: both;
animation-play-state: running;
}
/* 3D transforms */
.three-d-card {
transition: transform 0.5s;
}
.three-d-card:hover {
transform: perspective(1000px) rotateY(20deg) scale(1.05);
}
/* Loading animations */
.loading-spinner {
width: 50px;
height: 50px;
border: 5px solid #f3f3f3;
border-top: 5px solid #3498db;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
Try hovering over the first box!
Notice how each animation uses different CSS properties:
- Transition: Smooth state change on hover
- Animation: Continuous motion with keyframes
- Transform: Position and rotation changes
- Color: Smooth color transitions
Codcups
Master CSS - Complete Styling Course