Questions
39 of 50
1What is a pseudo-class in CSS?
2How are pseudo-classes different from pseudo-elements?
3What is the syntax of a pseudo-class in CSS?
4Give some commonly used pseudo-classes.
5What does the :hover pseudo-class do?
6What’s the purpose of the :active pseudo-class?
7How does the :visited pseudo-class work for links?
8What is the :focus pseudo-class used for?
9What does the :checked pseudo-class do?
10How does the :disabled pseudo-class behave?
11What is the difference between :first-child and :first-of-type?
12How do :nth-child() and :nth-of-type() differ?
13How do you select every odd or even element using pseudo-classes?
14How can you style an element when it’s being hovered over along with its child?
15What does the :not() pseudo-class do?
16How can you use multiple pseudo-classes together?
17What is the difference between :link and :visited?
18What does the :target pseudo-class represent?
19How can you use :empty to detect empty elements?
20How does the :root pseudo-class differ from the html selector?
21What does the :valid and :invalid pseudo-class do?
22How can you use :required and :optional pseudo-classes in forms?
23What is the use of :in-range and :out-of-range?
24What does :read-only and :read-write do?
25How can you style an input field when it’s autofilled?
26How does the :focus-within pseudo-class work?
27What’s the difference between :focus and :focus-visible?
28What is the purpose of the :placeholder-shown pseudo-class?
29How can you style a checkbox when it is checked or unchecked?
30How can you style invalid form inputs without JavaScript?
31Can pseudo-classes be chained together? Give an example.
32What is the specificity of pseudo-classes compared to normal selectors?
33How can you use :not() effectively to exclude elements from styling?
34How does :has() pseudo-class work, and why is it powerful?
35Is :has() supported in all browsers?
36How does :is() differ from :where() in terms of specificity?
37How can you combine :is() or :where() with other selectors for cleaner code?
38What’s the difference between :nth-child() and :nth-last-child()?
39How can you style only the last element of a list using pseudo-classes?
40What does the :lang() pseudo-class do?
41How do you change a button’s color when hovered but not when disabled?
42How can you highlight the current section in a menu using :target?
43How can you style a form field differently when focused and valid?
44How do you style every third list item differently?
45How can you style alternate table rows without adding extra classes?
46How do you hide empty <p> tags using pseudo-classes?
47How can you style the parent when any child inside it is focused?
48How can you highlight a link only when it’s both focused and hovered?
49How can you style the first letter of only the first paragraph using pseudo-classes?
50How would you use :has() to select a <div> that contains an image?
39 / 50

How can you style only the last element of a list using pseudo-classes?

Styling the Last Element of a List with :last-child

To style only the last element of a list, CSS provides the :last-child pseudo-class. It targets the last child element within its parent container.

Key Points About :last-child
  1. 1

    :last-child – Selects the last element among its siblings.

  2. 2

    Works with any element type, not just list items.

  3. 3

    Can be combined with other selectors for more precise targeting.

Example: Styling the Last List Item

In this example, only the last <li> (Item 4) receives the light coral background, bold text, and white color, clearly distinguishing it from the other list items.

Best Practices
  1. 1

    Use :last-child when you want to target the final element of a container without adding extra classes or IDs.

  2. 2

    Combine with other pseudo-classes like :not() or :nth-last-child() for more complex patterns.

  3. 3

    Be aware that dynamic changes to the list (adding/removing items) will affect which element is considered the last.

  4. 4

    Test across browsers, as :last-child is widely supported but interactions with other pseudo-classes may need verification.

Difficulty: 3/10
Topics: pseudo-classes, CSS selectors, list styling

Scenario Questions

0-2 years experience
  1. 1

    You're given a list of product cards and need to remove the bottom border only from the last one — how would you do that with CSS?

  2. 2

    A designer says the last list item in the navigation should have no margin-bottom, but your current CSS isn't working — what's the simplest fix?

  3. 3

    If you use :last-child on a list that has a hidden item at the end, will it still style the right element? Why or why not?

2-5 years experience
  1. 1

    Our product list renders dynamically from an API, and sometimes the last item is conditionally hidden — users are seeing broken borders on what looks like the last visible item. How do you fix this?

  2. 2

    A teammate used :nth-last-child(1) to style the last list item, but it broke after we added a footer div inside the parent container. What went wrong, and how would you fix it?

  3. 3

    We're seeing inconsistent styling on the last list item across browsers — sometimes it works, sometimes it doesn't. What edge cases should you check for?

5-8 years experience
  1. 1

    We have a component library where list items can be any element — divs, spans, anchors — and we need to style the last one consistently without knowing the tag. How do you design this without forcing markup constraints?

  2. 2

    Our list component is used in 20+ features, and we're getting inconsistent behavior because some parents have whitespace text nodes. How do you ensure :last-child works reliably across all contexts?

  3. 3

    We're migrating from inline styles to CSS modules and need to target the last list item without using global selectors. What's your approach to maintain encapsulation and avoid specificity wars?

8+ years experience
  1. 1

    We're redesigning our component architecture to support server-side rendering and hydration across multiple frameworks — how do you ensure :last-child styling remains robust when DOM structure is manipulated post-hydration?

  2. 2

    Our design system has 50+ list variants, and styling the last item is inconsistent because teams use different wrappers. How do you enforce a scalable, maintainable pattern without forcing every team to follow rigid markup rules?

  3. 3

    We're deprecating legacy list components that used JS to add 'last' classes. How do you migrate 300+ usages to pure CSS pseudo-classes without breaking existing layouts or introducing layout shifts?

Follow-up Questions

  • What if the list items aren't all the same element type?
  • How would you style the last visible item if some are dynamically hidden?
  • Why might :last-child not work as expected in a dynamically rendered list?