Cấu trúc CSS
Mỗi quy tắc CSS gồm ba phần: bộ chọn, thuộc tính và giá trị. Cú pháp chuẩn:
selector {
property: value;
property: value;
}
Định dạng văn bản
- Kích thước chữ:
font-size: 14px; - Màu sắc:
color: #ff0000; - Font chữ:
font-family: "Arial", sans-serif; - Độ đậm:
font-weight: 700; - Chiều cao dòng:
line-height: 1.6;
<style>
.text-demo {
font-size: 18px;
color: navy;
font-family: "Helvetica", sans-serif;
line-height: 1.8;
font-weight: 600;
}
</style>
<p class="text-demo">Đây là đoạn văn được định dạng</p>
Tính kế thừa và ghi đè
CSS có hai đặc điểm quan trọng:
- Kế thừa: Phần tử con tự động nhận style từ phần tử cha.
- Ghi đè: Style khai báo sau hoặc cụ thể hơn sẽ ghi đè style trước đó.
<style>
.container { color: green; }
.highlight { color: orange; }
</style>
<div class="container">
<p>Chữ màu xanh lá (kế thừa)</p>
<p class="highlight">Chữ màu cam (ghi đè)</p>
</div>
Thuộc tính điều khiển văn bản
| Thuộc tính | Mô tả |
|---|---|
text-indent | Canh lề đầu dòng (vd: 2em) |
text-align | Căn lề: left/center/right |
text-transform | Chuyển đổi hoa/thường |
text-decoration | Gạch chân, gạch ngang... |
list-style | Kiểu dấu đầu dòng danh sách |
Ví dụ: Thanh điều hướng
<style>
.nav-wrapper {
width: 100%;
height: 50px;
background: linear-gradient(to right, #333, #555);
}
.nav-list {
list-style: none;
margin: 0;
padding: 0;
display: flex;
}
.nav-item {
width: 120px;
text-align: center;
line-height: 50px;
}
.nav-link {
color: white;
text-decoration: none;
font-weight: 500;
display: block;
transition: background 0.3s;
}
.nav-item:hover .nav-link {
background: crimson;
}
</style>
<nav class="nav-wrapper">
<ul class="nav-list">
<li class="nav-item"><a class="nav-link" href="#">Trang chủ</a></li>
<li class="nav-item"><a class="nav-link" href="#">Sản phẩm</a></li>
<li class="nav-item"><a class="nav-link" href="#">Liên hệ</a></li>
</ul>
</nav>
Ví dụ: Hiển thị sản phẩm
<style>
.product-grid {
display: flex;
flex-wrap: wrap;
gap: 20px;
padding: 20px;
max-width: 1200px;
margin: 0 auto;
}
.product-card {
width: 240px;
border: 2px solid transparent;
border-radius: 8px;
overflow: hidden;
transition: all 0.3s ease;
}
.product-card:hover {
border-color: #e74c3c;
transform: translateY(-5px);
box-shadow: 0 10px 20px rgba(0,0,0,0.1);
}
.product-image {
width: 100%;
height: 200px;
object-fit: cover;
}
.product-info {
padding: 12px;
}
.price {
color: #e74c3c;
font-weight: bold;
font-size: 1.1em;
}
.sold-count {
color: #999;
float: right;
}
.product-title {
display: block;
color: #555;
text-decoration: none;
margin: 8px 0;
font-size: 0.9em;
}
.store-info {
color: #777;
font-size: 0.85em;
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 10px;
}
</style>
<div class="product-grid">
<div class="product-card">
<img class="product-image" src="product1.jpg" alt="Sản phẩm">
<div class="product-info">
<div>
<span class="price">$89</span>
<span class="sold-count">1.5K đã mua</span>
</div>
<a class="product-title" href="#">Gấu bông Doraemon chính hãng</a>
<div class="store-info">
<span>Shop ABC</span>
<span>Hà Nội</span>
</div>
</div>
</div>
<!-- Lặp lại các thẻ product-card khác -->
</div>