3

I am facing an issue when using the :last-child pseudo selector.

I have the following markup:

   <div class="apply_container">
        <form class="margin">
            <div class="apply_inn border">
                <span>Tell me why you want the job?</span>
            </div>
            <div class="apply_inn">
                <span>Some other details</span>
            </div>
            <div class="apply_inn location">
                <span>Apply at a particular location</span>
            </div>
            <div class="form-actions">
                <button type="submit" class="pull-right btn btn-info">
                    Submit
                </button>
            </div>
        </form>
    </div>

And apply these styles:

.apply_container .apply_inn {
    border-bottom: 1px dashed #E6E6E6;
    margin: auto;
    padding: 18px 0;
    width: 790px;
}

.apply_container .apply_inn:last-child {
    border:none;
}

My goal is to prevent the last div.apply_inn from being styled with a bottom-border like the rest of the div.apply_inns. The style does not seem to get applied. Can anyone explain what is happening?

Here is the original fiddle of my problem. As well as a simplified fiddle demonstrating the issue.

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
Chandrakant
  • 1,971
  • 1
  • 14
  • 33

4 Answers4

5

The problem is that the div with class .apply_inn is not the last-child within its parent. The CSS last-child pseudo-class operates as follows:

The :last-child CSS pseudo-class represents any element that is the last child element of its parent.

When ready very literally, it will only apply to an element that is the last child within its parent. It does not take into consideration the context you (mentally) create when you add the additional class selectors to it.

When applying the pseudo-class the browser doesn't understand the context created by the selector. Basically, its checking that the element matches the selector .apply_container .apply_inn, then asking the question, "Is this the last child within the parent?". It asks this question without any consideration of the aforementioned selector. In your case, the answer is no since there is another div after the last div.apply_inn.

In your example, the div with the class form-actions is actually the last child.

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • 1
    The same can be said for each part of a compound selector. `div` = "Is this a `div`?", `.apply_inn` = "Does this have a class `apply_inn`?", `:last-child` = "Is this the last child of its parent?" The element has to match all of these conditions, but independently of every other condition. – BoltClock Mar 21 '13 at 09:55
  • @BoltClock Great supplementing info! From a theoretical perspective, CSS boils down to a bunch of questions being asked by the browser. I'm guessing mastering CSS is all about learning the specifics of the questions the browser is asking/answering. – Kevin Bowersox Mar 21 '13 at 10:03
2

You can only use the last-child selector if it is the last child of it's parent container - although it was the last child with that class name it wasn't the last child of the container

https://developer.mozilla.org/en-US/docs/CSS/:last-child

Here is a fiddle showing your code with the last child style applied

http://jsfiddle.net/QSeU2/7/

Pete
  • 57,112
  • 28
  • 117
  • 166
  • Yes. There isn't a way to select the last element with the class name so one has to rely on workarounds such as those listed in the other answers. – BoltClock Mar 21 '13 at 09:35
  • the div with `apply_inn` is the first element of it's parent container (`margin`) – Pete Mar 21 '13 at 09:37
  • Ohh ok..Now Resolved all puzzles regarding last and first child, Thank you guys :) – Chandrakant Mar 21 '13 at 09:50
0

target for border-top and remove the first-child's border-top

http://jsfiddle.net/btevfik/QSeU2/6/

last-child doesn't work.

btevfik
  • 3,391
  • 3
  • 27
  • 39
  • yea it's good solution, I appreciate, but I'm trying to find out the reason - why last-child is not working, always this thing happens with me :) – Chandrakant Mar 21 '13 at 09:32
  • 2
    The reason is because the last `.apply_inn` is not the last child. There is another element without that class after the last one. – BoltClock Mar 21 '13 at 09:32
  • @BoltClock- I did't getting you- in .apply_container div, number of .apply_inn is approx 5-6, and I want to select last one from those 5-6. – Chandrakant Mar 21 '13 at 09:35
  • 1
    @Chandrakant: Yes but that last one has another element after it so it's not the last child. – BoltClock Mar 21 '13 at 09:40
  • @BoltClock :last-child is used to select the last element that matches the preceding selector. So in this case, it should match the last div with the .apply_in class. – Vimal Stan Mar 21 '13 at 09:50
  • @Vimal Stan: Um, no. I'm not sure where you pulled that from. – BoltClock Mar 21 '13 at 09:52
  • @VimalStan thats what i thought too. but apperantly that's not how it works. if i were to do it, i would do it that way as well. – btevfik Mar 21 '13 at 09:52
  • @BoltClock The definition says "The :last-child selector matches every element that is the last child of its parent." I assumed that it would work the way I described. Thanks for the correction. Learnt something new today :) – Vimal Stan Mar 21 '13 at 09:56
-1

Related: Using the last-child selector


Any reason you don't want to use

.location {border:none;}

?

Community
  • 1
  • 1
Vimal Stan
  • 2,007
  • 1
  • 12
  • 14