Bạn có thể lồng vào CSS không?
Giải thích cách lồng và nhóm trong CSS Show Cải thiện bài viết Lưu bài viết Thích bài viết
Cải thiện bài viết Lưu bài viết Khái niệm Nesting và Grouping rất quan trọng đối với nhà phát triển web để viết mã chính xác. Bạn có thể nhóm và lồng các mục để giảm số lượng mã bạn viết, điều này sẽ giảm độ dài mã của bạn và cho phép các trang tải nhanh hơn. Đó là một cách để đơn giản hóa mã của bạn. Với sự trợ giúp của Nesting và Grouping, chúng ta có thể cụ thể hơn trong mã của mình. Trong bài viết này, chúng ta sẽ xem cách lồng & nhóm giúp tối ưu hóa mã và làm cho mã hiệu quả cũng như tăng khả năng đọc làm tổ. Thuộc tính lồng nhau trong CSS tạo điều kiện lồng một quy tắc kiểu bên trong một quy tắc kiểu khác, với bộ chọn của quy tắc con có liên quan đến bộ chọn của quy tắc cha. Nó giúp tăng tính mô-đun và khả năng bảo trì của các biểu định kiểu CSS và do đó tăng khả năng đọc mã tổng thể. Chẳng hạn, nếu bạn viết một mô-đun CSS có cấu trúc, thay vì chỉ định các bộ chọn riêng biệt cho mọi phần tử HTML, tức là bằng cách sử dụng nhiều lớp hoặc bộ chọn ID, bạn có thể chỉ cần chỉ định các thuộc tính cho các bộ chọn trong các bộ chọn khác. Trong khi lồng các thuộc tính CSS, các phần tử HTML tạo thành một hình dạng cấu trúc cây. Nesting là một lối tắt để tạo quy tắc CSS cho nhiều bộ chọn cho một thuộc tính cụ thể. Vì vậy, thay vì viết lại cùng một bộ thuộc tính cho các bộ chọn khác nhau, chúng ta có thể chỉ cần lồng các bộ chọn bên trong các bộ chọn khác. Vì lý do này, chúng tôi không chỉ giảm kích thước của mã mà còn giảm thời gian tải tổng thể cú pháp class1_selector class2_selector id_selector { property: value; } Ví dụ table tr th { background-color: beige; }
Tiếp cận
Ghi chú. Hãy cụ thể với thứ tự lồng nhau Example: In this example, we will nest the tag inside the tag and tag inside the | HTMLtable tr th { background-color: beige; }02 ________ 103 ________ 104 ________ 105 table tr th { background-color: beige; }06 table tr th { background-color: beige; }03 table tr th { background-color: beige; }0 table tr th { background-color: beige; }05 table tr th { background-color: beige; }2____103 table tr th { background-color: beige; }4 table tr th { background-color: beige; }05 table tr th { background-color: beige; }6 table tr th { background-color: beige; }7 table tr th { background-color: beige; }8 table tr th { background-color: beige; }9 table tr th { background-color: beige; }6 table tr th { background-color: beige; }01 table tr th { background-color: beige; }06 table tr th { background-color: beige; }6 table tr th { background-color: beige; }04 table tr th { background-color: beige; }8 table tr th { background-color: beige; }06 table tr th { background-color: beige; }6 table tr th { background-color: beige; }01 table tr th { background-color: beige; }2____180____14 table tr th { background-color: beige; }05 table tr th { background-color: beige; }80 table tr th { background-color: beige; }0____105 table tr th { background-color: beige; }06 table tr th { background-color: beige; }03 table tr th { background-color: beige; }88 table tr th { background-color: beige; }05 table tr th { background-color: beige; }06 table tr th { background-color: beige; }2____103 table tr th { background-color: beige; }023 table tr th { background-color: beige; }05 table tr th { background-color: beige; }6 table tr th { background-color: beige; }026 table tr th { background-color: beige; }6 table tr th { background-color: beige; }03____1029 table tr th { background-color: beige; }030 table tr th { background-color: beige; }031 table tr th { background-color: beige; }032 table tr th { background-color: beige; }033 table tr th { background-color: beige; }029 table tr th { background-color: beige; }05 table tr th { background-color: beige; }6 table tr th { background-color: beige; }037 table tr th { background-color: beige; }2____180____1023 table tr th { background-color: beige; }05 table tr th { background-color: beige; }06 table tr th { background-color: beige; }2____103 table tr th { background-color: beige; }045 table tr th { background-color: beige; }4 table tr th { background-color: beige; }031 table tr th { background-color: beige; }048 table tr th { background-color: beige; }05 table tr th { background-color: beige; }6 table tr th { background-color: beige; }03____1052 table tr th { background-color: beige; }05 table tr th { background-color: beige; }8 table tr th { background-color: beige; }03____1056 table tr th { background-color: beige; }057 table tr th { background-color: beige; }056 table tr th { background-color: beige; }05 table tr th { background-color: beige; }8 table tr th { background-color: beige; }03____1056 table tr th { background-color: beige; }063 table tr th { background-color: beige; }056 table tr th { background-color: beige; }05 table tr th { background-color: beige; }6 table tr th { background-color: beige; }80____1052 table tr th { background-color: beige; }05 table tr th { background-color: beige; }6 table tr th { background-color: beige; }03____1052 table tr th { background-color: beige; }05 table tr th { background-color: beige; }8 table tr th { background-color: beige; }03____1036 table tr th { background-color: beige; }037 table tr th { background-color: beige; }036 table tr th { background-color: beige; }05 table tr th { background-color: beige; }8 table tr th { background-color: beige; }03____1036 table tr th { background-color: beige; }03 table tr th { background-color: beige; }036 table tr th { background-color: beige; }05 table tr th { background-color: beige; }6 table tr th { background-color: beige; }80____1052 table tr th { background-color: beige; }05 table tr th { background-color: beige; }6 table tr th { background-color: beige; }03____1052 table tr th { background-color: beige; }05 table tr th { background-color: beige; }8 table tr th { background-color: beige; }03____1036 table tr th { background-color: beige; }17 table tr th { background-color: beige; }036 table tr th { background-color: beige; }05 table tr th { background-color: beige; }8 table tr th { background-color: beige; }03____1036 table tr th { background-color: beige; }23 table tr th { background-color: beige; }036 table tr th { background-color: beige; }05 table tr th { background-color: beige; }6 table tr th { background-color: beige; }80____1052 table tr th { background-color: beige; }05 table tr th { background-color: beige; }6 table tr th { background-color: beige; }03____1052 table tr th { background-color: beige; }05 table tr th { background-color: beige; }8 table tr th { background-color: beige; }03____1036 table tr th { background-color: beige; }37 table tr th { background-color: beige; }036 table tr th { background-color: beige; }05 table tr th { background-color: beige; }8 table tr th { background-color: beige; }03____1036 table tr th { background-color: beige; }43 table tr th { background-color: beige; }036 table tr th { background-color: beige; }05 table tr th { background-color: beige; }6 table tr th { background-color: beige; }80____1052 table tr th { background-color: beige; }05 table tr th { background-color: beige; }2____180____1045 table tr th { background-color: beige; }05 table tr th { background-color: beige; }80 table tr th { background-color: beige; }88 table tr th { background-color: beige; }05 table tr th { background-color: beige; }06 table tr th { background-color: beige; }80 table tr th { background-color: beige; }04 table tr th { background-color: beige; }05 Output: We got tag as green color and of beige color using nesting. | làm tổ nhóm. Nhóm được sử dụng để chọn nhiều phần tử cùng nhau để áp dụng các thuộc tính tạo kiểu chung cho chúng. Vì lý do này, nó giúp giảm độ dài của mã có nhiều bộ chọn có cùng thuộc tính. Điều này làm cho mã dễ đọc. Thời gian tải trang và thời gian phát triển mã cũng giảm khi sử dụng nhóm. Thay vì viết đoạn mã dài này, hãy chỉ định các thuộc tính giống nhau cho các bộ chọn khác nhau table tr th { background-color: beige; }0 Chúng ta có thể nhóm chúng lại và viết như thế này và chúng ta cần dấu phẩy (,) để nhóm các bộ chọn khác nhau table tr th { background-color: beige; }8 Tiếp cận
|