Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Target the 2nd instance of a CSS Class

Due to the solution I am using, I only have limited CSS classes. I would like to know if there is a way I can target the 2nd class, when there are 2 classes of the same name.

I have 2 instances of media-link contained withing a DIV called item. I need to basically add additional styles to the 2nd instance of the class. However this is where the difficulty comes in... I can not add any additional code, or classes to the code! I know, not ideal but its down to the solution being used, not personal choice.

Example code:

<a href="#" class="media-link" style="display: block">   <img src="images/ph-a.png" alt="" /> </a> <a href="#" class="media-link">   <img src="images/auth-2-100px.png" alt="" />   <p class="author">Author: John Smith</p> </a> 

Thanks in advance for any suggestions, or solutions.

like image 508
thatuxguy Avatar asked Nov 02 '12 10:11

thatuxguy


People also ask

How do I target a second sibling in CSS?

You use the general sibling selector (~) in combination with :hover . The ~ combinator separates two selectors and matches the second element only if it is preceded by the first, and both share a common parent.

How do you target a CSS class?

To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class. To do this, start with the element name, then write the period (.)

How do I target my next child in CSS?

Definition and Usage. The :nth-child(n) selector matches every element that is the nth child of its parent. n can be a number, a keyword (odd or even), or a formula (like an + b). Tip: Look at the :nth-of-type() selector to select the element that is the nth child, of the same type (tag name), of its parent.


1 Answers

There are two pseudo-selectors that accomplish what you're looking for.

.media-link:nth-child(2) {   // here style  } 

or

.media-link:nth-of-type(2){   // here style } 
like image 130
Rohit Azad Malik Avatar answered Sep 20 '22 20:09

Rohit Azad Malik