Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

:not(:first-child) and :not(:first-of-type) not working

Tags:

html

css

I have sort of a tree system. What I'm trying to do is give all the parents a margin except for the first one. This is my HTML:

<div id="someDivID">     <div class="theBody">         <div class="someContainer">             <div id="someItem" class="someItemClass">                 Test             </div>         </div>         <div class="someContainer">             <div id="someItem2" class="someItemClass">                 Test2             </div>         </div>     </div> </div> 

And my CSS:

#someDivID {     width: 400px; }  #someItem, #someItem2 {     border: 1px solid #000;     padding: 1px;     margin-bottom: 2px;     clear: both;     overflow: auto; }  .someItemClass {     background-color: #0077FF; }  .someItemClass:not(:first-of-type) {     margin-top: 50px; } 

Now, my .someContainer has got the background color but the 2nd .someContainer doesn't have a top margin. If I remove the :first-of-type it works. :first-child doesn't work either.

Here's my jsfiddles:

With first-of-type: http://jsfiddle.net/JoshB1997/zsu2o3cg/

With first-child: http://jsfiddle.net/JoshB1997/zsu2o3cg/1/

like image 603
Joshua Bakker Avatar asked Dec 01 '15 10:12

Joshua Bakker


People also ask

What is a not (: first child?

ul:not(:first-child) means literally "any ul element that is not first child of its parent", so it won't match even the 1st ul if it's preceded by another element ( p , heading etc.). On the contrary, ul:not(:first-of-type) means "any ul element except the 1st ul in the container".

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

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.

Can I use not (: last child?

:not(:last-child)The :not() selector excludes the element passed to it from selection. The :last-child selector selects the last child. Combining these two above selector to excludes the last children (inner-div) of every parent div from the selection.

How do you ignore the first child in CSS?

I think :nth-child() will do the trick. This styles all of the p tags except for the first because it starts on the 2nd child. You could then style the first p tag separately with p:first-child .


1 Answers

That's because they are not siblings.

If you change the :not selector to the parent div, it will work.

.someContainer:not(:first-of-type) {     margin-top: 50px; } 

#someDivID  {      width: 400px;  }    #someItem,  #someItem2  {      border: 1px solid #000;      padding: 1px;      margin-bottom: 2px;      clear: both;      overflow: auto;  }    .someContainer  {      background-color: #0077FF;  }    .someContainer:not(:first-of-type)  {      margin-top: 50px;  }
<div id="someDivID">      <div class="theBody">          <div class="someContainer">              <div id="someItem" class="someItemClass">                  Test              </div>          </div>          <div class="someContainer">              <div id="someItem2" class="someItemClass">                  Test2              </div>          </div>      </div>  </div>
like image 181
Edward Avatar answered Oct 14 '22 05:10

Edward