Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the difference between General sibling combinator (~ )and child selector (>) in css

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 (>)?

like image 732
Mikkel Raicevic-Larsen Avatar asked Apr 04 '14 16:04

Mikkel Raicevic-Larsen


People also ask

What are the 3 different kinds of selectors in CSS?

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)

What is the difference between '+' and '~' sibling selectors?

+ 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.

What is difference between CSS selector?

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.

What is combinator selector in CSS?

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)


1 Answers

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 */
}
like image 173
Dryden Long Avatar answered Oct 13 '22 10:10

Dryden Long