Responsive Design in HTML
Responsive design ensures a web page looks good on all screen sizes.
Viewport Meta Tag
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Media Queries
/* Default: Mobile */
body {
font-size: 16px;
}
/* Tablet */
@media (min-width: 768px) {
body {
font-size: 18px;
}
}
/* Desktop */
@media (min-width: 1024px) {
body {
font-size: 20px;
}
}
Case Study: Responsive Campus Website
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Responsive Campus Website</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
line-height: 1.6;
}
.container {
width: 90%;
max-width: 1200px;
margin: 0 auto;
padding: 0 1rem;
}
header {
background: #1e40af;
color: white;
padding: 1rem;
}
nav ul {
display: flex;
flex-direction: column;
gap: 0.5rem;
list-style: none;
}
main {
padding: 2rem 0;
}
.grid {
display: grid;
grid-template-columns: 1fr;
gap: 1rem;
}
.card {
background: #f9fafb;
border: 1px solid #e5e7eb;
border-radius: 8px;
padding: 1.5rem;
}
footer {
background: #333;
color: white;
text-align: center;
padding: 1rem;
}
/* Tablet */
@media (min-width: 768px) {
nav ul {
flex-direction: row;
justify-content: center;
gap: 1rem;
}
.grid {
grid-template-columns: repeat(2, 1fr);
}
}
/* Desktop */
@media (min-width: 1024px) {
.grid {
grid-template-columns: repeat(3, 1fr);
}
}
</style>
</head>
<body>
<header>
<div class="container">
<h1>Technology University</h1>
<nav>
<ul>
<li><a href="beranda.html">Home</a></li>
<li><a href="academic.html">Academic</a></li>
<li><a href="kontak.html">Kontak</a></li>
</ul>
</nav>
</div>
</header>
<main>
<div class="container">
<h2>Major</h2>
<div class="grid">
<div class="card">
<h3>Computer Science</h3>
<p>4 Years - 144 Credits</p>
</div>
<div class="card">
<h3>Information Systems</h3>
<p>4 Years - 144 Credits</p>
</div>
<div class="card">
<h3>Teknik Komputer</h3>
<p>4 Years - 144 Credits</p>
</div>
</div>
</div>
</main>
<footer>
<p>© 2024 Technology University</p>
</footer>
</body>
</html>
Everyday Case Study
Case: A developer needs to create a responsive table.
<style>
.table-responsive {
overflow-x: auto;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 0.75rem;
border: 1px solid #e5e7eb;
text-align: left;
}
th {
background: #f3f4f6;
}
@media (max-width: 600px) {
table {
font-size: 0.875rem;
}
th, td {
padding: 0.5rem;
}
}
</style>
<div class="table-responsive">
<table>
<thead>
<tr>
<th>Name</th>
<th>StudentID</th>
<th>Major</th>
<th>IPK</th>
</tr>
</thead>
<tbody>
<tr>
<td>John Smith</td>
<td>1234567890</td>
<td>Computer Science</td>
<td>3.75</td>
</tr>
</tbody>
</table>
</div>
Best Practices
- Mobile-first: Design for mobile first
- Fluid images: Use max-width: 100%
- Media queries: For different breakpoints
- Flexible grid: Use CSS Grid or Flexbox
- Viewport meta tag: Always use it
Summary
Responsive design ensures a web page looks good on all devices. Use media queries and CSS Grid/Flexbox.