Một số CSS nhỏ hay gặp khi làm dự án
Bài đăng này đã không được cập nhật trong 6 năm
Chào các bạn, hôm nay chúng ta cùng nhau xem qua các tip CSS hay gặp khi làm dự án nhé.
Nguồn: https://30-seconds.github.io/30-seconds-of-css/
1. Dấu ba chấm
- Từ khoá: Loading-dot
- Code:
HTML
<div class="loading-dot">
<div></div>
<div></div>
<div></div>
</div>
CSS
@keyframes loading-dot {
to {
opacity: 0.1;
transform: translate3d(0, -1rem, 0);
}
}
.loading-dot {
display: flex;
justify-content: center;
}
.loading-dot > div {
width: 1rem;
height: 1rem;
margin: 3rem 0.2rem;
background: #8385aa;
border-radius: 50%;
animation: loading-dot 0.6s infinite alternate;
}
.loading-dot > div:nth-child(2) {
animation-delay: 0.2s;
}
.loading-dot > div:nth-child(3) {
animation-delay: 0.4s;
}
- Demo:

- Giải thích:
1remđược tính là16px@keyframesđịnh nghĩa mộtanimationvới hai trạng thái, khi element thay đổiopacityvà được chuyển lên trạng thái 2D bằngtransform: translate3d(). Sử dụngtransform: translate3d()sẽ tăng tốc độ củaanimation.loading-dotlà class cha của cácloading-dotvà sử dụng thuộc tínhdisplay: flexvàjustify-content: centerđể đưa chúng về giữa..loading-dot > divchỉ đến badivnhỏ nằm bên trongdiv.loading-dotvớiwidthvàheightlà1rem, sử dụngborder-radius: 50%để đưa chúng từ hình vuông sang hình tròn.animationlà property ngắn gọn của rất nhiều property animation khác như:animation-name,animation-duration,animation-iteration-count,animation-direction. Khi sử dụnganimationthì các property trên đều được sử dụng.nth-child(n)trỏ đến các element con của element cha.animation-delayđược sử dụng chodivthứ 2 và thứ 3 để bảo đảm các element không hoạt động cùng một lúc.
2. Hình tròn
- Từ khoá: Circle
- Code:
HTML
<div class="circle">
CSS
.circle {
border-radius: 50%;
width: 2rem;
height: 2rem;
background: #333;
}
- Demo:

- Giải thích:
- Thuộc tính
border-radius: 50%sẽ làm cong border của element thành hình tròn.- Vì hình tròn có cùng bán kính ở bất kỳ điểm nào, nên
widthvàheightphải bằng nhau. Nếu chúng khác nhau sẽ tạo ra hìnhelip.
3. Clearfix
- Từ khoá: Clearfix
- Code:
HTML
<div class="clearfix">
<div class="floated">Clearfix 1</div>
<div class="floated">Clearfix 2</div>
<div class="floated">Clearfix 3</div>
</div>
CSS
.clearfix::after {
content: '';
display: block;
clear: both;
}
.floated {
float: left;
}
- Demo:

- Giải thích:
- Class
clearfix::afterđịnh nghĩa một pseudo-element.content: ''cho phép pseudo-element có thể tác động đến layout.clear: bothchỉ định bên trái, bên phải của element không thể liền kề với phần tửfloatedở trước mặc dù trong cùngformat block.
4. Danh sách với số đếm
- Từ khoá: Counter in list
- Code:
HTML
<ul>
<li>List item</li>
<li>List item</li>
<li>
List item
<ul>
<li>List item</li>
<li>List item</li>
<li>List item</li>
</ul>
</li>
</ul>
CSS
ul {
counter-reset: counter;
}
li::before {
counter-increment: counter;
content: counters(counter, '.') ' ';
}
- Demo:

