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

人盡茶涼 提交于 2020-12-25 13:17:34

问题


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


回答1:


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 */
}



回答2:


Some examples are shown below to show how the CSS selectors are used...
Example:

div>p

The above, selects all p elements where the parent is a div element Example:

p~ul

The above, selects every ul element that are proceeded by a p element



来源:https://stackoverflow.com/questions/22868288/the-difference-between-general-sibling-combinator-and-child-selector-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!