Here you can find the guide on how to use Magnific Popup. Besides this docs page, you can play with examples on CodePen. If you’ve found any mistake in this site or you know how to improve some part of this documentation - please commit on GitHub.
Please ask general questions through Stack Overflow tagged with magnific-popup.
If you’re looking for touch-friendly popup just for images, PhotoSwipe might be a better choice.
You can get Magnific Popup JS and CSS file from the dist/ folder in the GitHub repository, or by compiling it yourself with Grunt.
It’s not required, but we recommend placing CSS files in <head> and JavaScript files and initialization code in the footer of your site (before the closing </body> tag). If you already have jquery.js on your site, don’t include it a second time, or use jQuery.noConflict(); mode. Optionally, you can include Zepto.js instead of jQuery, or choose which one to include based on browser support.
Initializing popup
Popup initialization code should be executed after document ready, for example:
There are three ways to initialize a popup:
1. From an HTML element
2. From a group of elements with one parent
Same as first one, but use this method if you are creating a popup from a list of elements in one container. Note that this method does not enable gallery mode, it just reduces the number of click event handlers; each item will be opened as a single popup. If you wish to enable gallery, add the gallery:{enabled:true} option.
3. From the ‘items’ option
The items option defines data for the popup item(s) and makes Magnific Popup ignore all attributes on the target DOM element. The value for items can be a single object or an array of objects.
By default, Magnific Popup has four types of content: image, iframe, inline, and ajax. There is no any “auto-detection” of type based on URL, so you should define it manually.
The type of a popup can be defined in a two ways:
Using the type option. E.g.: $('.image-link').magnificPopup({type:'image'}).
Using the mfp-TYPE CSS class (where TYPE is the desired content type). For example: <a class="mfp-image image-link">Open image</a>, $('.image-link').magnificPopup().
The second option always overrides the first, so you may initialize popups with multiple content types from one call.
inline is the default content type (from v0.8.4), so you may skip its definition.
The source of the the popup content (for example, a path to an image, a path to an HTML file, a path to a video page) can be defined in a few ways:
Method #1: From the href attribute:
Method #2: From the data-mfp-src attribute (overrides the first method):
Method #3: From the items option
If you want to modify how the source is parsed, you may hook into the elementParse callback. For example:
Image Type
The path to the image must be set as the main source if you selected this type. If your popup doesn’t have an image source and doesn’t have an image that shouldn’t be preloaded (and retina-ized, etc.), use the inline type.
Please note that Magnific Popup doesn’t implement any JavaScript-based client-side caching for images. So make sure that your server adds expires headers so the image won’t be downloaded each time.
Iframe Type
By default Magnific Popup supports only one type of URL for each service:
1) Create a HTML element that you wish to display in popup and add it somewhere. Class mfp-hide is required to hide the popup from the page.
2) Style this element. Magnific Popup by default doesn’t apply any styles to it, except vertical centering (if alignTop:false). Close button will be automatically appended inside (if closeBtnInside:true).
3) Add button that will open the popup (source must match CSS id of an element (#test-popup in our case).
4) Initialize script.
Here are some other ways to initialize popup:
I have created two examples on CodePen that will help you better understand how it works:
To create such type of popup, first of define the path to the file that you wish to display and select ajax type of the popup. Popup itself should be styled in exactly the same way as an inline popup type.
Important note! The contents of the file that you load is already a popup itself, so there must be only one root element.
Note that path to the file that will be loaded should have the same origin (e.g. be on the same domain), learn more.
Ajax options:
To modify content after it’s loaded, or to select and show just specific element from loaded file, there is a parseAjax callback:
Options
Options should be passed to the initialization code and separated by comma, e.g.:
Options for specific modules are explained in their sections of documentation (e.g. related to text are in translating section, related to gallery are in gallery section. Here you can find the list of general options:
disableOn
null
If window width is less than the number in this option lightbox will not be opened and the default behavior of the element will be triggered. Set to 0 to disable behavior. Option works only when you initialize Magnific Popup from DOM element.
Can also accept Function as a parameter, which should return true if lightbox can be opened and false otherwise. For example:
key
null
“Key” option is a unique identifier of a single or a group of popups with the same structure. If you will not define it, DOM elements will be created and destroyed each time when you open and close popup.
You may (and should) set an equal key to different popups if their markup matches. By markup I mean options that change HTML structure of the popup (e.g. close icon placement and HTML code of it).
For example: you have many popups that show title, some text and button - you may use one key for all of them, so only one instance of this element is created. Same for popup that always contains image and caption.
You can delete cached templates like so:
// delete template with key "your-key"
delete $.magnificPopup.instance.popupsCache['your-key'];
// delete all templates
$.magnificPopup.instance.popupsCache = {};
midClick
false
If set to true lightbox is opened if the user clicked on the middle mouse button, or click with Command/Ctrl key. Option works only when you initialize Magnific Popup from DOM element.
mainClass
empty string
String that contains classes that will be added to the root element of popup wrapper and to dark overlay. For example "myClass", can also contain multiple classes - 'myClassOne myClassTwo'.
preloader
true
Preloader in Magnific Popup is used as an indicator of current status. If option enabled, it’s always present in DOM only text inside of it changes. Below you can see explanation of CSS names that are applied to container that holds preloader and content area depending on the state of current item:
For example, if you want your error message to be in red add such CSS:
You can trigger change of status manually by calling instance.updateStatus('error', 'error message').
focus
empty string
String with CSS selector of an element inside popup that should be focused. Ideally it should be the first element of popup that can be focused. For example 'input' or '#login-input'. Leave empty to focus the popup itself.
closeOnContentClick
false
Close popup when user clicks on content of it. It’s recommended to enable this option when you have only image in popup.
closeOnBgClick
true
Close the popup when user clicks on the dark overlay.
closeBtnInside
true
If enabled, Magnific Popup will put close button inside content of popup, and wrapper will get class mfp-close-btn-in (which in default CSS file makes color of it change). If markup of popup item is defined element with class mfp-close it will be replaced with this button, otherwise close button will be appended directly.
showCloseBtn
true
Controls whether the close button will be displayed or not.
enableEscapeKey
true
Controls whether pressing the escape key will dismiss the active popup or
not.
modal
false
When set to true, the popup will have a modal-like behavior: it won’t be
possible to dismiss it by usual means (close button, escape key, or
clicking in the overlay).
This is a shortcut to set closeOnContentClick, closeOnBgClick,
showCloseBtn, and enableEscapeKey to false.
alignTop
false
If set to true popup is aligned to top instead of to center. (basically all this option does is adds mfp-align-top CSS class to popup which removes styles that align popup to center).
index
null
Used for gallery. Defines starting index. If popup is initialised from DOM element, this option will be ignored.
fixedContentPos
auto
Popup content position. Can be "auto", true or false. If set to true - fixed position will be used, to false - absolute position based on current scroll. If set to "auto" popup will automatically disable this option when browser doesn’t support fixed position properly.
fixedBgPos
auto
Same as an option above, but it defines position property of the dark transluscent overlay. If set to false - huge tall overlay will be generated that equals height of window to emulate fixed position. It’s recommended to set this option to true if you animate this dark overlay and content is most likely will not be zoomed, as size of it will be much smaller.
overflowY
auto
Defines scrollbar of the popup, works as overflow-y CSS property - any CSS acceptable value is allowed (e.g. auto, scroll, hidden). Option is applied only when fixed position is enabled.
There is no option overflowX, but you may easily emulate it just via CSS.
removalDelay
0
Delay before popup is removed from DOM. Used for the animation.
Markup of close button. %title% will be replaced with option tClose.
prependTo
document.body
The DOM element to which popup will be added. Useful when you’re using ASP.NET where popup should be inside form. Option available since 2013/12/04.
autoFocusLast
true
If set to true last focused element before popup showup will be focused after popup close. Option available since 2015/12/16.
allowHTMLInTemplate
false
Available since 1.2.0. Allows HTML content in the source template (such as text under image). If you enable this - make sure your HTML attributes are sanitized if they can be created by a non-admin user.
allowHTMLInStatusIndicator
false
Available since 1.2.0. Same as above, but allows HTML content in the status indicator (such as “Loading…” or error message text).
Gallery
The gallery module allows you to switch the content of the popup and adds navigation arrows. It can switch and mix any types of content, not just images. Gallery options:
Example:
Multiple galleries
To have multiple galleries on a page, you need to create a new instance of Magnific Popup for each separate gallery. For example
You don’t necessarily need to use delegate option, it can be just $(this).find('a').magnificPopup( ....
Lazy-loading
Lazy-loading option preloads nearby items. It accepts an array with two integers. The first is the number of items to preload before the current. The second is the number of images to preload after the current. For example, preload: [1,3] will load 3 next items and 1 that is before current. These values are automatically switched based on direction of movement.
By default all what it does is just searches for an image tag and preloads it with JavaScript. But you can extend it and do your custom preloading logic with help of lazyLoad event, like so:
“Preload” option can be changed dynamically. To disable it set preload:0.
Animation
Animation can be added to any example. For Ajax based popup content animation is fired only after content is loaded.
After popup is opened popup wrapper and background overlay get class mfp-ready. Before popup is removed they get class mfp-removing.
For example:
Then just play with CSS3 transitions:
Please use animation wisely and when it’s really required. Do not enable it when your popup may contain large image or a lot of HTML text.
Retina
“Retina” module allows you to display high-resolution images on high-dpi screens with different devicePixelRatio. Module works only with “image” type and only when window.devicePixelRatio > 1.
First of prepare two sets of images. Default supported syntax requires @2x at the end of the image file name, e.g.: image.jpg > image@2x.jpg. Then initialize popup as usual and add ratio in retina set of options.
Zooming only works for images, for now. To add zooming effect, first of make sure that you included “zoom” module to Magnific Popup build (since v0.9.1.). Then follow steps:
1) Add the thumbnail with link that will open the popup, for example:
You need to make sure that ratio of your thumbnail matches the ratio of the big image, to avoid “jumps” at the end of zoom-out animation.
2) Initialize popup with zoom options:
3) Optionally, add CSS fading animation to background overlay
Zoom module adds zoomAnimationEnded callback, which fires when zoom-in animation is finished.
API
There is a much bigger list of public events, methods and variables than provided here which is used for module development, look inside code or type in console $.magnificPopup.instance. to find them, if you think that something should be added to docs - please submit commit.
Events
You can define callbacks in callbacks option. Besides that, all Magnific Popup events are also dispatched using triggerHandler on target element (or to document if the element doesn’t exist).
List of callbacks. In each callback this is $.magnificPopup.instance, so you can execute methods (this.close()) or access public variables (this.currItem).
Public methods
You may also call ANY method via $.fn.magnificPopup('methodName' /*, param1, param2 ... */) after you initialized the popup, for example:
You may also open the popup directly at initialization:
Public properties
Most properties are available only after the popup is opened. Only most used are listed here.
Translating
Internationalization of Magnific Popup is very simple, all you need is to extend default settings object with new values, or just pass options to your initialization code. If you’re making some public plugin or theme, it’s strongly recommended to use only second method to avoid conflicts.
Some properties contain %keys% that should not be translated, but may be reordered or removed.
Same thing, but applied only to specific slider:
FAQ
How to place gallery navigation arrows “inside” the image?
How to override some function without modifying the source files?
Rewrite the function that you wish to modify by editing Magnific Popup object, you can access it like so $.magnificPopup.instance. For example to override function that goes to “next” item in gallery:
You may override any public function, just note that this change applies globally.
How to add spinner indicator instead of “Loading…” text?
Just style element with class .mfp-preloader. Example on CodePen. Another example (if you want to show image only after its fully loaded).
Known issues
When popup is opened scrollbar of window disappears and creates empty space or shifts some fixed-positioned menu (or whatever)
Solution 1: add overflowY:’scroll’ option to force the scrollbar. Solution 2: use open/close popup callbacks to apply custom styling to menu that behaves incorrectly.
Text input in Select2 plugin is inactive when added inside popup
Improve this documentation page (simply submit commit via GitHub). Any improvements, including your own CodePen examples are very welcome. And, lastly, don’t forget to star the script on GitHub: