Tiện ích Bootstrap 4
Tiện ích Bootstrap 4
Bootstrap 4 có rất nhiều lớp tiện ích/trợ giúp để nhanh chóng tạo kiểu cho các phần tử mà không cần sử dụng bất kỳ mã CSS nào.
Biên giới
Sử dụng các lớp border
để thêm hoặc xóa đường viền khỏi một phần tử:
Ví dụ
Ví dụ
<span class="border"></span>
<span class="border border-0"></span>
<span class="border border-top-0"></span>
<span class="border border-right-0"></span>
<span
class="border border-bottom-0"></span>
<span class="border border-left-0"></span>
Hãy tự mình thử »Màu viền
Thêm màu vào đường viền bằng bất kỳ lớp màu đường viền theo ngữ cảnh nào:
Ví dụ
Ví dụ
<span class="border border-primary"></span>
<span class="border
border-secondary"></span>
<span class="border border-success"></span>
<span class="border border-danger"></span>
<span class="border
border-warning"></span>
<span class="border border-info"></span>
<span class="border border-light"></span>
<span class="border
border-dark"></span>
<span class="border border-white"></span>
Hãy tự mình thử »Bán kính đường viền
Thêm các góc tròn vào một phần tử có các lớp rounded
:
Ví dụ
Ví dụ
<span class="rounded-sm"></span>
<span class="rounded"></span>
<span class="rounded-lg"></span>
<span class="rounded-top"></span>
<span class="rounded-right"></span>
<span
class="rounded-bottom"></span>
<span class="rounded-left"></span>
<span class="rounded-circle"></span>
<span
class="rounded-0"></span>
Hãy tự mình thử » Float và Clearfix
Di chuyển một phần tử sang phải bằng lớp .float-right
hoặc sang trái bằng .float-left
và xóa phần nổi bằng lớp .clearfix
:
Ví dụ
Ví dụ
<div class="clearfix">
<span class="float-left">Float left</span>
<span
class="float-right">Float right</span>
</div>
Hãy tự mình thử »Phao đáp ứng
Di chuyển một phần tử sang trái hoặc sang phải tùy thuộc vào chiều rộng màn hình, với các lớp float đáp ứng ( .float-*-left|right
- trong đó * là sm
(>=576px), md
(>=768px), lg
(> =992px) hoặc xl
(>=1200px)):
Ví dụ
Ví dụ
<div class="float-sm-right">Float right on small screens or wider</div><br>
<div class="float-md-right">Float right on medium screens or wider</div><br>
<div class="float-lg-right">Float right on large screens or wider</div><br>
<div class="float-xl-right">Float right on extra large screens or
wider</div><br>
<div class="float-none">Float none</div>
Hãy tự mình thử »Căn giữa
Căn giữa một phần tử với lớp .mx-auto
(thêm lề trái và lề phải: auto):
Ví dụ
Ví dụ
<div class="mx-auto
bg-warning" style="width:150px">Centered</div>
Hãy tự mình thử »Chiều rộng
Đặt độ rộng của một phần tử với các lớp w-* ( .w-25
, .w-50
, .w-75
, .w-100
, .mw-100
):
Ví dụ
Ví dụ
<div class="w-25 bg-warning">Width 25%</div>
<div class="w-50 bg-warning">Width
50%</div>
<div class="w-75 bg-warning">Width 75%</div>
<div
class="w-100 bg-warning">Width 100%</div>
<div class="mw-100 bg-warning">Max Width 100%</div>
Hãy tự mình thử »Chiều cao
Đặt chiều cao của một phần tử với các lớp h-* ( .h-25
, .h-50
, .h-75
, .h-100
, .mh-100
):
Ví dụ
Ví dụ
<div style="height:200px;background-color:#ddd">
<div class="h-25 bg-warning">Height 25%</div>
<div class="h-50 bg-warning">Height
50%</div>
<div class="h-75 bg-warning">Height 75%</div>
<div
class="h-100 bg-warning">Height 100%</div>
<div class="mh-100 bg-warning"
style="height:500px">Max Height 100%</div>
</div>
Hãy tự mình thử »khoảng cách
Bootstrap 4 có một loạt các lớp tiện ích đệm và lề đáp ứng. Chúng hoạt động với tất cả các điểm dừng: xs
(<=576px), sm
(>=576px), md
(>=768px), lg
(>=992px) hoặc xl
(>=1200px)):
Các lớp được sử dụng theo định dạng: {property}{sides}-{size}
cho xs
và {property}{sides}-{breakpoint}-{size}
cho sm
, md
, lg
và xl
.
Trường hợp tài sản là một trong:
-
m
- đặtmargin
-
p
- bộpadding
Trong đó các bên là một trong:
-
t
- đặtmargin-top
hoặcpadding-top
-
b
- đặtmargin-bottom
hoặcpadding-bottom
-
l
- đặtmargin-left
hoặcpadding-left
-
r
- đặtmargin-right
hoặcpadding-right
-
x
- đặt cảpadding-left
vàpadding-right
hoặcmargin-left
vàmargin-right
-
y
- đặt cảpadding-top
vàpadding-bottom
hoặcmargin-top
vàmargin-bottom
- trống - đặt
margin
hoặcpadding
trên cả 4 cạnh của phần tử
Trong đó kích thước là một trong:
-
0
- đặtmargin
hoặcpadding
thành0
-
1
- đặtmargin
hoặcpadding
thành.25rem
(4px nếu cỡ chữ là 16px) -
2
- đặtmargin
hoặcpadding
thành.5rem
(8px nếu cỡ chữ là 16px) -
3
- đặtmargin
hoặcpadding
thành1rem
(16px nếu cỡ chữ là 16px) -
4
- đặtmargin
hoặcpadding
thành1.5rem
(24px nếu cỡ chữ là 16px) -
5
- đặtmargin
hoặcpadding
thành3rem
(48px nếu cỡ chữ là 16px) -
auto
- đặtmargin
thành tự động
Lưu ý: lề cũng có thể âm bằng cách thêm "n" vào trước size :
-
n1
- đặtmargin
thành-.25rem
(-4px nếu cỡ chữ là 16px) -
n2
- đặtmargin
thành-.5rem
(-8px nếu cỡ chữ là 16px) -
n3
- đặtmargin
thành-1rem
(-16px nếu cỡ chữ là 16px) -
n4
- đặtmargin
thành-1.5rem
(-24px nếu cỡ chữ là 16px) -
n5
- đặtmargin
thành-3rem
(-48px nếu cỡ chữ là 16px)
Ví dụ
Ví dụ
<div class="pt-4 bg-warning">I only have a top padding (1.5rem =
24px)</div>
<div class="p-5 bg-success">I have a padding on all sides
(3rem = 48px)</div>
<div class="m-5 pb-5 bg-info">I have a margin on
all sides (3rem = 48px) and a bottom padding (3rem = 48px)</div>
Hãy tự mình thử »Thêm ví dụ về khoảng cách
.m-# / m-*-# | lề ở tất cả các bên | Thử nó |
.mt-# / mt-*-# | lề trên | Thử nó |
.mb-# / mb-*-# | lề dưới | Thử nó |
.ml-# / ml-*-# | lề trái | Thử nó |
.mr-# / mr-*-# | lề phải | Thử nó |
.mx-# / mx-*-# | lề trái và phải | Thử nó |
.my-# / my-*-# | lề trên và dưới | Thử nó |
.p-# / p-*-# | đệm ở tất cả các mặt | Thử nó |
.pt-# / pt-*-# | đệm đầu | Thử nó |
.pb-# / pb-*-# | đệm đáy | Thử nó |
.pl-# / pl-*-# | phần đệm bên trái | Thử nó |
.pr-# / pr-*-# | đệm bên phải | Thử nó |
.py-# / py-*-# | đệm trên và dưới | Thử nó |
.px-# / px-*-# | đệm trái và phải | Thử nó |
Bóng tối
Sử dụng các lớp shadow-
để thêm bóng vào một phần tử:
Ví dụ
Ví dụ
<div class="shadow-none p-4 mb-4 bg-light">No shadow</div>
<div
class="shadow-sm p-4 mb-4 bg-white">Small
shadow</div>
<div class="shadow p-4 mb-4 bg-white">Default
shadow</div>
<div class="shadow-lg p-4 mb-4 bg-white">Large
shadow</div>
Hãy tự mình thử »Căn dọc
Sử dụng các lớp align-
để thay đổi cách căn chỉnh các phần tử (chỉ hoạt động trên các phần tử nội tuyến, khối nội tuyến, bảng nội tuyến và ô bảng):
Ví dụ
Ví dụ
<span class="align-baseline">baseline</span>
<span
class="align-top">top</span>
<span class="align-middle">middle</span>
<span class="align-bottom">bottom</span>
<span
class="align-text-top">text-top</span>
<span
class="align-text-bottom">text-bottom</span>
Hãy tự mình thử »Nhúng đáp ứng
Tạo nội dung nhúng video hoặc trình chiếu đáp ứng dựa trên chiều rộng của phần gốc.
Thêm .embed-responsive-item
vào bất kỳ phần tử nhúng nào (như <iframe> hoặc <video>) trong phần tử gốc có .embed-responsive
và tỷ lệ khung hình mà bạn chọn:
Ví dụ
Ví dụ
<!-- 21:9 aspect ratio -->
<div class="embed-responsive
embed-responsive-21by9">
<iframe class="embed-responsive-item"
src="..."></iframe>
</div>
<!-- 16:9 aspect ratio -->
<div class="embed-responsive
embed-responsive-16by9">
<iframe class="embed-responsive-item"
src="..."></iframe>
</div>
<!-- 4:3 aspect ratio -->
<div class="embed-responsive
embed-responsive-4by3">
<iframe class="embed-responsive-item"
src="..."></iframe>
</div>
<!-- 1:1 aspect ratio -->
<div class="embed-responsive
embed-responsive-1by1">
<iframe class="embed-responsive-item"
src="..."></iframe>
</div>
Hãy tự mình thử »Hiển thị
Sử dụng các lớp .visible
hoặc .invisible
để kiểm soát mức độ hiển thị của các phần tử. Lưu ý: Các lớp này không thay đổi giá trị hiển thị CSS. Họ chỉ thêm visibility:visible
hoặc visibility:hidden
:
Ví dụ
Ví dụ
<div class="visible">I am visible</div>
<div class="invisible">I am
invisible</div>
Hãy tự mình thử »Chức vụ
Sử dụng lớp .fixed-top
để cố định/giữ nguyên phần tử ở đầu trang:
Ví dụ
<nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top">
...
</nav>
Hãy tự mình thử » Sử dụng lớp .fixed-bottom
để cố định/giữ nguyên phần tử ở cuối trang:
Ví dụ
<nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-bottom">
...
</nav>
Hãy tự mình thử » Sử dụng lớp .sticky-top
để cố định/giữ nguyên phần tử ở đầu trang khi bạn cuộn qua nó. Lưu ý: Lớp này không hoạt động trong IE11 trở về trước (sẽ coi nó là position:relative
).
Ví dụ
<nav class="navbar navbar-expand-sm bg-dark navbar-dark sticky-top">
...
</nav>
Hãy tự mình thử »Đóng biểu tượng
Sử dụng lớp .close
để tạo kiểu cho biểu tượng đóng. Điều này thường được sử dụng cho các cảnh báo và phương thức. Lưu ý rằng chúng tôi sử dụng ×
để tạo biểu tượng thực tế (chữ "x" trông đẹp hơn). Cũng lưu ý rằng nó nổi ngay theo mặc định:
Trình đọc màn hình
Sử dụng lớp .sr-only
để ẩn một phần tử trên tất cả các thiết bị, ngoại trừ trình đọc màn hình:
Ví dụ
<span class="sr-only">I will be hidden on all screens except for screen
readers.</span>
Hãy tự mình thử »Màu sắc
Như được mô tả trong chương Màu sắc , đây là danh sách tất cả các lớp màu văn bản và màu nền:
Các lớp dành cho màu văn bản là: .text-muted
, .text-primary
, .text-success
, .text-info
, .text-warning
, .text-danger
, .text-secondary
, .text-white
, .text-dark
, .text-body
(màu nội dung mặc định/thường là màu đen) và .text-light
:
Ví dụ
This text is muted.
This text is important.
This text indicates success.
This text represents some information.
This text represents a warning.
This text represents danger.
Secondary text.
Dark grey text.
Body text.
Light grey text.
Hãy tự mình thử »Các lớp văn bản theo ngữ cảnh cũng có thể được sử dụng trên các liên kết, điều này sẽ thêm màu di chuột tối hơn:
Ví dụ
Muted link.
Primary link.
Success link.
Info link.
Warning link.
Danger link.
Secondary link.
Dark grey link.
Body/black link.
Light grey link.
Hãy tự mình thử » Bạn cũng có thể thêm độ mờ 50% cho văn bản đen hoặc trắng bằng các lớp .text-black-50
hoặc .text-white-50
:
Ví dụ
Black text with 50% opacity on white background
White text with 50% opacity on black background
Hãy tự mình thử »Màu nền
Các lớp dành cho màu nền là: .bg-primary
, .bg-success
, .bg-info
, .bg-warning
, .bg-danger
, .bg-secondary
, .bg-dark
và .bg-light
.
Lưu ý rằng màu nền không đặt màu văn bản, vì vậy trong một số trường hợp, bạn sẽ muốn sử dụng chúng cùng với lớp .text-*
.
Ví dụ
This text is important.
This text indicates success.
This text represents some information.
This text represents a warning.
This text represents danger.
Secondary background color.
Dark grey background color.
Light grey background color.
Hãy tự mình thử »Lớp học kiểu chữ/văn bản
Như được mô tả trong chương Kiểu chữ , đây là danh sách tất cả các lớp kiểu chữ/văn bản:
Class | Description | Example |
---|---|---|
.display-* |
Display headings are used to stand out more than normal headings (larger font-size and lighter font-weight), and there are four classes to choose from: .display-1 , .display-2 , .display-3 , .display-4 |
Try it |
.font-weight-bold |
Bold text | Try it |
.font-weight-bolder |
Bolder bold text | Try it |
.font-weight-normal |
Normal text | Try it |
.font-weight-light |
Light weight text | Try it |
.font-weight-lighter |
Lighter weight text | Try it |
.font-italic |
Italic text | Try it |
.lead |
Makes a paragraph stand out | Try it |
.small |
Indicates smaller text (set to 85% of the size of the parent) | Try it |
.text-break |
Prevents long text from breaking layout | Try it |
.text-center |
Indicates center-aligned text | Try it |
.text-decoration-none |
Removes the underline from a link | Try it |
.text-left |
Indicates left-aligned text | Try it |
.text-justify |
Indicates justified text | Try it |
.text-monospace |
Monospaced text | Try it |
.text-nowrap |
Indicates no wrap text | Try it |
.text-lowercase |
Indicates lowercased text | Try it |
.text-reset |
Resets the color of a text or a link (inherits the color from its parent) | Try it |
.text-right |
Indicates right-aligned text | Try it |
.text-uppercase |
Indicates uppercased text | Try it |
.text-capitalize |
Indicates capitalized text | Try it |
.initialism |
Displays the text inside an <abbr> element in a slightly smaller font size |
Try it |
.list-unstyled |
Removes the default list-style and left margin on list items (works on both <ul> and <ol> ). This class only applies to immediate children list items (to remove the default list-style from any nested lists, apply this class to any nested lists as well) |
Try it |
.list-inline |
Places all list items on a single line (used together with
.list-inline-item on each <li> elements) |
Try it |
.pre-scrollable |
Makes a <pre> element scrollable |
Try it |
Khối phần tử
Để biến một phần tử thành phần tử khối, hãy thêm lớp .d-block
. Sử dụng bất kỳ lớp d-*-block
nào để kiểm soát KHI phần tử phải là phần tử khối trên chiều rộng màn hình cụ thể:
Ví dụ
Ví dụ
<span class="d-block bg-success">d-block</span>
<span class="d-sm-block
bg-success">d-sm-block</span>
<span class="d-md-block bg-success">d-md-block</span>
<span class="d-lg-block bg-success">d-lg-block</span>
<span
class="d-xl-block bg-success">d-xl-block</span>
Hãy tự mình thử »Các lớp hiển thị khác
Các lớp hiển thị khác cũng có sẵn:
Class | Description | Example |
---|---|---|
.d-none |
Hides an element | Try it |
.d-*-none |
Hides an element on a specific screen size | Try it |
.d-inline |
Makes an element inline | Try it |
.d-*-inline |
Makes an element inline on a specific screen size | Try it |
.d-inline-block |
Makes an element inline block | Try it |
.d-*-inline-block |
Makes an element inline block on a specific screen size | Try it |
.d-table |
Makes an element display as a table | Try it |
.d-*-table |
Makes an element display as a table on a specific screen size | Try it |
.d-table-cell |
Makes an element display as a table cell | Try it |
.d-*-table-cell |
Makes an element display as a table cell on a specific screen size | Try it |
.d-table-row |
Makes an element display as a table row | Try it |
.d-*-table-row |
Makes an element display as a table row on a specific screen size | Try it |
.d-flex |
Creates a flexbox container and transforms direct children into flex items | Try it |
.d-*-flex |
Creates a flexbox container on a specific screen size | Try it |
.d-inline-flex |
Creates an inline flexbox container | Try it |
.d-*-inline-flex |
Creates an inline flexbox container on a specific screen size | Try it |
Uốn cong
Sử dụng các lớp .flex-*
để kiểm soát bố cục bằng flexbox.
Đọc thêm về Bootstrap 4 Flex trong chương tiếp theo của chúng tôi .
Ví dụ
Horizontal:
Vertical: