March 19, 2012
Adding margin with Jquery
Question by Josh Davies
I am trying to center tags within a div but depending on what page you are on it can depend on how many tags there will be as it’s for an image gallery. So i am trying to do it with Jquery.
Basically what i want to say is:
For every tag add 5% to the margin.
Can someone point me in the right direction to do so. I was thinking of using the .size() to calculate the amount of tags but not sure where to go then.
Thanks.
Answer by Starx
Use the *
selector
$("*").css("margin","5%");
However, for the safety purposes, you have to limit the use of such wildcard selector. There are lots of other ways you can select mass elements.
- Class Selecotors:
$(".myclass')
- Tag Selecotors:
$("div")
- Atrribute:
$("input[type="text"]')