- Giải thích:
counter-resetkhởi tạo bộ đếm, giá trị là tên của bộ đếm. Mặc định bộ đếm sẽ bắt đầu đếm từ 0,propertynày có thể thay đổi giá trị thành bất cứ số nào.counter-increment: Sử dụng trong element cần được đếm, khicounter-resetđược khởi tạo thì giá trị của bộ đếm sẽ được tăng hoặc giảm tuỳ ý.counter(name, style): Hiển thị giá trị của bộ đếm, thông thường sẽ được sử dụng trongproperty content. Hàmcounternày cần 2 tham số, đầu tiên là tên của bộ đếm và tiếp theo làdecimalhoặcupper-roman(mặc định làdecimal).- counters(counter, string, style): Hiển thị giá trị của bộ đếm, tương tự ở
mục số 3, hàm này lại nhận 3 tham số, đầu tiên là tên của bộ đếm, tiếp theo bạn có thể thêm string (tuỳ ý) vào trước hoặc sau của bộ đếm, cuối cùng làdecimalhoặcupper-roman(mặc định làdecimal).
5. Custom lại scrollbar
- Từ khoá: Custom scrollbar
- Code:
HTML
<div class="custom-scrollbar">
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit.<br />
Iure id exercitationem nulla qui repellat laborum vitae, <br />
molestias tempora velit natus. Quas, assumenda nisi. <br />
Quisquam enim qui iure, consequatur velit sit?<br />
Lorem ipsum dolor sit amet consectetur adipisicing elit.<br />
Iure id exercitationem nulla qui repellat laborum vitae, <br />
molestias tempora velit natus. Quas, assumenda nisi. <br />
Lorem ipsum dolor sit amet consectetur adipisicing elit.<br />
Iure id exercitationem nulla qui repellat laborum vitae, <br />
molestias tempora velit natus. Quas, assumenda nisi.
</p>
</div>
CSS
.custom-scrollbar {
height: 70px;
overflow-y: scroll;
}
/* To style the document scrollbar, remove `.custom-scrollbar` */
.custom-scrollbar::-webkit-scrollbar {
width: 8px;
}
.custom-scrollbar::-webkit-scrollbar-track {
box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
border-radius: 10px;
}
.custom-scrollbar::-webkit-scrollbar-thumb {
border-radius: 10px;
box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.5);
}
- Demo:

- Giải thích:
::webkit-scrollbar: trỏ đến toàn bộ element của scrollbar.::webkit-scrollbar-track: trỏ đến phần trắng của scrollbar.::webkit-scrollbar-thumb: trỏ đến phần thanh cuộn của scrollbar.
6. Custom lại style của select text
- Từ khoá: Custom text selection
- Code:
HTML
<p class="custom-text-selection">Select some of this text.</p>
CSS
::selection {
background: aquamarine;
color: black;
}
.custom-text-selection::selection {
background: deeppink;
color: white;
}
- Demo:

- Giải thích:
::selectionđịnh nghĩapseudo selectortrong element để thay đổistylekhi select một đoạn text. Nếu chúng ta không thêm::selectionvào trong bất cứ element nào thì nó sẽ tác động đến phần tửroot, hay bất cứ element nào được select.
7. Không cho phép select text
- Từ khoá: Disable selection
- Code:
HTML
<p>You can select me.</p>
<p class="unselectable">You can't select me!</p>
CSS
.unselectable {
user-select: none;
}
- Demo:

- Giải thích:
user-select: nonelàm cho text không thể được select.
8. Hiển thị table vào giữa
- Từ khoá: Display table centering
- Code:
HTML
<div class="container">
<div class="center"><span>Centered content</span></div>
</div>
CSS
.unselectable {
user-select: none;
}
- Demo:

- Giải thích:
display: tablenằm bên trong class.centercho phép element trở thành giống tag<table>.- 100% height và width cho phép element có thể lấp đầy toàn bộ độ rộng của element cha.
display: table-cellbên trong.center > spancho phép element trở thành giống một HTML element.text-align: centerbên trong.center > spansẽ căn giữa theo chiều ngang.vertical-align: middlebên trong.center > spansẽ căn giữa theo chiều dọc.- Phần tử bọc ngoài (trong trường hợp này là
class .container) cần phải có width và height cố định.
Vậy là chúng ta đã điểm qua được 8 CSS nho nhỏ hay gặp trong quá trình làm dự án rồi. Cảm ơn các bạn đã theo dõi!
All rights reserved