jQuery – Random Heights and Widths

This is one way to create elements with random heights and widths. The same technique could also be used for any numerical css values.

First it inserts 6 divs into the body with ids of t#, where # is numbers 1-6.

I decided that I wanted elements with widths between 100px and 200px, and heights between 100px and 300px. So first it generates a random number between 0 and 200 for width, and 0 and 200 for height, and then adds 100 to each.

Then for each t# id, it adds the random height and width to the css style.

 
View demo

The following two tabs change content below.

Jeremy Varnham

Latest posts by Jeremy Varnham (see all)

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">