Explaining about CSS Selector
Cascading Style Sheets: A CSS selector is the part of a CSS rule set that actually selects the content you want to style.
CSS Syntax: A CSS rule-set consists of a selector and a declaration block:
Explaining about CSS Selector
- The selector points to the HTML element you want to style.
- The declaration block contains one or more declarations separated by semicolons.
- Each declaration includes a CSS property name and a value, separated by a colon.
- A CSS declaration always ends with a semicolon, and declaration blocks are surrounded by curly braces.
- In the following example all <p>elements will be center-aligned, with a red text color
“previous sibling” in CSS selector
- Cascading Style Sheets : A cascade is like a waterfall, there’s no backward motion. So, naturally, there is no previous sibling selector in CSS.
- However by using flexbox, a previous sibling selector can be simulated.
- The flex order property can move elements around the screen.
- The element A to turn red when element B is hovered.
STEPS :
Step 1: Make the ul a flex container.
Step 2: Reverse the order of siblings in the mark-up.
Step 3: Use a sibling selector to target Element A (~ or + will do) .
Step 4: Use the flex order property to restore the order of siblings on the visual display.
Two Outdated Beliefs about CSS
- Flexbox is shattering long-held beliefs about CSS.
- A previous sibling selector is not possible in CSS
- If you know the exact position an :nth-child()-based exclusion of all following siblings would work.
- You can also select the nth-child right-to-left:
Previous sibling, the missing CSS selector
- A way to style all previous siblings (opposite of ~) that may work depending on what you need.
- Let’s say you have a list of links and when hovering on one, all the previous ones should turn red. You can do it like this:
Syntax idea :
- Since parent element > child element is used to select child elements with a specified parent and preceding element(s) ~ element is used to select elements that are preceded by a specified element.
- Using element < previous element would be a bad format as because of the > use it could be confused with referring to the parent element.
- The format would be element – previous element as it doesn’t seem to be in use and relates to use of the + symbol for “next”.
- CSS 2.1 has some really handy selectors, one of which is the adjacent (next) sibling selector which has the form:
- The above would apply a tasty pink(ish) text colour to el2 where it directly follows el1 in HTML element order.
- The glaring omission (as far as i can see) in the CSS selectors currently available though is the exact opposite selector, previous-sibling which might perhaps have syntax:
- The obvious way to style el2 where it occurs directly before el1 with that same delightful pink(ish) text colour.
HTML source:
- There is also currently a non-direct sibling selector which uses a tilde in place of the plus, the opposite of this could perhaps be:
Good One