Danny Cooper
Danny Cooper

Reputation: 355

How do I target the first link inside a div with CSS?

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.

Upvotes: 7

Views: 15014

Answers (3)

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35973

Try this code:

div > a:first-child{
   //your css code
}

Upvotes: 10

sticksu
sticksu

Reputation: 3738

You can use the first-child selector:

div > a:first-child { /* your css */ }

Upvotes: 18

Nick Ginanto
Nick Ginanto

Reputation: 32130

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 */
} 

Upvotes: 3

Related Questions