You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
3.0 KiB
3.0 KiB
CSS - Advanced Selectors
Lesson Objectives
- Explain what a pseudo selector is
- Style link and form elements depending on their "state"
- Style the first and last elements of a container
- Style an element depending on what child number it is of its parent
- Style an element if it is the only child of its parent
- Negate a selector
Explain what a pseudo selector is
There are in general two kinds of pseudo selectors
- Ones that select elements with specific "states"
- Ones that select certain elements of a set based on where they are in the tree
Style link and form elements depending on their "state"
- :hover
- selects elements that have a mouse "hovering" on top of them
- :active
- selects elements that have been clicked on
- :focus
- you can use Tab to jump between links and form fields
- when you do this, the currently selected link is "focused" on
- :visited
- selects links that have been visited
- :checked
- selects radio button and check boxes in forms that have been selected
- usually not used to style the input directly, but style sibling elements (https://css-tricks.com/the-checkbox-hack/)
Style the first and last elements of a container
- :first-child
- selects only elements that are the first child of their parent
- :first-of-type
- selects only elements that are the first of their tag type of their parent
<section> <div>hi</div> <p>paragraph</p> </section>section :first-of-type { background:yellow; /* both div and p tags will have a yellow background */ } - :last-child
- selects only elements that are the last child of their parent
- :last-of-type
- selects only elements that are the last of their tag type of their parent
Style an element depending on what child number it is of its parent
selectors
- :nth-child()
- selects only elements that are the nth child of their parent, where n is specified inside the parentheses
- :nth-of-type()
- selects only elements that are the nth of their tag type of their parent, where n is specified inside the parentheses
- :nth-last-child()
- selects only elements that are the nth to last child of their parent, where n is specified inside the parentheses
- :nth-last-of-type()
- selects only elements that are the nth to last of their tag type of their parent, where n is specified inside the parentheses
what can N be?
- a number (1st element is 1, not 0)
evenorodd- a formula in the form of
an + b- you specify
aandbas integers - if an element's index equals the value of that equation for some integer n, the rule applies that element
- examples
- 3n - indexes selected: 3, 6, 9
- 3n+1 - indexes selected: 1, 4, 7, 10
- you specify
Style an element if it is the only child of its parent
- :only-child
- selects elements that are the sole child of their parent
- :only-of-type
- selects elements that are the sole child of their tag type of their parent
Negate a selector
- :not()
- selects everything except whatever elements are selected by the selector inside the parentheses