Tujuan Belajar
Setelah pelajaran ini, kamu akan bisa:
- Menggunakan background warna, gambar, dan gradien
- Mengatur posisi, ukuran, dan pengulangan background
- Membuat hero section dengan background menarik
Materi Singkat
Background bukan hanya tentang warna. Dengan CSS, kamu bisa membuat background yang kaya dengan gambar, gradien, dan kombinasi keduanya.
Background Warna
background-color: #2563eb;
background-color: rgb(37, 99, 235);
background-color: transparent; /* transparan */Background Gambar
background-image: url('gambar.jpg');
background-image: url('https://picsum.photos/1200/400');Properti pendukung:
/* Pengulangan */
background-repeat: repeat; /* tile/ulang (default) */
background-repeat: no-repeat; /* tidak diulang */
background-repeat: repeat-x; /* ulang horizontal */
/* Posisi */
background-position: center;
background-position: top left;
background-position: 50% 30%;
/* Ukuran */
background-size: auto; /* ukuran asli */
background-size: cover; /* menutupi seluruh area (bisa terpotong) */
background-size: contain; /* masuk seluruhnya (bisa ada ruang kosong) */
background-size: 300px;
background-size: 100% 200px;Background Gradient
Gradien adalah transisi mulus dari satu warna ke warna lain:
/* Linear gradient */
background-image: linear-gradient(to right, #2563eb, #7c3aed);
background-image: linear-gradient(135deg, #1a1a1a, #2563eb);
background-image: linear-gradient(to bottom, #f5f5f5, #ffffff);
/* Radial gradient (dari tengah ke luar) */
background-image: radial-gradient(circle, #2563eb, #1a1a1a);Shorthand Background
/* background: color image repeat position / size */
background: #f5f5f5 url('gambar.jpg') no-repeat center / cover;Overlay: Teks di atas Gambar
Untuk membuat teks terbaca di atas gambar, tambahkan overlay semi-transparan:
.hero {
position: relative;
background-image: url('foto.jpg');
background-size: cover;
background-position: center;
color: white;
padding: 80px 24px;
}
/* Overlay gelap */
.hero::before {
content: '';
position: absolute;
inset: 0;
background-color: rgba(0, 0, 0, 0.5);
}
/* Konten di atas overlay */
.hero-content {
position: relative;
z-index: 1;
}Contoh Kode
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Background CSS</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<section class="hero">
<div class="hero-content">
<h1>Selamat Datang!</h1>
<p>Ini adalah hero section dengan background gambar dan overlay.</p>
<a href="#" class="btn">Mulai Sekarang</a>
</div>
</section>
<section class="gradient-section">
<h2>Section dengan Gradient</h2>
<p>Background gradien dari biru ke ungu.</p>
</section>
<section class="pattern-section">
<h2>Section dengan Pola</h2>
</section>
</body>
</html>*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: sans-serif; }
/* Hero dengan gambar + overlay */
.hero {
position: relative;
background-image: url('https://picsum.photos/1200/400?random=5');
background-size: cover;
background-position: center;
padding: 80px 24px;
text-align: center;
color: white;
}
.hero::before {
content: '';
position: absolute;
inset: 0;
background-color: rgba(0, 0, 0, 0.55);
}
.hero-content {
position: relative;
z-index: 1;
}
.hero h1 {
font-size: 2.5rem;
margin-bottom: 12px;
}
.hero p {
font-size: 1.125rem;
margin-bottom: 24px;
opacity: 0.9;
}
.btn {
display: inline-block;
padding: 12px 28px;
background-color: #2563eb;
color: white;
border-radius: 6px;
text-decoration: none;
font-weight: 600;
}
/* Gradient section */
.gradient-section {
background-image: linear-gradient(135deg, #2563eb, #7c3aed);
color: white;
padding: 48px 24px;
text-align: center;
}
.gradient-section h2 {
margin-bottom: 8px;
}
/* Pattern section */
.pattern-section {
background-color: #f0f4ff;
background-image: radial-gradient(#2563eb22 1px, transparent 1px);
background-size: 20px 20px;
padding: 48px 24px;
text-align: center;
}Tugas Kecil
Tambahkan ke halaman profilmu:
- Hero section di bagian paling atas (dalam atau sebelum
<header>) dengan:- Background gambar dari
picsum.photos(ukuran 1200x400) - Overlay hitam semi-transparan (
rgba(0,0,0,0.5)) - Teks nama dan tagline yang terbaca di atasnya (warna putih)
- Background gambar dari
- Footer dengan gradient menarik (dua warna pilihanmu)