#Responsive Design
Di dunia nyata, website diakses dari berbagai perangkat — HP, tablet, laptop, monitor besar. Responsive design memastikan website tampil bagus di semua ukuran layar.
#Media Queries
Media query adalah fitur CSS yang memungkinkan kita menerapkan style berdasarkan ukuran layar.
/* Style default (untuk semua ukuran) */
.container {
max-width: 1200px;
margin: 0 auto;
padding: 0 20px;
}
/* Jika lebar layar maksimal 768px (tablet/HP) */
@media (max-width: 768px) {
.container {
padding: 0 15px;
}
}
/* Jika lebar layar maksimal 480px (HP kecil) */
@media (max-width: 480px) {
.container {
padding: 0 10px;
}
}#Breakpoint Umum
| Breakpoint | Perangkat |
|---|---|
> 1024px | Desktop |
768px - 1024px | Tablet |
480px - 768px | HP landscape |
< 480px | HP portrait |
#Pendekatan Mobile First
Mulai desain dari ukuran kecil (HP), lalu tambahkan style untuk layar besar:
/* Default: style untuk HP */
.card-grid {
display: grid;
grid-template-columns: 1fr; /* 1 kolom di HP */
gap: 15px;
}
/* Tablet ke atas */
@media (min-width: 768px) {
.card-grid {
grid-template-columns: 1fr 1fr; /* 2 kolom */
}
}
/* Desktop ke atas */
@media (min-width: 1024px) {
.card-grid {
grid-template-columns: 1fr 1fr 1fr; /* 3 kolom */
}
}#Viewport Meta Tag
Pastikan setiap halaman HTML punya meta viewport di <head>. Tanpa ini, responsive design tidak akan berfungsi di HP!
<meta name="viewport" content="width=device-width, initial-scale=1.0">#Responsive Navigation
Navbar di desktop biasanya horizontal, di HP biasanya vertikal atau berupa hamburger menu:
/* Default: Navbar horizontal */
nav {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px 30px;
background: #333;
}
.menu {
display: flex;
gap: 20px;
list-style: none;
}
/* HP: Navbar vertikal */
@media (max-width: 768px) {
nav {
flex-direction: column;
gap: 15px;
}
.menu {
flex-direction: column;
text-align: center;
gap: 10px;
}
}#Responsive Layout
Sidebar yang di desktop ada di samping, di HP pindah ke bawah:
/* Desktop: 2 kolom */
.page-layout {
display: grid;
grid-template-columns: 1fr 300px;
gap: 30px;
}
/* Tablet & HP: 1 kolom */
@media (max-width: 768px) {
.page-layout {
grid-template-columns: 1fr;
}
}#Responsive Images
/* Gambar tidak boleh lebih besar dari container */
img {
max-width: 100%;
height: auto;
}#Responsive Typography
/* Font yang menyesuaikan ukuran layar */
h1 {
font-size: 2.5rem; /* Desktop */
}
@media (max-width: 768px) {
h1 {
font-size: 1.8rem; /* Tablet */
}
}
@media (max-width: 480px) {
h1 {
font-size: 1.5rem; /* HP */
}
}
/* Alternatif: clamp() — otomatis menyesuaikan */
h1 {
font-size: clamp(1.5rem, 4vw, 2.5rem);
/* minimum: 1.5rem, ideal: 4vw, maximum: 2.5rem */
}#Latihan: Halaman Portofolio Responsive
Buat file portfolio.html dan portfolio-style.css:
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Portofolio User</title>
<link rel="stylesheet" href="portfolio-style.css">
</head>
<body>
<nav>
<div class="logo">User</div>
<ul class="menu">
<li><a href="#beranda">Beranda</a></li>
<li><a href="#skill">Skill</a></li>
<li><a href="#project">Project</a></li>
<li><a href="#kontak">Kontak</a></li>
</ul>
</nav>
<section id="beranda" class="hero">
<h1>Halo, Saya User! 👋</h1>
<p>Web Developer pemula yang sedang belajar dengan semangat.</p>
</section>
<section id="skill" class="section">
<h2>Skill</h2>
<div class="skill-grid">
<div class="skill-card">📄 HTML</div>
<div class="skill-card">🎨 CSS</div>
<div class="skill-card">⚡ JavaScript</div>
<div class="skill-card">🐘 PHP</div>
<div class="skill-card">🗄️ MySQL</div>
</div>
</section>
<section id="project" class="section">
<h2>Project</h2>
<div class="project-grid">
<div class="project-card">
<h3>Halaman Profil</h3>
<p>Halaman profil sederhana dengan HTML.</p>
</div>
<div class="project-card">
<h3>Blog Pribadi</h3>
<p>Blog dengan semantic HTML dan CSS styling.</p>
</div>
<div class="project-card">
<h3>Buku Tamu</h3>
<p>Aplikasi CRUD dengan PHP & MySQL.</p>
</div>
</div>
</section>
<section id="kontak" class="section">
<h2>Kontak</h2>
<form>
<input type="text" placeholder="Nama" required>
<input type="email" placeholder="Email" required>
<textarea placeholder="Pesan" rows="5" required></textarea>
<button type="submit">Kirim Pesan</button>
</form>
</section>
<footer>
<p>© 2026 User — Dibuat sambil belajar ❤️</p>
</footer>
</body>
</html>/* portfolio-style.css */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', sans-serif;
line-height: 1.6;
color: #333;
}
/* Navbar */
nav {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px 30px;
background: white;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
position: sticky;
top: 0;
z-index: 100;
}
.logo { font-size: 24px; font-weight: bold; color: #667eea; }
.menu { display: flex; gap: 25px; list-style: none; }
.menu a { color: #555; text-decoration: none; font-weight: 500; }
.menu a:hover { color: #667eea; }
/* Hero */
.hero {
text-align: center;
padding: 100px 20px;
background: linear-gradient(135deg, #667eea, #764ba2);
color: white;
}
.hero h1 { font-size: 3rem; margin-bottom: 15px; }
.hero p { font-size: 1.2rem; opacity: 0.9; }
/* Section */
.section {
max-width: 1000px;
margin: 0 auto;
padding: 60px 20px;
}
.section h2 {
text-align: center;
font-size: 2rem;
margin-bottom: 30px;
color: #333;
}
/* Skill Grid */
.skill-grid {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 15px;
}
.skill-card {
background: white;
padding: 20px 30px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
font-size: 1.1rem;
font-weight: 600;
}
/* Project Grid */
.project-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 20px;
}
.project-card {
background: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
border-left: 4px solid #667eea;
}
.project-card h3 { color: #667eea; margin-bottom: 10px; }
/* Form */
form {
max-width: 500px;
margin: 0 auto;
display: flex;
flex-direction: column;
gap: 15px;
}
form input, form textarea {
padding: 12px 15px;
border: 2px solid #ddd;
border-radius: 8px;
font-size: 16px;
font-family: inherit;
}
form input:focus, form textarea:focus {
outline: none;
border-color: #667eea;
}
form button {
padding: 12px;
background: #667eea;
color: white;
border: none;
border-radius: 8px;
font-size: 16px;
cursor: pointer;
}
form button:hover { background: #5a6fd6; }
/* Footer */
footer {
text-align: center;
padding: 20px;
background: #333;
color: #aaa;
}
/* ========== RESPONSIVE ========== */
@media (max-width: 768px) {
nav {
flex-direction: column;
gap: 10px;
}
.menu { gap: 15px; }
.hero h1 { font-size: 2rem; }
.hero { padding: 60px 20px; }
.section { padding: 40px 15px; }
}
@media (max-width: 480px) {
.hero h1 { font-size: 1.6rem; }
.hero p { font-size: 1rem; }
.menu {
flex-wrap: wrap;
justify-content: center;
gap: 10px;
}
}Coba buka http://belajar.test/portfolio.html lalu resize browser dari besar ke kecil, perhatikan layoutnya berubah!
#Rangkuman CSS
| Topik | Konsep Utama |
|---|---|
| Dasar | Selector, property-value, class vs id |
| Box Model | content, padding, border, margin |
| Flexbox | display: flex, justify-content, align-items |
| Grid | display: grid, grid-template-columns |
| Responsive | @media, viewport meta, mobile-first |
#Selanjutnya
Website sudah punya struktur (HTML) dan tampilan (CSS). Sekarang saatnya menambahkan interaktivitas dengan Dasar JavaScript →!