Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why :first-child selector not working on div?

Tags:

html

css

I am selecting specific divs with the :first-child and :last-child pseudo selectors but :first-child is not working in any browser I have checked it in.

I've checked caniuse.com and css-tricks.com and the consensus is that :first-child is pretty widely supported so I'm thinking maybe there is some bug I am not aware of. I am running the app on a local node server. I have validated both my css and my html. Is anyone aware of any bugs that might prevent :first-child from working?

HTML

<div class="col-md-6 blurbs">
 <label>NEWS</label>
 <div>
  <p>Lorem ipsum dolor spernatur aut odit aut fugit conse oluptate</p>
 </div>
 <div class="clearfix">
  <a class="pull-left">RSS</a>
  <a class="pull-right">MORE NEWS</a>
 </div>
 </div>

CSS

(not working)

.news .blurbs div:first-child{
    outline: 1px solid red;
    height: 260px;
    overflow: auto;
    margin-bottom: 10px;
}

(working)

.news .blurbs div:last-child{
    outline: 1px solid red;
    width: 95%;
}
like image 519
user1457366 Avatar asked Nov 12 '13 21:11

user1457366


People also ask

How do I get my first child in a div?

The :first-child checks if its subject is the first child of its parent. In thise case you are trying to select element with id #LeftScrollableDiv that is the first child of its parent. All the : selectors just filter previous selection, they don't select any new elements.

How do I select the first child of a div in CSS?

The :first-child selector is used to select the specified selector, only if it is the first child of its parent.

Is pseudo selector The first child?

The first-child is a pseudo class in CSS which represents the first element among a group of sibling elements. The :first-child Selector is used to target the first child element of it's parent for styling. Example: HTML.


1 Answers

The :first-child and :last-child pseudo-classes select the element that is the first/last child of the parent, filtered by any other chained selector, so div:first-child does not actually match anything as the first child within .blurbs is not a div.

What you need to use to get the effect you are after is the :first-of-type pseudo-class like this:

.news .blurbs div:first-of-type{
    outline: 1px solid red;
    height: 260px;
    overflow: auto;
    margin-bottom: 10px;
}

here is a working example

like image 76
Ken Herbert Avatar answered Oct 05 '22 19:10

Ken Herbert