Sizing and Positioning Fancybox

You've probably seen Fancybox all over the place. In a way, it's the living-successor of ThickBox and Lightbox.

I was recently on a project where I had to both size and place it. Sizing it is easy. Say you have a link tag with an id of opener, like this:

<a id="opener" href="http://www.google.com">Google</a>

To create and size the fancybox that will open when it's clicked, you'd just add the following javascript to your page:

<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
  $("#opener").fancybox({
    'width': 390,
    'height': 400
  });
});
</script>

Note that pixels are assumed, you should not say something like "390 pixels" like you would in css, it won't work then.

So, above we have defined a fancybox that is 390 pixels wide, and 400 pixels high. Next we need to position it.

(Note this is based on Fancybox 1.3 - I can't say if it'll work with other versions.)

Fancybox puts everything in a layer with an id of "fancybox-wrap" - so we can easily manipulate it with basic css, like so:

#fancybox-wrap {
  margin: -70px 0 0 290px;
}

You can do the same with padding, maybe even positioning.

Hope that helps someone!

This entry was posted in jquery. Bookmark the permalink.

11 Responses to "Sizing and Positioning Fancybox"

Leave a reply