1
<ol>
  <li>Text</li>
  <li>Text</li>
  <li><a href="#"> Hyper Text 1</a></li>
  <li><a href="#"> Hyper Text 2</a></li>
  <li>Text</li>
</ol>

I want to apply css on second anchor tag(Hyper Text 2) only. I wanna do it with css only if possible. I searched a lot but couldn't figured out how to do this.

Update: I don't know how many more li will be added in list in future. I don't wanna ended up by change nth-item(no.) again and again. That's why putting nth-child only on li won't help. But I am certin that li with anchor tag will only be 2.

Smuuf
  • 5,291
  • 3
  • 20
  • 32
Rishabh
  • 450
  • 2
  • 9
  • 24

3 Answers3

2

Please note: This answer presents a possible solution to the OP's specific problem which was clarified in a comment and does not necessarily address the problem in title of the question "How to apply css on anchor tag of nth list item?".


If you ...

  • have control over URLs where these links point to, and
  • don't expect those URL to change suddenly (unless you change the CSS, too),

... then you might want to use CSS attribute selectors.

Having this HTML specifying the menu:

<ol class="some-menu">
  <li>Text</li>
  <li>Text</li>
  <li><a href="http://url_abc#"> Hyper Text 1</a></li>
  <li><a href="http://url_def#"> Hyper Text 2</a></li>
  <li>Text</li>
</ol>

... you can select the specific element with CSS based on that element's attribute value:

ol.some-menu li a[href*="url_def"] {
  outline: 5px solid red;
}

This selector selects all <a> elements that have a href value containing url_def - that are also inside <li>s, which are inside <ul class="some-menu"> elements.

Working example: https://codepen.io/anon/pen/jxWmxR.

Attribute selectors do have a few modi operandi available and are well described here: MDN web docs.

Smuuf
  • 5,291
  • 3
  • 20
  • 32
1

You can do it with a little bit of jQuery:

$('li:has(a) a').last().css('color','red');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ol>
  <li>Text</li>
  <li>Text</li>
  <li>Text</li>
  <li>Text</li>
  <li><a href="#">Hyper Text 1</a></li>
  <li><a href="#">Hyper Text 2</a></li>
  <li>Text</li>
  <li>Text</li>
  <li>Text</li>
  <li>Text</li>
</ol>
VXp
  • 10,307
  • 6
  • 25
  • 40
  • Your solution is good but problem is I am using premium theme and I can't add Jquery without making child theme. And I don't wanna make child theme for just this little issue. – Rishabh Apr 23 '18 at 11:17
  • I'm pretty sure there are Wordpress plugins that allow you to insert JS/jQuery stuff into your templates without directly modifying the theme. – Smuuf Apr 23 '18 at 11:48
  • 1
    @vxp I can add JS either by installing a plugin or by making child theme of my current theme. Installing a plugin just for one or two lines of JS is something I will consider as my very last option. And creating child theme just of this little issue is something I won't even bother to try coz child theme shud not be made for this little thing. – Rishabh Apr 23 '18 at 12:27
0

It is not possible with css only...one thing you can do is Simply add class to that 2nd anchor tag..and here you go..

.Changecolor{
color:green;
}
<ol>
  <li>Text</li>
  <li>Text</li>
  <li>Text</li>
  <li>Text</li>
  <li>Text</li>
  <li><a href="#"> Hyper Text 1</a></li>
  <li><a href="#" class="Changecolor"> Hyper Text 2</a></li>
  <li>Text</li>
</ol>
Khushbu Vaghela
  • 735
  • 2
  • 5
  • 16
  • I can't add class coz it's wordpress site. I am gonna setup data one time only. Later client will add more data in future(Updates). And if he needs to create more OL tags then he won't be able to add class coz of lack of knowledge of html. That's why I didn't put class. – Rishabh Apr 23 '18 at 11:25