Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between div~div and div:not(:first-of-type)?

Is there a difference between div~div and div:not(:first-of-type)? Aside from IE6 whatever errors, are there cases where they would do different things?

like image 243
Eric Wolf Avatar asked Nov 29 '14 03:11

Eric Wolf


People also ask

What is the difference between a div and a div class?

The main difference between div id and div class is that div id involves assigning an id attribute to a specific div element to apply styling or interactivity to that element while div class involves assigning the class attribute to several div elements to apply styling or interactivity to a set of div elements.

What is the difference between first child and first-of-type in CSS?

The :first-child: The :first-child selector is used to select those elements which are the first-child elements. For :first-child selector the <! DOCTYPE> must be declared for IE8 and earlier versions. The :first-of-type: The :first-of-type Selector is used to targeting the first child of every element of it's parent.

What is the difference between div and class in CSS?

A div is a container tag that is used to define a division or a section in an HTML document, whereas a class is an attribute that specifies actions to be taken to one or more elements. When using a class in CSS you have to specify the HTML elements you want it to affect by using a dot(.)

What is first-of-type in CSS?

The :first-of-type selector in CSS allows you to target the first occurence of an element within its container. It is defined in the CSS Selectors Level 3 spec as a “structural pseudo-class”, meaning it is used to style content based on its relationship with parent and sibling content.


1 Answers

In terms of matching elements, there is no difference. Any div that follows some other div within the same parent is, by necessity, not the first element of type div within its parent.

If the sibling selector were + and not ~, then div+div has the added condition that the following element must appear immediately after the preceding element. This is not the case with ~ — any number of siblings of any other kind may appear between the two.

If the pseudo-class were :first-child and not :first-of-type, then div:not(:first-child) will still match div:first-of-type if the first div within its parent is not its first child.

Here's an illustration:

<section>
    <div></div> <!-- div:first-child or div:first-of-type -->
    <div></div> <!-- div+div or div~div or div:nth-of-type(2) -->
    <p></p>
    <div></div> <!-- div+p+div or div~div or div:nth-of-type(3),
                     but not div+div -->
</section>
<section>
    <h1></h1>   <!-- h1:first-child -->
    <div></div> <!-- div:first-of-type or div:nth-child(2) -->
    <div></div> <!-- div~div or div:nth-of-type(2) or div:nth-child(3) -->
</section>

In terms of specificity, there is a difference. If you have CSS rules with both selectors matching the same elements, then div:not(:first-of-type) will take precedence due to the :first-of-type pseudo-class. Notice that the :not() itself is not counted — only its argument is considered.

Here's another illustration:

div {
    float: left;
    width: 50px;
    height: 50px;
    margin: 5px;
    border: 1px solid red;
}

/* 1 pseudo-class, 1 type -> specificity = 0-1-1 */
div:not(:first-of-type) {
    border-color: green;
}

/* 2 types                -> specificity = 0-0-2 */
div ~ div {
    border-color: blue;
}
<section>
    <div></div>
    <div></div>
    <div></div>
</section>
like image 103
BoltClock Avatar answered Oct 01 '22 21:10

BoltClock