7

For example I have this:

<div>
  <a href="#">sample 1</a>
  <a href="#">sample 2</a>
  <a href="#">sample 3</a>
</div>

I want to target the first link with CSS.

Teun Zengerink
  • 4,277
  • 5
  • 30
  • 32
Danny Cooper
  • 355
  • 1
  • 8
  • 25

3 Answers3

18

You can use the first-child selector:

div > a:first-child { /* your css */ }
sticksu
  • 3,660
  • 3
  • 23
  • 41
10

Try this code:

div > a:first-child{
   //your css code
}
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
3
div a:nth-of-type(n)
{
   /* css */
} 

where n is the number of line you want.. in your case

div a:nth-of-type(1)
{
   /* css */
} 
Nick Ginanto
  • 31,090
  • 47
  • 134
  • 244