There are multiple types of selectors we can use to target elements in CSS
- Universal * All properties are inherited to every element, used for base resets
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; } html { font-size: 62.5%; /* changes the rem = 10px for ease */ }
- Type Used to target by element name. The same selector
p { color: orange; }
- Class Used to group similar types together
.bold { font-weight: bold; }
- ID Used to target unique elements
#header { margin-bottom: 1rem; }
- Attribute Let’s say there are many input elements and we want to target the ones with password field
input[type=”password”] { border: 1px solid black; }
- Descendant All the child no matter how level deep. This will target all the p tags inside div any level nested
div p { color: red; }
- Direct Child Will target all the paragraphs within the div tag that are direct child
div > p { color: red; }
- Adjacent Sibling
It means the very next element on same level. All the
ulthat are directly after the paragraph
p + ul{ color: red; }
- General Sibling
Selects all the elements that are next siblings of a specified element. This will target all the
pafterh1
h1 ~ p { color: red; }
- Multiple Selector (Grouping) This targets multiple
ul, p, h1 {}
Pseudo Class (:)
- Used to target different states like when mouse enters a certain element, etc.
:hover(on mouse hover),:focus(for input elements on tab keypress),:active(click),:nth-child(2n)for even items
Pseudo Elements (::)
before,after,first-line,first-letter,selection(When the text is selected)
::after,::beforeis used to add fake content using CSS for stylistic purposes. Only shows up after the actual content. Suppose we have a box div with some content and then after will show the content after the text not the box.
el::after { content: "Hey"; }