Skip to main content

Fancybox

Fancybox saves you time and helps to easily create beautiful, modern overlay windows containing images, iframes, videos or any kind of HTML content.

This is the 4th generation of Fancybox and brings lots of fresh features. Both Carousel and Panzoom components are used under the hood and help ensure a best-in-class user experience. In addition, they are easy to integrate with each other.

Key features#

  • No external dependencies
  • Touch and mobile optimized with swipe, drag and pinch-to-zoom gestures
  • Highly customizable with configuration options, Sass and CSS variables
  • Multiple zoom levels
  • Properly manages focus
  • Closes with Back Button
  • Accessible
  • Smooth, natural animations
  • Multiple instances
  • Widely used, battle-tested
  • Best user and developer experience

Quick demo#

Click on any image thumbnail to open image gallery with default look and feel.

This is just a tip of the iceberg, Fancybox can do so much more, check out Showcase for real-world examples for your inspiration.

Usage#

There are two main styles of coding when using Fancybox. Each of these styles has advantages and disadvantages, and you can combine the two.

Declarative#

Add a data-fancybox attribute to any element to enable Fancybox.

Fancybox automaticaly attaches click handler to [data-fancybox] selector. That means, clicking any DOM element having data-fancybox attribute will launch Fancybox with default options by default.

If you wish to customize options, simply overwrite default click handler:

Fancybox.bind("[data-fancybox]", {  // Your options go here});

You can choose any CSS selector here to attach click handler to the currently selected set of elements as well as all future matching elements.

note
  • Galleries are created by adding the same attribute data-fancybox value to multiple elements.
  • Use data-src or href attribute to specify the source of the content.
  • Optionally, add a data-caption attribute to show a caption under the content.

Programmatic#

Use API to launch Fancybox programmatically from anywhere in the code.

Media types#

Fancybox itself only displays HTML content. Additional media type support is provided by plugins that are included by default:

  • Image for images;
  • HTML for inline content, iframes, videos (HTML5, Youtube and Vimeo), Google maps, and Ajax.

A frequent question is - "Does it support PDF?". First, you have to understand that PDF is a complex file format. Parsing and rendering PDFs is outside the scope of this project.

However, there are two options:

  • Simply link to a PDF file (optionally, use data-type="pdf" attribute):

    <a data-fancybox data-type="pdf" href="YOUR_PDF_FILE.pdf">Click me</a>

    Fancybox will then create iframe element and will use file URL as src attribute. It will work very well on computers with modern browsers.

  • If you're concerned about users using outdated browsers or using mobile devices, you can use PDF.js. Download and extract files on the root directory of your project. Then create links like this:

    <a  data-fancybox  data-type="pdf"  href="/pdfjs/web/viewer.html?file=YOUR_PDF_FILE.pdf"  >Click me</a>