Week 7: Responsive Design + Bootstrap 5
CSS | Media Queries, Bootstrap Grid, Components
- التصميم المتجاوب و Bootstrap 5 — شرح عملي — Mobile-First و media queries بثلاث نقاط توقف، الصور المتجاوبة و picture و clamp، ثم صفحة المعرض بـ Bootstrap: الأعمدة والمكونات والأصناف المساعدة والتخصيص · 8 دقائق
Al-Aqsa University
Responsive Design + Bootstrap 5
Media Queries, Mobile-First, and the Bootstrap Framework
Week 07
1. What is Responsive Design? Adapting to All Screens
Responsive Web Design
Responsive design means your website looks and works great on all devices - phones, tablets, laptops, and large monitors. Instead of building separate sites for each device, you build ONE site that adapts.
- Fluid layouts: Use percentages and relative units instead of fixed pixels
- Flexible images: Images scale with their container
- Media queries: Apply different CSS rules at different screen sizes
- Mobile-first: Design for mobile first, then enhance for larger screens
The Viewport Meta Tag
This tag is required for responsive design. Without it, mobile browsers zoom out to show the desktop version.
HTML - Viewport Meta Tag
<!-- ALWAYS include this in <head> -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- width=device-width: match screen width -->
<!-- initial-scale=1.0: no zoom on load -->
Mobile-First Approach
Write CSS for mobile screens first (default styles), then use min-width media queries to add styles for larger screens.
CSS - Mobile-First
/* Mobile-First: base styles are for mobile */
.card-grid {
display: grid;
grid-template-columns: 1fr; /* 1 column on mobile */
gap: 20px;
}
/* Tablet and up */
@media (min-width: 768px) {
.card-grid {
grid-template-columns: repeat(2, 1fr); /* 2 columns */
}
}
/* Desktop and up */
@media (min-width: 1024px) {
.card-grid {
grid-template-columns: repeat(3, 1fr); /* 3 columns */
}
}
Why Mobile-First?
Mobile-first means simpler base styles (mobile is simpler), better performance on mobile (less CSS to parse), and forces you to prioritize content. It uses min-width (from small to large), while desktop-first uses max-width (from large to small).
2. Media Queries Conditional CSS
Media Query Syntax
CSS - Media Queries
/* Basic syntax */
@media (condition) {
/* CSS rules here only apply when condition is true */
}
/* min-width: applies at this width AND ABOVE */
@media (min-width: 768px) {
.sidebar { display: block; }
}
/* max-width: applies at this width AND BELOW */
@media (max-width: 767px) {
.sidebar { display: none; }
}
/* Range: between two widths */
@media (min-width: 768px) and (max-width: 1023px) {
.container { max-width: 720px; }
}
/* Orientation */
@media (orientation: landscape) {
.hero { min-height: 60vh; }
}
@media (orientation: portrait) {
.hero { min-height: 100vh; }
}
/* Combine conditions with "and" */
@media (min-width: 768px) and (orientation: landscape) {
/* tablet in landscape */
}
/* Dark mode preference */
@media (prefers-color-scheme: dark) {
body { background: #0f0f1a; color: #e0e0e0; }
}
Common Breakpoints
| Breakpoint | Device | Bootstrap Name |
< 576px | Extra small phones | xs (default) |
≥ 576px | Small phones (landscape) | sm |
≥ 768px | Tablets | md |
≥ 992px | Laptops / small desktops | lg |
≥ 1200px | Desktops | xl |
≥ 1400px | Large desktops | xxl |
CSS - Practical Responsive Example
/* Mobile: stack everything vertically */
.navbar {
flex-direction: column;
text-align: center;
}
.nav-links { display: none; }
.hamburger { display: block; }
/* Tablet: show navigation */
@media (min-width: 768px) {
.navbar {
flex-direction: row;
justify-content: space-between;
}
.nav-links { display: flex; }
.hamburger { display: none; }
}
/* Desktop: wider container */
@media (min-width: 1200px) {
.container { max-width: 1140px; }
}
3. Responsive Images Flexible Media
Making Images Responsive
CSS - Responsive Images
/* Basic responsive image */
img {
max-width: 100%; /* never wider than container */
height: auto; /* maintain aspect ratio */
}
/* Cover a container (crop to fit) */
.hero-img {
width: 100%;
height: 300px;
object-fit: cover; /* fills container, crops excess */
}
/* Contain within a box (no cropping) */
.product-img {
width: 100%;
height: 200px;
object-fit: contain; /* fits inside, may letterbox */
}
HTML - srcset and picture
<!-- srcset: browser picks best image for screen size -->
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w,
photo-800.jpg 800w,
photo-1200.jpg 1200w"
sizes="(max-width: 600px) 400px,
(max-width: 1000px) 800px,
1200px"
alt="Responsive photo">
<!-- picture: serve different images for different screens -->
<picture>
<source media="(min-width: 1200px)" srcset="hero-desktop.jpg">
<source media="(min-width: 768px)" srcset="hero-tablet.jpg">
<img src="hero-mobile.jpg" alt="Hero image">
</picture>
4. Responsive Typography Flexible Text
Responsive Font Sizing
CSS - Responsive Typography
/* clamp() - responsive size between min and max */
h1 {
font-size: clamp(1.5rem, 5vw, 3rem);
/* min: 1.5rem, preferred: 5vw, max: 3rem */
}
p {
font-size: clamp(0.9rem, 2vw, 1.1rem);
}
/* calc() - computed values */
h2 {
font-size: calc(1.2rem + 1vw);
/* grows with viewport */
}
/* vw units (use carefully - can get too big/small) */
.hero-title {
font-size: 5vw; /* 5% of viewport width */
}
/* Media query approach */
h1 { font-size: 1.8rem; } /* mobile */
@media (min-width: 768px) {
h1 { font-size: 2.5rem; } /* tablet */
}
@media (min-width: 1200px) {
h1 { font-size: 3rem; } /* desktop */
}
clamp() is the Best Approach
clamp(min, preferred, max) gives you responsive text in one line, no media queries needed. The preferred value (usually in vw) smoothly scales between the min and max bounds.
5. Introduction to Bootstrap 5 CSS Framework
What is Bootstrap?
Bootstrap is the most popular CSS framework. It provides pre-built components, a responsive grid system, and utility classes that let you build professional websites quickly without writing much custom CSS.
- Grid System: 12-column responsive grid
- Components: Pre-built navbar, cards, modals, carousels, etc.
- Utilities: Spacing, colors, display, flexbox classes
- Responsive: Built-in breakpoints and responsive utilities
- Bootstrap 5: No jQuery dependency, improved utilities, RTL support
Adding Bootstrap via CDN
HTML - Bootstrap CDN
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Bootstrap Page</title>
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
rel="stylesheet">
</head>
<body>
<!-- Your content here -->
<!-- Bootstrap JS (at end of body) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
Important
The Bootstrap JS bundle is required for interactive components like Navbar toggler, Modals, Carousels, Accordion, and Dropdowns. Always include it at the end of the body.
6. Bootstrap Grid System 12-Column Layout
Container, Row, and Columns
Bootstrap uses a 12-column grid. Content goes in .col classes inside a .row inside a .container.
HTML - Bootstrap Grid Basics
<!-- Container types -->
<div class="container"> <!-- max-width at each breakpoint -->
<div class="container-fluid"> <!-- always 100% width -->
<!-- Equal columns (auto-width) -->
<div class="container">
<div class="row">
<div class="col">Column 1</div>
<div class="col">Column 2</div>
<div class="col">Column 3</div>
</div>
</div>
<!-- Specific column widths (must add up to 12) -->
<div class="row">
<div class="col-4">Sidebar (4/12 = 33%)</div>
<div class="col-8">Main (8/12 = 67%)</div>
</div>
<!-- Responsive columns -->
<div class="row">
<!-- Full width on mobile, half on tablet, third on desktop -->
<div class="col-12 col-md-6 col-lg-4">Card 1</div>
<div class="col-12 col-md-6 col-lg-4">Card 2</div>
<div class="col-12 col-md-6 col-lg-4">Card 3</div>
</div>
| Class | Breakpoint | Width |
col-* | xs (always) | 0px+ |
col-sm-* | sm | 576px+ |
col-md-* | md | 768px+ |
col-lg-* | lg | 992px+ |
col-xl-* | xl | 1200px+ |
col-xxl-* | xxl | 1400px+ |
HTML - Common Grid Patterns
<!-- Product cards: 1 col mobile, 2 col tablet, 4 col desktop -->
<div class="row g-4"> <!-- g-4 = gap/gutter -->
<div class="col-12 col-md-6 col-lg-3">Product 1</div>
<div class="col-12 col-md-6 col-lg-3">Product 2</div>
<div class="col-12 col-md-6 col-lg-3">Product 3</div>
<div class="col-12 col-md-6 col-lg-3">Product 4</div>
</div>
<!-- Sidebar layout -->
<div class="row">
<div class="col-12 col-lg-3">Sidebar</div>
<div class="col-12 col-lg-9">Main Content</div>
</div>
7. Bootstrap Components Pre-Built UI
Navbar
HTML - Bootstrap Navbar
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container">
<a class="navbar-brand" href="#">MySite</a>
<!-- Hamburger button (mobile) -->
<button class="navbar-toggler" data-bs-toggle="collapse"
data-bs-target="#navMenu">
<span class="navbar-toggler-icon"></span>
</button>
<!-- Collapsible menu -->
<div class="collapse navbar-collapse" id="navMenu">
<ul class="navbar-nav ms-auto">
<li class="nav-item"><a class="nav-link" href="#">Home</a></li>
<li class="nav-item"><a class="nav-link" href="#">About</a></li>
<li class="nav-item"><a class="nav-link" href="#">Contact</a></li>
</ul>
</div>
</div>
</nav>
Cards
HTML - Bootstrap Cards
<div class="card" style="width: 18rem;">
<img src="image.jpg" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">Card Title</h5>
<p class="card-text">Some description text.</p>
<a href="#" class="btn btn-primary">Learn More</a>
</div>
</div>
Buttons
HTML - Bootstrap Buttons
<button class="btn btn-primary">Primary</button>
<button class="btn btn-success">Success</button>
<button class="btn btn-danger">Danger</button>
<button class="btn btn-warning">Warning</button>
<button class="btn btn-info">Info</button>
<button class="btn btn-outline-primary">Outline</button>
<button class="btn btn-lg btn-success">Large</button>
<button class="btn btn-sm btn-danger">Small</button>
Modal
HTML - Bootstrap Modal
<!-- Button to trigger modal -->
<button class="btn btn-primary" data-bs-toggle="modal"
data-bs-target="#myModal">Open Modal</button>
<!-- Modal structure -->
<div class="modal fade" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal Title</h5>
<button class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<p>Modal content goes here.</p>
</div>
<div class="modal-footer">
<button class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button class="btn btn-primary">Save Changes</button>
</div>
</div>
</div>
</div>
Carousel
HTML - Bootstrap Carousel
<div id="myCarousel" class="carousel slide" data-bs-ride="carousel">
<!-- Indicators -->
<div class="carousel-indicators">
<button data-bs-target="#myCarousel" data-bs-slide-to="0" class="active"></button>
<button data-bs-target="#myCarousel" data-bs-slide-to="1"></button>
<button data-bs-target="#myCarousel" data-bs-slide-to="2"></button>
</div>
<!-- Slides -->
<div class="carousel-inner">
<div class="carousel-item active">
<img src="slide1.jpg" class="d-block w-100" alt="Slide 1">
</div>
<div class="carousel-item">
<img src="slide2.jpg" class="d-block w-100" alt="Slide 2">
</div>
</div>
<!-- Controls -->
<button class="carousel-control-prev" data-bs-target="#myCarousel" data-bs-slide="prev">
<span class="carousel-control-prev-icon"></span>
</button>
<button class="carousel-control-next" data-bs-target="#myCarousel" data-bs-slide="next">
<span class="carousel-control-next-icon"></span>
</button>
</div>
Accordion, Alerts, Badges, Pagination, Forms
HTML - More Bootstrap Components
<!-- Accordion -->
<div class="accordion" id="faqAccordion">
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button" data-bs-toggle="collapse"
data-bs-target="#q1">Question 1?</button>
</h2>
<div id="q1" class="accordion-collapse collapse show"
data-bs-parent="#faqAccordion">
<div class="accordion-body">Answer here.</div>
</div>
</div>
</div>
<!-- Alerts -->
<div class="alert alert-success">Success!</div>
<div class="alert alert-danger">Error!</div>
<div class="alert alert-warning alert-dismissible fade show">
Warning!
<button class="btn-close" data-bs-dismiss="alert"></button>
</div>
<!-- Badges -->
<span class="badge bg-primary">New</span>
<span class="badge rounded-pill bg-success">99+</span>
<!-- Pagination -->
<nav>
<ul class="pagination">
<li class="page-item"><a class="page-link" href="#">Previous</a></li>
<li class="page-item active"><a class="page-link" href="#">1</a></li>
<li class="page-item"><a class="page-link" href="#">2</a></li>
<li class="page-item"><a class="page-link" href="#">Next</a></li>
</ul>
</nav>
<!-- Form -->
<form>
<div class="mb-3">
<label class="form-label">Email</label>
<input type="email" class="form-control" placeholder="name@example.com">
</div>
<div class="mb-3">
<label class="form-label">Message</label>
<textarea class="form-control" rows="3"></textarea>
</div>
<button class="btn btn-primary">Submit</button>
</form>
8. Bootstrap Utilities Quick Styling Classes
Spacing Utilities
HTML - Spacing (m = margin, p = padding)
<!-- Format: {property}{sides}-{size}
property: m (margin) or p (padding)
sides: t(top) b(bottom) s(start/left) e(end/right) x(left+right) y(top+bottom)
size: 0, 1, 2, 3, 4, 5, auto -->
<div class="mt-3">margin-top: 1rem</div>
<div class="mb-5">margin-bottom: 3rem</div>
<div class="mx-auto">center horizontally</div>
<div class="p-4">padding: 1.5rem all sides</div>
<div class="py-3 px-4">vertical + horizontal padding</div>
Display, Text, Background, and Flex Utilities
HTML - Common Utility Classes
<!-- Display -->
<div class="d-none">Hidden</div>
<div class="d-block">Block</div>
<div class="d-flex">Flexbox</div>
<div class="d-none d-md-block">Hidden on mobile, visible on tablet+</div>
<div class="d-block d-lg-none">Visible until desktop</div>
<!-- Text -->
<p class="text-center">Centered</p>
<p class="text-end">Right-aligned</p>
<p class="fw-bold">Bold</p>
<p class="fs-1">Large text</p>
<p class="text-muted">Muted</p>
<p class="text-success">Green text</p>
<p class="text-uppercase">uppercase</p>
<!-- Background -->
<div class="bg-primary text-white">Blue bg</div>
<div class="bg-dark text-light">Dark bg</div>
<div class="bg-success bg-opacity-25">25% green bg</div>
<!-- Flex -->
<div class="d-flex justify-content-between align-items-center">
<span>Left</span>
<span>Right</span>
</div>
<div class="d-flex justify-content-center gap-3">
<span>A</span>
<span>B</span>
<span>C</span>
</div>
Customizing Bootstrap with CSS
CSS - Override Bootstrap
/* Link your custom CSS AFTER Bootstrap */
/* <link href="bootstrap.min.css" rel="stylesheet"> */
/* <link href="custom.css" rel="stylesheet"> <-- after! */
/* Override Bootstrap styles */
.navbar {
background: #0f0f1a !important;
border-bottom: 2px solid #4caf50;
}
.btn-primary {
background-color: #4caf50;
border-color: #4caf50;
}
.btn-primary:hover {
background-color: #388e3c;
border-color: #388e3c;
}
/* Use Bootstrap variables */
:root {
--bs-primary: #4caf50;
}
Load Order Matters
Always load your custom CSS file after Bootstrap's CSS. This ensures your styles override Bootstrap's defaults. Use more specific selectors if !important is needed.