Trong CSS, thuộc tính hiển thị xác định giao diện của một phần tử. Nó cũng là một phần quan trọng trong việc trình bày mã HTML của bạn vì nó có tác động đáng kể đến bố cục.
Trên thực tế, để sử dụng các mô hình Flexbox và Grid hiện đại, bạn cần sử dụng thuộc tính display trước khi có quyền truy cập vào các thuộc tính và giá trị khác nhau của chúng. Đây là một lý do tại sao thuộc tính hiển thị rất quan trọng trong CSS
Hãy đi sâu vào và tìm hiểu cách sử dụng thuộc tính
Lorem ipsum dolor sit amet consectetur adipisicing elit.
This is an inline lement Modi eaque debitis eos quod labore
maiores delectus asperiores voluptatem voluptas soluta!
9 và tất cả các giá trị khác nhau của nóCú pháp thuộc tính
Lorem ipsum dolor sit amet consectetur adipisicing elit.
This is an inline lement Modi eaque debitis eos quod labore
maiores delectus asperiores voluptatem voluptas soluta!
9 cơ bản
Lorem ipsum dolor sit amet consectetur adipisicing elit.
This is an inline lement Modi eaque debitis eos quod labore
maiores delectus asperiores voluptatem voluptas soluta!
element {
display: value;
}
Hiển thị giá trị thuộc tính trong CSS
Có các phần tử cấp độ khối và nội tuyến trong CSS. Sự khác biệt giữa hai loại này là các phần tử nội tuyến không chiếm toàn bộ không gian – nghĩa là chúng không bắt đầu trên một dòng mới – nhưng các phần tử khối thì có
Thuộc tính hiển thị có nhiều giá trị khác nhau, chẳng hạn như
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
1, body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
2, body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
3, body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
4, v.v., tất cả đều ảnh hưởng đến bố cục và cách trình bày của một thành phần trên trang web. Ngoài ra, để triển khai bố cục flex và grid, bạn cần sử dụng thuộc tính displayBạn có thể sử dụng thuộc tính hiển thị này để thay đổi phần tử
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
1 thành phần tử ________ 33, phần tử ________ 33 thành phần tử ________ 31, phần tử ________ 33 và body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
1 thành phần tử body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
2, v.v.
Lorem ipsum dolor sit amet consectetur adipisicing elit.
This is an inline lement Modi eaque debitis eos quod labore
maiores delectus asperiores voluptatem voluptas soluta!
42
Lorem ipsum dolor sit amet consectetur adipisicing elit.
This is an inline lement Modi eaque debitis eos quod labore
maiores delectus asperiores voluptatem voluptas soluta!
Một phần tử có thuộc tính hiển thị được đặt thành
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
1 sẽ không bắt đầu trên một dòng mới và nó sẽ chiếm chiều rộng màn hình còn lại/có sẵn. Nó chỉ chiếm không gian mà một phần tử như vậy thường chiếmDo đó, bạn không thể đặt
Lorem ipsum dolor sit amet consectetur adipisicing elit.
This is an inline lement Modi eaque debitis eos quod labore
maiores delectus asperiores voluptatem voluptas soluta!
44 và
Lorem ipsum dolor sit amet consectetur adipisicing elit.
This is an inline lement Modi eaque debitis eos quod labore
maiores delectus asperiores voluptatem voluptas soluta!
45 của phần tử có hiển thị là body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
1, vì nó không chiếm toàn bộ chiều rộng màn hìnhMột số phần tử là nội tuyến theo mặc định, như ________ 147, ________ 148, ________ 149 và ________ 220
Lorem ipsum dolor sit amet consectetur adipisicing elit.
This is an inline lement Modi eaque debitis eos quod labore
maiores delectus asperiores voluptatem voluptas soluta!
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
element {
display: value;
}
21
element {
display: value;
}
Một phần tử có thuộc tính hiển thị được đặt thành
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
3 bắt đầu trên một dòng mới và chiếm chiều rộng màn hình có sẵnBạn có thể chỉ định các thuộc tính
Lorem ipsum dolor sit amet consectetur adipisicing elit.
This is an inline lement Modi eaque debitis eos quod labore
maiores delectus asperiores voluptatem voluptas soluta!
44 và
Lorem ipsum dolor sit amet consectetur adipisicing elit.
This is an inline lement Modi eaque debitis eos quod labore
maiores delectus asperiores voluptatem voluptas soluta!
45 cho các phần tử đó. Ví dụ về các phần tử ở cấp độ khối theo mặc định là element {
display: value;
}
25, element {
display: value;
}
26, element {
display: value;
}
27, v.v.Bạn có thể đặt
element {
display: value;
}
28 từ mã HTML trước đó thành hiển thị body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
3 và nó sẽ hoạt động giống như một phần tử cấp khối
Lorem ipsum dolor sit amet consectetur adipisicing elit.
This is an inline lement Modi eaque debitis eos quod labore
maiores delectus asperiores voluptatem voluptas soluta!
4Bạn có thể thấy rằng
Lorem ipsum dolor sit amet consectetur adipisicing elit.
This is an inline lement Modi eaque debitis eos quod labore
maiores delectus asperiores voluptatem voluptas soluta!
47 chiếm toàn bộ chiều rộng. Đó là bởi vì nó có thuộc tính hiển thị được đặt thành chặn body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
61
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
Ngoài hiển thị khối và nội tuyến, còn có khối nội tuyến
Một phần tử bạn chỉ định hiển thị
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
2 là nội tuyến theo cách trình bày. Nhưng nó có thêm lợi thế là bạn có thể áp dụng
Lorem ipsum dolor sit amet consectetur adipisicing elit.
This is an inline lement Modi eaque debitis eos quod labore
maiores delectus asperiores voluptatem voluptas soluta!
44 và
Lorem ipsum dolor sit amet consectetur adipisicing elit.
This is an inline lement Modi eaque debitis eos quod labore
maiores delectus asperiores voluptatem voluptas soluta!
45 cho nó, điều mà bạn không thể làm được khi phần tử được gán phân tán là body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
1Vì vậy, bạn có thể xem màn hình
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
2 dưới dạng phần tử nội tuyến và phần tử khối trong một góielement {
display: value;
}
2 body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
67
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
Khi bạn đặt thuộc tính hiển thị của một phần tử thành
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
68, phần tử đó sẽ bị xóa hoàn toàn khỏi trang và không ảnh hưởng đến bố cụcĐiều này cũng có nghĩa là các thiết bị như trình đọc màn hình giúp người mù có thể truy cập trang web sẽ không có quyền truy cập vào phần tử
Đừng nhầm lẫn giữa
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
67 với body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
60. Cái sau cũng ẩn phần tử, nhưng để lại khoảng trống mà nó thường mở hoặc để trống body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
6Chế độ hiển thị bị ẩn khiến không gian bị phần tử span chiếm giữ mở, như bạn có thể thấy bên dưới
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
6 body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
61
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
Ngày nay, bạn sẽ hiếm khi sử dụng giá trị hiển thị là
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
4, nhưng điều quan trọng cần biết là. Trước đây, nó hữu ích hơn vì bạn sẽ sử dụng nó cho các bố cục trước khi các float, Flex và Grid ra đờiĐặt hiển thị thành
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
4 làm cho phần tử hoạt động giống như một bảng. Vì vậy, bạn có thể tạo một bản sao của bảng HTML mà không cần sử dụng phần tử bảng và các phần tử tương ứng, chẳng hạn như body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
64 và body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
65Ví dụ: trong HTML, bạn có thể tạo một bảng có phần tử
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
66 và cả phần tử element {
display: value;
}
25 hoặc bất kỳ vùng chứa nào bạn chọnBạn tạo một bảng với phần tử HTML
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
66 như thế này body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
7 body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
8Kết quả của đoạn mã HTML và CSS ở trên trông như thế này
Nhưng bạn có thể tạo cùng một bảng với phần tử
element {
display: value;
}
25 bằng cách đặt các màn hình tương ứng thành body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
4, body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
71 và body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
72. Bạn sẽ nhận được kết quả tương tự như bạn có thể thấy bên dưới body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
3
Lorem ipsum dolor sit amet consectetur adipisicing elit.
This is an inline lement Modi eaque debitis eos quod labore
maiores delectus asperiores voluptatem voluptas soluta!
0Bạn vẫn nhận được bàn của bạn
Các giá trị khác của Thuộc tính hiển thị
Ngoài
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
1, body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
3, body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
68 và body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
4 thực sự quan trọng vì chúng ảnh hưởng đáng kể đến giao diện của các trang web, còn có các giá trị khác của thuộc tính
Lorem ipsum dolor sit amet consectetur adipisicing elit.
This is an inline lement Modi eaque debitis eos quod labore
maiores delectus asperiores voluptatem voluptas soluta!
9 đáng để bạn chú ýMột số trong số chúng bạn sẽ sử dụng mọi lúc mà không thực sự nhận ra rằng chúng cũng là một phần của thuộc tính hiển thị. Và những thứ khác bạn sẽ không sử dụng thường xuyên
Bây giờ chúng ta hãy nhìn vào một số trong số họ
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
78
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
Màn hình của
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
79 cung cấp cho bạn quyền truy cập vào hệ thống bố cục Flex, giúp đơn giản hóa cách chúng tôi thiết kế và bố cục các trang web của mình
Lorem ipsum dolor sit amet consectetur adipisicing elit.
This is an inline lement Modi eaque debitis eos quod labore
maiores delectus asperiores voluptatem voluptas soluta!
1
Lorem ipsum dolor sit amet consectetur adipisicing elit.
This is an inline lement Modi eaque debitis eos quod labore
maiores delectus asperiores voluptatem voluptas soluta!
2 body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
80
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
Màn hình được đặt thành
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
81 cho phép bạn tạo bố cục bằng hệ thống lưới, giống như một dạng flex nâng cao
Lorem ipsum dolor sit amet consectetur adipisicing elit.
This is an inline lement Modi eaque debitis eos quod labore
maiores delectus asperiores voluptatem voluptas soluta!
1
Lorem ipsum dolor sit amet consectetur adipisicing elit.
This is an inline lement Modi eaque debitis eos quod labore
maiores delectus asperiores voluptatem voluptas soluta!
4 body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
82
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
Điều này làm cho phần tử kế thừa thuộc tính hiển thị của cha mẹ nó. Vì vậy, nếu bạn có thẻ
Lorem ipsum dolor sit amet consectetur adipisicing elit.
This is an inline lement Modi eaque debitis eos quod labore
maiores delectus asperiores voluptatem voluptas soluta!
47 bên trong div và bạn hiển thị thẻ span là body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
84, nó sẽ biến nó từ phần tử nội tuyến thành phần tử khối
Lorem ipsum dolor sit amet consectetur adipisicing elit.
This is an inline lement Modi eaque debitis eos quod labore
maiores delectus asperiores voluptatem voluptas soluta!
5
Lorem ipsum dolor sit amet consectetur adipisicing elit.
This is an inline lement Modi eaque debitis eos quod labore
maiores delectus asperiores voluptatem voluptas soluta!
6 body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
85
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 2rem;
}
div {
max-width: 600px;
}
span {
background-color: #006100;
}
Điều này đặt thuộc tính hiển thị của một phần tử thành giá trị mặc định của nó. Vì vậy, nếu bạn đặt thuộc tính hiển thị của một khoảng thành ban đầu, thì nó vẫn ở trong dòng và nếu bạn đặt cùng một giá trị cho một div, thì nó vẫn bị chặn
Lorem ipsum dolor sit amet consectetur adipisicing elit.
This is an inline lement Modi eaque debitis eos quod labore
maiores delectus asperiores voluptatem voluptas soluta!
5
Lorem ipsum dolor sit amet consectetur adipisicing elit.
This is an inline lement Modi eaque debitis eos quod labore
maiores delectus asperiores voluptatem voluptas soluta!
8Phần kết luận
Nắm bắt tốt thuộc tính hiển thị sẽ giúp bố cục trang của bạn trông đẹp mắt. Nó cũng cung cấp cho bạn nhiều quyền kiểm soát hơn đối với cách bạn trình bày các phần tử của mình khi làm việc với CSS
Bạn có thể tiếp tục quay lại bài viết này để tham khảo vì thuộc tính display lúc đầu luôn khó hiểu cho đến khi bạn sử dụng nó đủ để hiểu đầy đủ về nó
Tôi hy vọng bài viết này đã cung cấp cho bạn kiến thức cơ bản cần thiết để sử dụng tốt thuộc tính display
Cảm ơn bạn đã đọc và tiếp tục mã hóa
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
Nhà phát triển web và nhà văn kỹ thuật tập trung vào các công nghệ giao diện người dùng
Nếu bạn đọc đến đây, hãy tweet cho tác giả để cho họ thấy bạn quan tâm. Tweet một lời cảm ơn
Học cách viết mã miễn phí. Chương trình giảng dạy mã nguồn mở của freeCodeCamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu