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.

  1. Class Selecotors: $(".myclass')
  2. Tag Selecotors: $("div")
  3. Atrribute: $("input[type="text"]')

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!