Selector
Selector Cơ Bản
*Tổng quát — tất cả phần tử
divLoại — tất cả phần tử
.classClass — phần tử có class
#idID — phần tử có id
[attr]Thuộc tính — có thuộc tính
[attr="val"]Thuộc tính bằng giá trị
Combinator
A BCon cháu (độ sâu bất kỳ)
A > BChỉ con trực tiếp
A + BAnh em liền kề (tiếp theo)
A ~ BAnh em chung (tất cả phía sau)
Pseudo-class
:hoverChuột di qua phần tử
:focusPhần tử được focus
:first-childCon đầu tiên của parent
:nth-child(n)Con thứ n (1-based, odd, even, 2n+1)
:not(sel)Phủ định — loại trừ kết quả khớp
:has(sel)Selector parent (chứa kết quả khớp)
Pseudo-element
::beforeChèn nội dung trước phần tử
::afterChèn nội dung sau phần tử
::placeholderStyle văn bản placeholder của input
::selectionStyle văn bản được chọn
Box Model
Box Sizing
/* Include padding/border in width */ *, *::before, *::after { box-sizing: border-box; }
Thuộc Tính
marginKhoảng cách ngoài border
borderViền bao quanh padding
paddingKhoảng cách trong border
width / heightKích thước nội dung
outlineVòng ngoài margin (không chiếm không gian)
Viết Tắt
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; }
Thuộc Tính Container
flex-directionrow | column | row-reverse | column-reverse
flex-wrapnowrap | wrap | wrap-reverse
justify-contentflex-start | center | space-between | space-around | space-evenly
align-itemsstretch | center | flex-start | flex-end | baseline
align-contentCăn chỉnh trục chéo nhiều dòng
gapKhoảng cách giữa các flex item
Thuộc Tính Item
flex: 1Mở rộng để lấp đầy không gian
flex: 0 0 200pxChiều rộng cố định, không co dãn
align-selfGhi đè align-items cho một item
orderThay đổi thứ tự hiển thị (mặc định 0)
Grid
Container
.grid { display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: auto 1fr auto; gap: 1rem; }
Thuộc Tính Container
grid-template-columnsĐịnh nghĩa cột: 1fr 2fr, repeat(3, 1fr)
grid-template-rowsĐịnh nghĩa hàng
grid-template-areasVùng có tên: "header header" "nav main"
gapKhoảng cách hàng và cột
justify-itemsCăn chỉnh item theo chiều ngang
align-itemsCăn chỉnh item theo chiều dọc
Đặt Vị Trí Item
.item { grid-column: 1 / 3; /* span cols 1-2 */ grid-row: 1 / -1; /* span all rows */ grid-area: header; /* named area */ }
Typography
Thuộc Tính Font
font-familyBộ font: 'Inter', sans-serif
font-sizeKích thước: 1rem, 16px, clamp(1rem, 2vw, 2rem)
font-weightnormal (400) | bold (700) | 100-900
font-stylenormal | italic | oblique
line-heightKhoảng cách dòng: 1.5 (không đơn vị, được khuyến nghị)
letter-spacingKhoảng cách ký tự: 0.05em
Thuộc Tính Văn Bản
text-alignleft | center | right | justify
text-decorationnone | underline | line-through
text-transformuppercase | lowercase | capitalize
text-overflowellipsis (với overflow: hidden)
white-spacenowrap | pre | pre-wrap
word-breakbreak-all | break-word
Màu Sắc & Nền
Định Dạng Màu
color: #ff6600; /* hex */ color: rgb(255, 102, 0); /* rgb */ color: hsl(24, 100%, 50%); /* hsl */ color: oklch(70% 0.15 50); /* oklch */
Thuộc Tính Nền
background-colorMàu nền: #f0f0f0
background-imageurl(img.jpg) hoặc gradient
background-sizecover | contain | 100px 200px
background-positioncenter | top right | 50% 50%
background-repeatno-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);
Transition & Animation
Transition
.btn { transition: background 0.3s ease, transform 0.2s; } .btn:hover { background: #0056b3; transform: scale(1.05); }
Thuộc Tính Transition
transition-propertyThuộc tính nào được animate
transition-durationThời lượng: 0.3s, 300ms
transition-timing-functionease | linear | ease-in-out | cubic-bezier()
transition-delayChờ trước khi bắt đầu
Keyframe Animation
@keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } .icon { animation: spin 1s linear infinite; }
Thuộc Tính Animation
animation-nameTham chiếu tên @keyframes
animation-durationĐộ dài một chu kỳ
animation-iteration-count1 | 3 | infinite
animation-directionnormal | alternate | reverse
animation-fill-modeforwards | backwards | both
Thiết Kế Responsive
Media Query
@media (max-width: 768px) { .sidebar { display: none; } } @media (prefers-color-scheme: dark) { body { background: #1a1a1a; color: #eee; } }
Điểm Ngắt Phổ Biến
max-width: 480pxĐiện thoại di động
max-width: 768pxMáy tính bảng
max-width: 1024pxLaptop nhỏ
max-width: 1280pxDesktop
Đơn Vị Viewport
vw / vh% chiều rộng / chiều cao viewport
dvhDynamic viewport height (an toàn cho mobile)
svh / lvhChiều cao viewport nhỏ / lớn
cqiKích thước inline container query
Container Query
.card-wrapper { container-type: inline-size; } @container (min-width: 400px) { .card { flex-direction: row; } }
Vị Trí
Giá Trị Position
staticMặc định — luồng tài liệu bình thường
relativeLệch so với vị trí bình thường; giữ không gian
absoluteCăn theo ancestor có position gần nhất
fixedCăn theo viewport; không cuộn
stickyChuyển đổi relative/fixed theo cuộn
Căn Giữa
/* Flex center */ display: flex; justify-content: center; align-items: center; /* Grid center */ display: grid; place-items: center;
Xếp Chồng
z-indexThứ tự xếp chồng (cao hơn = trên cùng); cần position
isolation: isolateTạo ngữ cảnh xếp chồng mới
Thuộc Tính Tùy Chỉnh
Khai Báo & Sử Dụng
:root { --color-primary: #3b82f6; --spacing-md: 1rem; } .btn { background: var(--color-primary); padding: var(--spacing-md); }
Giá Trị Dự Phòng
color: var(--accent, #ff6600); /* uses #ff6600 if --accent is not defined */
Theming Động
[data-theme="dark"] { --bg: #1a1a2e; --text: #e0e0e0; } body { background: var(--bg); color: var(--text); }