How to add padding-top to an anchor link?

advertisements

I would like add a padding-top to my div when I click on the link to the anchor :

<a href="#myAnchor">Go to my anchor</a>

...

<div id="myAnchor"> ... </div>

The issues is that I want add the padding just when my link redirect me to the anchor. I don't want add padding-top in the html, I just don't want that my div is on the top of my page, I need a padding or margin top.

Thank you.


There's only one way I know of: javascript. If you're already using jquery:

 $('a[href="#myAncre"]').click(function(){
   $("#myAncre").css("padding-top", "20px");
   });

Although if you don't use jquery already, it might be worth to do it with simple javascript.