REFERENSI CEPAT CSS
Selector, layout, flexbox, grid, animasi, desain responsif
Selector
Selector Dasar
| * | Universal — semua elemen |
| div | Tipe — semua elemen |
| .class | Class — elemen dengan class |
| #id | ID — elemen dengan id |
| [attr] | Atribut — memiliki atribut |
| [attr="val"] | Atribut sama dengan nilai |
Kombinator
| A B | Keturunan (kedalaman berapa pun) |
| A > B | Anak langsung saja |
| A + B | Sibling bersebelahan (berikutnya) |
| A ~ B | Sibling umum (semua setelahnya) |
Pseudo-class
| :hover | Mouse di atas elemen |
| :focus | Elemen mendapat fokus |
| :first-child | Anak pertama dari parent |
| :nth-child(n) | Anak ke-n (1-based, odd, even, 2n+1) |
| :not(sel) | Negasi — kecualikan yang cocok |
| :has(sel) | Selector parent (mengandung yang cocok) |
Pseudo-element
| ::before | Sisipkan konten sebelum elemen |
| ::after | Sisipkan konten setelah elemen |
| ::placeholder | Gaya teks placeholder input |
| ::selection | Gaya teks yang disorot |
Box Model
Box Sizing
/* Include padding/border in width */
*, *::before, *::after {
box-sizing: border-box;
}
Properti
| margin | Ruang di luar border |
| border | Tepi di sekitar padding |
| padding | Ruang di dalam border |
| width / height | Dimensi konten |
| outline | Ring di luar margin (tanpa ruang) |
Shorthand
margin: 10px; /* all sides */
margin: 10px 20px; /* vertical | horizontal */
margin: 10px 20px 30px; /* top | horiz | bottom */
margin: 10px 20px 30px 40px; /* T R B L */
Flexbox
Container
.flex {
display: flex;
justify-content: center; /* main axis */
align-items: center; /* cross axis */
gap: 1rem;
}
Properti Container
| flex-direction | row | column | row-reverse | column-reverse |
| flex-wrap | nowrap | wrap | wrap-reverse |
| justify-content | flex-start | center | space-between | space-around | space-evenly |
| align-items | stretch | center | flex-start | flex-end | baseline |
| align-content | Perataan sumbu silang multi-baris |
| gap | Jarak antar item flex |
Properti Item
| flex: 1 | Tumbuh mengisi ruang yang tersedia |
| flex: 0 0 200px | Lebar tetap, tidak tumbuh/menyusut |
| align-self | Override align-items untuk satu item |
| order | Ubah urutan visual (default 0) |
Grid
Container
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: auto 1fr auto;
gap: 1rem;
}
Properti Container
| grid-template-columns | Definisikan kolom: 1fr 2fr, repeat(3, 1fr) |
| grid-template-rows | Definisikan baris |
| grid-template-areas | Area bernama: "header header" "nav main" |
| gap | Jarak antar baris dan kolom |
| justify-items | Perataan item horizontal |
| align-items | Perataan item vertikal |
Penempatan Item
.item {
grid-column: 1 / 3; /* span cols 1-2 */
grid-row: 1 / -1; /* span all rows */
grid-area: header; /* named area */
}
Tipografi
Properti Font
| font-family | Stack typeface: 'Inter', sans-serif |
| font-size | Ukuran: 1rem, 16px, clamp(1rem, 2vw, 2rem) |
| font-weight | normal (400) | bold (700) | 100-900 |
| font-style | normal | italic | oblique |
| line-height | Spasi baris: 1.5 (tanpa satuan direkomendasikan) |
| letter-spacing | Spasi karakter: 0.05em |
Properti Teks
| text-align | left | center | right | justify |
| text-decoration | none | underline | line-through |
| text-transform | uppercase | lowercase | capitalize |
| text-overflow | ellipsis (dengan overflow: hidden) |
| white-space | nowrap | pre | pre-wrap |
| word-break | break-all | break-word |
Warna & Background
Format Warna
color: #ff6600; /* hex */
color: rgb(255, 102, 0); /* rgb */
color: hsl(24, 100%, 50%); /* hsl */
color: oklch(70% 0.15 50); /* oklch */
Properti Background
| background-color | Warna solid: #f0f0f0 |
| background-image | url(img.jpg) atau gradient |
| background-size | cover | contain | 100px 200px |
| background-position | center | top right | 50% 50% |
| background-repeat | no-repeat | repeat-x | repeat-y |
Gradient
background: linear-gradient(to right, #f00, #00f);
background: radial-gradient(circle, #fff, #000);
background: conic-gradient(red, yellow, green);
Transisi & Animasi
Transisi
.btn {
transition: background 0.3s ease, transform 0.2s;
}
.btn:hover {
background: #0056b3;
transform: scale(1.05);
}
Properti Transisi
| transition-property | Properti mana yang dianimasikan |
| transition-duration | Durasi: 0.3s, 300ms |
| transition-timing-function | ease | linear | ease-in-out | cubic-bezier() |
| transition-delay | Tunggu sebelum mulai |
Animasi Keyframe
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.icon { animation: spin 1s linear infinite; }
Properti Animasi
| animation-name | Referensi nama @keyframes |
| animation-duration | Durasi satu siklus |
| animation-iteration-count | 1 | 3 | infinite |
| animation-direction | normal | alternate | reverse |
| animation-fill-mode | forwards | backwards | both |
Desain Responsif
Media Query
@media (max-width: 768px) {
.sidebar { display: none; }
}
@media (prefers-color-scheme: dark) {
body { background: #1a1a1a; color: #eee; }
}
Breakpoint Umum
| max-width: 480px | Ponsel |
| max-width: 768px | Tablet |
| max-width: 1024px | Laptop kecil |
| max-width: 1280px | Desktop |
Satuan Viewport
| vw / vh | % lebar / tinggi viewport |
| dvh | Tinggi viewport dinamis (aman untuk mobile) |
| svh / lvh | Tinggi viewport kecil / besar |
| cqi | Ukuran inline container query |
Container Query
.card-wrapper { container-type: inline-size; }
@container (min-width: 400px) {
.card { flex-direction: row; }
}
Positioning
Nilai Position
| static | Default — alur dokumen normal |
| relative | Offset dari posisi normal; tetap mengisi ruang |
| absolute | Relatif terhadap ancestor yang ter-position |
| fixed | Relatif terhadap viewport; tetap saat scroll |
| sticky | Berganti relative/fixed berdasarkan scroll |
Pola Centering
/* Flex center */
display: flex;
justify-content: center;
align-items: center;
/* Grid center */
display: grid;
place-items: center;
Stacking
| z-index | Urutan tumpukan (lebih tinggi = di atas); butuh position |
| isolation: isolate | Membuat stacking context baru |
Custom Properties
Definisi & Penggunaan
:root {
--color-primary: #3b82f6;
--spacing-md: 1rem;
}
.btn {
background: var(--color-primary);
padding: var(--spacing-md);
}
Nilai Fallback
color: var(--accent, #ff6600);
/* uses #ff6600 if --accent is not defined */
Theming Dinamis
[data-theme="dark"] {
--bg: #1a1a2e;
--text: #e0e0e0;
}
body { background: var(--bg); color: var(--text); }