Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

:last-of-type doesn't work

Tags:

html

css

Example GIF

I have a HTML document with the abbreviated HTML content:

<div class="list">
    <form name="signupForm">
      <label class="item item-input">
        <input>
      </label>
      <label class="item item-input">
        <input>
      </label>
      <label class="item item-input">
        <input>
      </label>
      <label class="item item-input">
        <input>
      </label>
      <label class="item item-button">
        <input class="button button-block button-positive">
      </label>
      <label class="item item-button">
        <input class="button button-block button-signup">
      </label>
    </form>
</div>

My expected behavior is that the CSS selector .item.item-input:last-of-type will grab the 4th label element and the last .item.item-input element of the form parent.

What am I doing wrong?

like image 585
Ian Avatar asked Apr 24 '15 23:04

Ian


People also ask

Does last-of-type work on classes?

class is not possible with last-of-type . Edit: To actually add something constructive to this answer, you could fallback to JavaScript to locate this selector, or revise your markup/CSS.

How do you use last-of-type in CSS?

The :last-of-type selector matches every element that is the last child, of a particular type, of its parent. Tip: This is the same as :nth-last-of-type(1).

How do I use not the last-of-type in CSS?

The :nth-last-of-type selector allows you select one or more elements based on their source order, according to a formula. 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 elements.

How do I select the last div element on the page?

To select the last element of a specific class, you can use the CSS :last-of-type pseudo-class.


1 Answers

Apparently :last-of-type (from the MDN Docs) can only be used with namespace and type selectors (tag name selectors like input).

I suggest changing your HTML structure to wrap the .item-input elements in a <div>. Alternatively, I have also seen a .last class manually added to substitute the pseudo-selector.

like image 64
Yatharth Agarwal Avatar answered Nov 15 '22 20:11

Yatharth Agarwal