0

I want to access parent element of anchor tag

<td class="class1">
    <div id="id1">
        <a class="class2" id="id2" href="link">Link</a>
    </div>
</td>

I tried using CSS: .class2:parent:parent {}

but this does not seem to work. Is it possible to achieve the same in CSS? I do not have flexibility to use jQuery on the page.

ipradhansk
  • 332
  • 9
  • 32

1 Answers1

6

You can't do this without using Javascript. I highly recommend JQuery, it has a parent function to access parent items.

CSS traditionally does not have a parent selector because of performance. See http://snook.ca/archives/html_and_css/css-parent-selectors

I strongly recommend reading that as it describes how CSS is parsed by the browser and why it has it's limitations. Here's an excerpt:

On a seemingly regular basis, I see this discussion come up as to whether CSS should have a particular feature like the parent selector and while I haven't worked on a browser engine, I have my theories.

In short: performance.

How CSS gets evaluated With work, I've had to do quite a bit of examination of performance. We run a number of tools over an application to determine where the bottlenecks are. One such application is Google Page Speed which provides a number of recommendations to improve JavaScript and rendering performance. Before I get into its recommendations, we need to understand a little better about how browsers evaulate CSS.

Malfist
  • 29,255
  • 58
  • 174
  • 263