- vừa được xem lúc

[CSS] Selectors

0 0 8

Người đăng: Vivian

Theo Viblo Asia

Quick review about CSS selectors

  • There are 5 categories of CSS selectors
  1. Simple selectors - based on name, id, class
  2. Combinator selectors - based on elements' relationship
  3. Pseudo-class selectors - based on state
  4. Pseudo-elements selectors - selects and style a part of an element
  5. Attribute selectors - based on an attribute or attribute value

I. Simple selectors

Selector Example Description
#id #main-heading Selects elem with id="main-heading"
.class .container Selects all elem with class="container"
element.class div.container Selects only div elems with class="container"
* - universal selector * Selects all elems
element p Selects all <p> elems
element, element div, p Selects all <div> and <p> elems

II. Combination selectors

1. Child selectors

  • Select direct child: body > ol
  • Select child and grandchild using whitespace: body li

2. Adjacent selector using +

  • Selects element which comes directly after another element.
  • Selects p element which comes after h1: h1 + p

3. Sibling selector using ~

  • Selects all elements which comes after an element
  • Selects all p elements which comes after h1: h1 ~ p

III. Pseudo-class selectors: selector:pseudo-class-keyword{style}

  • Display state
pseudo-class-keyword Description
:fullscreen Matches elem is currently in fullscreen mode
:modal -
:picture-in-picture -
  • Input
pseudo-class-keyword Description
:autofill Matches an input tag is autofilled
:enable -
:disable -
:read-only -
:read-write -
:placeholder-shown Matches an input elem which displays placeholder text
:default -
:checked Matches checkboxes and radio btn are checked
:interminate -
:blank -
:valid -
:invalid -
:in-range -
:out-of-range -
:required -
:optional -
:user-invalid -
  • Behavioral - user action
pseudo-class-keyword Description
:hover Matches when the mouse is pointing to an item
:active Matches when the item is clicked
:visited Matches when the link is visited
:focus Matches when an elem has focus
  • Structural
pseudo-class-keyword Description
:only-child Matches elem which has no siblings
:first-child Matches elem which is the first of its siblings
:last-child Matches elem which is the last of its siblings
:first-of-tytpe Matches elem which is the first of its siblings and matches a specific type
:last-of-type Matches elem which is the last of its siblings and matches a specific type
:nth-child(number/even/odd/formular ex: 2n +1 ) Using formula to select elems from a list of sibling elems
:nth-last-child(number/even/odd/formular ex: 2n +1 ) Using formula to select elems from a list of sibling elems, counting backwards
:nth-of-type Using formula to select elems from a list of sibling elems matching a specific type
:nth-last-type-of Using formula to select elems from a list of sibling elems matching a specific type, couting backwards
  • Functional
pseudo-class-keyword Example Description
:is(selectors) :is(ol,ul) Matches elems which matches any of the selectors provided in the list
:not(selectors) :not(:placeholder-shown) Selects elems which dont match a list of selectors
:where(selectors) :where(ol, ul) Same as :is but :where has 0 specificity so its easy to be override by simple selector
:has(relative selectors) h1:has( + p) Selects parent elem or prev sibling elems ( Selects and applies style on h1 which is followed by p

IV. Pseudo-elements selectors selector::pseudo-element{style}

pseudo-element-keyword Description
::after Creates a pseudo elem which is the last child of the selected elem
::before Creates a pseudo elem which is the first child of the selected elem
::first-letter Applies style on the first letter of the first line of block-level elem
::first-line Applies style on the first line of block-level elem
::placeholder Applies style on the placeholder text of elem
::selection Applies style on the elem which is highlighted by user

V. Attribute selectors using []

  • Selects a tag which has a title: a[title]
  • Selects a tag which its title value is click me: a[title="click me"]
  • Selects div tag which has one of classes named container: div[class~="container"]
  • Selects a tag which its href attr ends with html : a[href$="html"]
  • Selects a tag which its href attr starts with http: a[href^="http"]

Bình luận

Bài viết tương tự

- vừa được xem lúc

Thủ thuật nhỏ để căn chỉnh image với object-fit

Chào các bạn,. Có lẽ trong hành trình code của các bạn thì không ít lần gặp vấn đề méo ảnh do fix cứng cả width, height của ảnh nhỉ? Hoặc kể cả khi bạn set value cho 1 thuộc tính weigth hoặc height còn thuộc tính còn lại để auto thì nhiều lúc ảnh cũng không được hiển thị toàn vẹn cho lắm.

0 0 34

- vừa được xem lúc

Tìm hiểu về CSS framework - Bulma

Mở đầu:. Mấy bữa nay đang lướt web thấy có giới thiệu framework bulma này, được mọi người giới thiệu gọn nhẹ và dễ sử dụng, nên mình mới tìm hiểu thử và hôm nay xin viết 1 bài viết giới thiệu sơ qua với các bạn.

0 0 25

- vừa được xem lúc

Một số mẹo vặt "hay ho" của ES6 có thể bạn chưa biết - Phần 4

Xin chào, ở 3 bài trước của series "Một số mẹo vặt "hay ho" của ES6", mình đã chia sẻ 1 số tips/tricks nhỏ với ES6, hy vọng ít nhiều nó sẽ có ích với các bạn khi áp dụng vào thực tế. Hôm nay, xin mời các bạn theo dõi phần 4 của series này.

0 0 30

- vừa được xem lúc

Tìm hiểu về Jest Mocks Test phía frontend

Giới thiệu. Chắc hẳn không ai phủ nhận rằng UnitTest là 1 phần quan trọng trong giai đoạn phát triển phần mềm, đảm bảo cho code được coverage tránh các bug không mong muốn.

0 0 24

- vừa được xem lúc

Convert từ SVG sang Icon Font như thế nào?

Chào các bạn. Như câu hỏi trên title của bài viết, hôm nay mình sẽ hướng dẫn các bạn cách convert 1 file svg 1 cách khá đơn giản và vô cùng tiện lợi cho các bạn. https://icomoon.io/app/#/select.

0 0 40

- vừa được xem lúc

Một vài thủ thuật làm việc với các dạng layout - Phần 4

. Chào mọi người, cũng đã lâu rồi mình không thấy nhau. Để tiếp tục với series's về các dạng layout hôm nay mình sẽ chia sẻ thêm một trick thú vị nữa về step layout.

0 0 31