Trình đơn thả xuống HTML
Menu thả xuống giúp người dùng dễ dàng điều hướng các trang web và ứng dụng web. Với CSS, chúng tôi có thể tạo một menu thả xuống tuyệt vời, đáp ứng nhanh, có thể truy cập cho cả người dùng bàn phím và chuột Show
Phương pháp thả xuống CSS trong bài học này sử dụng
Mặc dù các kỹ thuật này không yêu cầu JavaScript, nhưng bạn có thể cải thiện việc triển khai chúng bằng JavaScript, đặc biệt đối với người dùng trình đọc màn hình Chúng tôi sẽ đề cập đến những điều sau đây trong bài học này
Bản xem trước danh sách thả xuống của chúng tôi với dự án CSSVào cuối bài học này, chúng ta sẽ có một menu thả xuống đáp ứng với CSS trông như thế này Xem menu thả xuống Bút bằng CSS của Ibadehin Mojeed (@ibaslogic) Bạn có thể tương tác với dự án và sau đó, hãy bắt đầu Đánh dấu menu thả xuốngXây dựng menu thả xuống có thể truy cập bằng CSS bắt đầu bằng cách sử dụng đánh dấu HTML5 ngữ nghĩa và cấu trúc menu để hướng dẫn người dùng đến đích của họ Đoạn mã dưới đây thiết lập một số điều hướng đơn giản với ba mục menu chính — input[type="checkbox"] { opacity: 0; /* .. */ }0, input[type="checkbox"] { opacity: 0; /* .. */ }1 và input[type="checkbox"] { opacity: 0; /* .. */ }2. Mục input[type="checkbox"] { opacity: 0; /* .. */ }1 chứa danh sách thả xuống với ba mục menu con input[type="checkbox"] { opacity: 0; /* .. */ }4 lồng nhau Bằng cách sử dụng các mục danh sách không có thứ tự, trình đọc màn hình sẽ biết có bao nhiêu liên kết trong điều hướng. Đầu ra sẽ giống như vậy Hộp kiểm có trong mã và như được thấy trong hình ảnh sẽ được sử dụng để chuyển đổi danh sách thả xuống chính trên màn hình nhỏ hơn Thiết kế menu thả xuống ưu tiên thiết bị di động với CSSChúng tôi sẽ bắt đầu bằng cách định kiểu điều hướng cho màn hình nhỏ hơn. Trong tệp CSS, chúng tôi sẽ thêm các kiểu sau * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; } Bây giờ chúng ta sẽ có một menu thả xuống giống như vậy Hãy đi vào mã trong phần tiếp theo Điều gì đang xảy ra trong CSS?Sử dụng CSS input[type="checkbox"] { opacity: 0; /* .. */ }5 trên bộ chọn lớp input[type="checkbox"] { opacity: 0; /* .. */ }6, chúng tôi đã đặt menu thả xuống điều hướng tương ứng với thanh tiêu đề, như trong hình trên Sau đó, chúng tôi đã sử dụng lớp giả input[type="checkbox"] { opacity: 0; /* .. */ }7 và input[type="checkbox"] { opacity: 0; /* .. */ }8 để chuyển đổi hộp kiểm đầu vào thành menu bánh hamburger tùy chỉnh để kích hoạt danh sách thả xuống trên màn hình nhỏ hơn Xin lưu ý rằng chúng tôi đã sử dụng input[type="checkbox"] { opacity: 0; /* .. */ }9 để ẩn hộp kiểm đầu vào thay vì thuộc tính * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }40 input[type="checkbox"] { opacity: 0; /* .. */ } Trong khi đó, với mã bên dưới, biểu tượng bánh hamburger có thể nhận tiêu điểm khi người dùng bàn phím điều hướng qua phím tab * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }4 Chuyển đổi khối thả xuốngViệc cho phép người dùng chuyển đổi menu thả xuống có thể cải thiện đáng kể trải nghiệm người dùng — đặc biệt là trên màn hình di động nhỏ hơn, nơi menu luôn hiển thị sẽ che khuất hầu hết hoặc tất cả chế độ xem Trước tiên, chúng tôi sẽ ẩn danh sách thả xuống bằng cách sử dụng thuộc tính CSS * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }41 * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }8 Sau đó, bật menu thả xuống khi nhấp vào bánh hamburger * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }9 Quy tắc CSS ở trên nhắm mục tiêu trình đơn thả xuống input[type="checkbox"] { opacity: 0; /* .. */ }4 thả xuống là phần tử con trực tiếp của phần tử * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }43 sau hộp kiểm đầu vào. Các hành vi nên giống như vậy Sử dụng lớp giả * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; } 8Chúng tôi sẽ sử dụng CSS * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }8 để kích hoạt các mục thả xuống của input[type="checkbox"] { opacity: 0; /* .. */ }1 khi mục menu input[type="checkbox"] { opacity: 0; /* .. */ }1 nhận được tiêu điểm hoặc đang được nhấp vào Hãy cập nhật bộ chọn lớp * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }48 để ẩn các mục thả xuống input[type="checkbox"] { opacity: 0; /* .. */ }1 input[type="checkbox"] { opacity: 0; /* .. */ }8 Sau đó, thêm quy tắc này để kích hoạt danh sách thả xuống input[type="checkbox"] { opacity: 0; /* .. */ }9 Quy tắc CSS ở trên khớp với danh sách thả xuống nếu phần tử gốc * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }80 được đặt tiêu điểm. Nói cách khác, menu con sẽ hiển thị nếu chúng ta nhấp vào mục input[type="checkbox"] { opacity: 0; /* .. */ }1 hoặc tập trung vào mục đó thông qua phím tab Thêm chỉ báo thả xuốngChúng tôi sẽ thêm một mũi tên thả xuống vào mục input[type="checkbox"] { opacity: 0; /* .. */ }1 để cho biết rằng có tồn tại một danh sách thả xuống. Hãy cập nhật mục input[type="checkbox"] { opacity: 0; /* .. */ }1 để bao gồm một phần tử span có tên lớp * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }84 input[type="checkbox"] { opacity: 0; /* .. */ }5 Trong CSS, chúng tôi sẽ thêm quy tắc kiểu cho bộ chọn lớp input[type="checkbox"] { opacity: 0; /* .. */ }6 Nếu chúng tôi lưu, chúng tôi sẽ đặt mũi tên bên cạnh mục menu input[type="checkbox"] { opacity: 0; /* .. */ }1, như vậy Để xoay mũi tên lên trên khi menu thả xuống được mở, chúng tôi sẽ thêm quy tắc CSS sau * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }0 Thực hiện chuyển đổi sang danh sách thả xuốngChúng tôi sẽ thêm hiệu ứng chuyển tiếp mượt mà khi người dùng mở hoặc đóng menu thả xuống. Bắt đầu với menu thả xuống chính, chúng tôi sẽ cập nhật input[type="checkbox"] { opacity: 0; /* .. */ }6 để bao gồm các thuộc tính CSS của * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }87 và * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }88 * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }1 Sau đó, khi danh sách thả xuống hoạt động, chúng tôi sẽ sử dụng thuộc tính CSS * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }89 để di chuyển danh sách thả xuống trở lại vị trí ban đầu * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }2 Tiếp theo, chúng tôi sẽ thêm chuyển đổi vào danh sách thả xuống input[type="checkbox"] { opacity: 0; /* .. */ }1 và chỉ báo mũi tên của nó. Hãy cập nhật * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }48 để chuyển đổi thuộc tính * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }92 như vậy * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }3 Sau đó, đối với mũi tên, chúng tôi sẽ chuyển đổi thuộc tính * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }87 để chúng tôi có thông tin sau * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }4 Nếu chúng ta lưu CSS, trình đơn thả xuống sẽ hoạt động như vậy Tăng cường trình đơn thả xuống CSS cho trình đọc màn hìnhViệc thêm các thuộc tính ARIA vào danh sách thả xuống sẽ giúp trình đọc màn hình chuyển tải hành vi và mục đích dự định cho người dùng khiếm thị Chúng tôi sẽ thêm thuộc tính * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }94 vào các phần tử tương tác để phần mềm trình đọc màn hình có thể thông báo mục đích của điều khiển trong khi điều hướng qua menu thả xuống. Trong khi đó, thuộc tính * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }95 sẽ thông báo cho trình đọc màn hình rằng có một cửa sổ bật lên Thuộc tính * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }96 sẽ ánh xạ phần tử điều khiển tới tiện ích mở rộng. Chúng tôi sẽ đặt * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }97 của tiện ích mở rộng thành giá trị của thuộc tính * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }96 trên phần tử kiểm soát Hơn 200 nghìn nhà phát triển sử dụng LogRocket để tạo ra trải nghiệm kỹ thuật số tốt hơnCuối cùng, * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }99 sẽ cho trình đọc màn hình biết liệu trình đơn thả xuống hiện có bị ẩn hay không. Nó cần chuyển đổi giữa input[type="checkbox"] { opacity: 0; /* .. */ }80 hoặc input[type="checkbox"] { opacity: 0; /* .. */ }81 tùy thuộc vào trạng thái của danh sách thả xuống. Đây là một nơi chúng tôi có thể cải thiện khả năng truy cập thả xuống bằng JavaScript Với việc triển khai CSS của chúng tôi, lớp giả * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }8 sẽ ngay lập tức mở danh sách thả xuống khi người dùng khiếm thị di chuyển tiêu điểm đến mục thả xuống. Đối với điều này, bằng cách gán giá trị Boolean của input[type="checkbox"] { opacity: 0; /* .. */ }80 cho * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }99, phần mềm của trình đọc màn hình sẽ diễn giải chính xác tiện ích thả xuống Chúng ta có thể cài đặt tiện ích mở rộng Trình đọc màn hình của Chrome để quan sát diễn giải Hãy cập nhật đánh dấu HTML để bao gồm các thuộc tính ARIA * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }5 Nếu chúng ta sử dụng bàn phím để điều hướng menu thả xuống, hành vi sẽ giống như vậy Như chúng ta có thể thấy, khi chúng ta di chuyển tiêu điểm đến mục input[type="checkbox"] { opacity: 0; /* .. */ }1, trình đơn thả xuống sẽ mở ra ngay lập tức. Do đó, trình đọc màn hình sẽ thông báo chính xác phần mở rộng Thiết kế menu thả xuống cho màn hình lớn hơn bằng CSSChúng tôi sẽ sử dụng truy vấn phương tiện CSS và xác định quy tắc kiểu cho chiều rộng màn hình từ input[type="checkbox"] { opacity: 0; /* .. */ }86 trở lên * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }6 Trong CSS ở trên, chúng tôi đã bắt đầu bằng cách thêm một input[type="checkbox"] { opacity: 0; /* .. */ }87 vào nội dung tiêu đề để định vị logo và các menu điều hướng cạnh nhau. Sau đó, chúng tôi đã thêm một khai báo input[type="checkbox"] { opacity: 0; /* .. */ }88 vào nội dung điều hướng để ghi đè vị trí tuyệt đối mà chúng tôi đã sử dụng cho thiết kế di động và đặt các mục điều hướng trong quy trình bình thường của trang Cũng cần lưu ý cách chúng tôi đưa vào các quy tắc để kích hoạt danh sách thả xuống khi di chuột với lớp giả input[type="checkbox"] { opacity: 0; /* .. */ }89. Kết quả bây giờ trông giống như vậy Hỗ trợ trợ năng cho trình duyệt SafariTheo mặc định, tính năng sử dụng phím tab để điều hướng bị tắt trong Safari. Chúng tôi có thể kích hoạt nó bằng cách mở Tùy chọn Safari và mở Cài đặt nâng cao của chúng tôi. Sau đó, trong phần Khả năng truy cập, chúng ta có thể chọn hộp kiểm có nhãn “Nhấn Tab để tô sáng từng mục trên trang web. ” Chúng tôi cũng có thể nhận thấy rằng việc nhấp vào mục menu input[type="checkbox"] { opacity: 0; /* .. */ }1 không hiển thị danh sách thả xuống trong Safari. Một phần tử trên Safari sẽ nhận được một * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }8 nếu nó có một input[type="checkbox"] { opacity: 0; /* .. */ }92. Vì vậy, chúng tôi sẽ cập nhật * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }80 cho input[type="checkbox"] { opacity: 0; /* .. */ }1 để bao gồm một input[type="checkbox"] { opacity: 0; /* .. */ }95 * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; } header { position: relative; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.07), 0 1px 2px 0 rgba(0, 0, 0, 0.05); } .header-content { align-items: center; max-width: 1200px; margin: 0 auto; padding: 10px 20px; color: #212529; } .logo { text-decoration: none; font-size: 25px; color: inherit; margin-right: 20px; } label { padding: 23px 20px; position: absolute; cursor: pointer; right: 0; top: 0; } input[type="checkbox"] { opacity: 0; position: absolute; right: 0; } label span { width: 20px; height: 3px; display: block; background: #4f3e3e; position: relative; } label span::after, label span::before { content: ""; position: absolute; display: block; background: inherit; width: inherit; height: inherit; } label span::before{ top: 8px; } label span::after { bottom: 8px; } label::before { position: absolute; content: ""; width: 58px; height: 49px; top: 0; right: 0; } input[type="checkbox"]:focus + label::before { box-shadow: 0 0 20px black; } ul { background: #f2f2f2; } ul li { list-style: none; font-size: 18px; } ul li button { font-size: inherit; border: none; background-color: transparent; cursor: pointer; width: 100%; } ul li a { display: block; color: inherit; text-decoration: none; } ul li a, ul li button { padding: 0.7rem 1rem; text-align: left; } .menus { position: absolute; top: 3.2rem; left: 0; right: 0; } .dropdown { padding: 2px 1.5rem; }7 Danh sách thả xuống bây giờ sẽ hoạt động như mong đợi Phần kết luậnCó thể tạo một menu thả xuống tuyệt vời bằng CSS, như được hiển thị trong bài học này. Người dùng bàn phím, chuột và trình đọc màn hình có thể truy cập menu thả xuống. Chúng tôi có thể cải thiện hơn nữa các menu thả xuống CSS bằng cách sử dụng ngôn ngữ kịch bản như JavaScript Menu thả xuống là một mẫu thiết kế phổ biến trong các ứng dụng hiện đại. Để tìm hiểu thêm về chúng, bạn có thể xem cách xây dựng menu thả xuống đa cấp trong React Nếu bạn có câu hỏi hoặc đóng góp, xin vui lòng chia sẻ suy nghĩ của bạn trong phần bình luận. Cuối cùng, hãy cố gắng chia sẻ bài học này trên web Giao diện người dùng của bạn có ngốn CPU của người dùng không?Khi giao diện người dùng web ngày càng phức tạp, các tính năng ngốn tài nguyên ngày càng đòi hỏi nhiều hơn từ trình duyệt. Nếu bạn quan tâm đến việc giám sát và theo dõi mức sử dụng CPU phía máy khách, mức sử dụng bộ nhớ, v.v. cho tất cả người dùng của bạn trong sản xuất, hãy thử LogRocket.LogRocket giống như một DVR dành cho ứng dụng web và thiết bị di động, ghi lại mọi thứ xảy ra trong ứng dụng web hoặc trang web của bạn. Thay vì đoán lý do tại sao sự cố xảy ra, bạn có thể tổng hợp và báo cáo về các chỉ số hiệu suất chính của giao diện người dùng, phát lại các phiên của người dùng cùng với trạng thái ứng dụng, ghi lại các yêu cầu mạng và tự động hiển thị tất cả các lỗi Làm cách nào để tạo menu thả xuống trong HTML?Cách tạo Menu thả xuống trong HTML . Step 1: Add a Step 2: Add a Step 3: Create Bước 4. Thêm một giá trị mặc định từ danh sách thả xuống, nếu muốn Làm cách nào để tạo menu thả xuống?Use any element to open the dropdown menu, e.g. a |