Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why :last-child doesn't work when we have more elements inside?

See this example http://jsfiddle.net/jwmCd/

HTML

<div class="round-buttons">
                <a href="#">hello</a>
                <a href="#">world</a>
    <form method="get" action="/search" id="search">
  <input name="q" type="text" size="40" placeholder="Search...">
</form>
</div>

CSS

.round-buttons a:first-child {background:red}

.round-buttons a:last-child {background:green}

.round-buttons a:last-child {background:green} is not applying in this condition.

Dynamically there could be more links and I want to give different styling to First and last Anchor.

like image 849
Jitendra Vyas Avatar asked Nov 27 '22 09:11

Jitendra Vyas


2 Answers

:last-child will only match the last element of the parent, irrespective of its type (in your example, :last-child will only match the <input>). Use :last-of-type to match the last element of a specific type in the parent, for example:

.round-buttons a:last-of-type {background:green}
like image 190
一二三 Avatar answered Dec 04 '22 07:12

一二三


Because a is not the last child, the last child is form

like image 22
welldan97 Avatar answered Dec 04 '22 08:12

welldan97