I have been reading about CSS the last couple of days, and searched the internet for this question.
Could anyone please explain me whats the difference between (~) and (>)?
Simple selectors (select elements based on name, id, class) Combinator selectors (select elements based on a specific relationship between them) Pseudo-class selectors (select elements based on a certain state)
+ will only select the first element that is immediately preceded by the former selector. ~ selector all the sibling preceded by the former selector. Save this answer.
and hash(#) both of them are used as a CSS selector. Both selectors are used to select the content to set the style. CSS selectors select HTML elements according to its id, class, type, attribute, etc. Id selector(“#”): The id selector selects the id attribute of an HTML element to select a specific element.
A combinator is something that explains the relationship between the selectors. A CSS selector can contain more than one simple selector. Between the simple selectors, we can include a combinator. There are four different combinators in CSS: descendant selector (space)
General sibling means the element is after another element, where the child selector targets elements that are directly inside of certain elements.
Siblings:
HTML:
<div class="brother"></div>
<div class="sister"></div>
CSS:
.brother ~ .sister {
/* This styles .sister elements that follow .brother elements */
}
Children:
HTML
<div class="parent">
<div class="child">
<div class="child"></div>
</div>
</div>
CSS:
.parent > .child{
/* This styles .child element that is the direct child of the parent element;
It will not style the .child element that is the child of .child */
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With