Overview

The Colorbox module integrates the Colorbox plugin into Drupal. The Colorbox plugin is a light-weight, customizable lightbox plugin for jQuery 1.4.3+.

With Colorbox images, iframed or inline content etc. can be displayed in a overlay above the current page.

Features

The Colorbox module:

  1. Excellent integration with Image field and Image styles
  2. Choose between a default style and 5 example styles that are included.
  3. Style the Colorbox with a custom colorbox.css file in your theme.
  4. Drush command to download and install the Colorbox plugin in sites/all/libraries

The Colorbox plugin:

  1. Compatible with: jQuery 1.3.2+ in Firefox, Safari, Chrome, Opera, Internet Explorer 7+
  2. Supports photos, grouping, slideshow, ajax, inline, and iframed content.
  3. Lightweight: 10KB of JavaScript (less than 5KBs gzipped).
  4. Appearance is controlled through CSS so it can be restyled.
  5. Can be extended with callbacks & event-hooks without altering the source files.
  6. Completely unobtrusive, options are set in the JS and require no changes to existing HTML.
  7. Preloads upcoming images in a photo group.
  8. Well vetted and currently in use in over 600,000 websites.
  9. Released under the MIT License.

Installation

  1. Download and unpack the Libraries module directory in your modules folder (this will usually be "sites/all/modules/").
  2. Download and unpack the Colorbox module directory in your modules folder (this will usually be "sites/all/modules/").
    • For Drupal 8.x : Download the Colorbox plugin and unpack in /libraries (at the root of your site). Make sure the path to the plugin file becomes: "/libraries/colorbox/jquery.colorbox-min.js".
    • For Drupal 7.x : Download the Colorbox plugin and unpack in "sites/all/libraries". Make sure the path to the plugin file becomes: "sites/all/libraries/colorbox/jquery.colorbox-min.js".
      Drush provides a command installing the plugin:
      % drush colorbox-plugin.
      The command will download the plugin and unpack it in "sites/all/libraries". It is possible to add another path as an option to the command, but not recommended unless you know what you are doing.
  3. Go to "Administer" » "Modules" and enable the Colorbox module.

You may use the jQuery update module if you need a later version of jQuery than Drupal ships with but as long as you use the 1.x branch of the Colorbox plugin it's not a requirement.

You need to complete the above steps in order. If you enabled the colorbox module before you installed the plugin library, you may find that colorbox does not work until you reinstall the colorbox module.

Configuration

Go to "Configuration" > "Media" > "Colorbox" to find all the configuration options.

Add a custom Colorbox style to your theme

The easiest way is to start with either the default style or one of the example styles included in the Colorbox JS library download. Simply copy the entire style folder to your theme and rename it to something logical like "mycolorbox". Inside that folder are both a .css and .js file, rename both of those as well to match your folder name: i.e. "colorbox_mycolorbox.css" and "colorbox_mycolorbox.js"

Add entries in your theme's .info file for the Colorbox CSS/JS files:

stylesheets[all][] = mycolorbox/colorbox_mycolorbox.css
scripts[] = mycolorbox/colorbox_mycolorbox.js

Go to "Configuration" » "Media" » "Colorbox" and select "None" under "Styles and Options". This will leave the styling of Colorbox up to your theme. Make any CSS adjustments to your "colorbox_mycolorbox.css" file.

Load images from custom links in a Colorbox

Add the class "colorbox" to the link and point the src to the image you want to display in the Colorbox (requires Colorbox Inline module in Drupal 8).

Load content in a Colorbox

Check the "Enable Colorbox load" option in Colorbox settings.

This enables custom links that can open content in a Colorbox. Add the class "colorbox-load" to the link and build the url like this "[path]?width=500&height=500&iframe=true" or "[path]?width=500&height=500" if you don't want an iframe.

If you want to pass the width and height of the Colorbox as a percentage (useful for responsive designs), you can format the link like this "[path]?width=90%25&height=90%25&iframe=true" or "[path]?width=90%25&height=90%25", where the "%25" is the HTML entity for "%".

Other modules may activate this for easy Colorbox integration.

Position Colorbox

Traditionally, the Colorbox will automatically position itself close to the center of the visitors browser window. But what if you want to position this to the top, bottom, right, or left? There are some options for this within the Colorbox library but the module does not take this into account as the feature is newer.

We can, however, still pass these options along in the links we use to trigger the overlay. For example:

<a class="colorbox-load" href="http://www.example.com?width=300&height=150&top=40&right=20&iframe=true">link</a>.

Note the addition of "top=40" and "right=20". This tells Colorbox that you want the box to appear 40 pixels down from the top and 20 pixels in from the right.

The options are: top, bottom, left, and right.

Load inline content in a Colorbox

Check the "Enable Colorbox inline" option in Colorbox settings.

This enables custom links that can open inline content in a Colorbox. Inline in this context means some part/tag of the current page, e.g. a div. Replace "id-of-content" with the id of the tag you want to open.

Add the class "colorbox-inline" to the link and build the url like this "?width=500&height=500&inline=true#id-of-content".

It could e.g. look like this.

<a class="colorbox-inline" href="?width=500&height=500&inline=true#id-of-content">Link to click</a>

<div style="display: none;">
<div id="id-of-content">What ever content you want to display in a Colorbox.</div>
</div>

Alternatively instead of style="display: none;" you could use class="element-hidden".

Other modules may activate this for easy Colorbox integration.

Image in Colorbox not displayed in Internet Explorer 8

If your theme has CSS like this (popular in responsive design):

img {
  max-width: 100%;
}

Internet Explorer 8 will have problems with showing images in the Colorbox. The fix is to add this to the theme CSS:

#cboxLoadedContent img {
  max-width: none;
}

Comments

loloyd’s picture

The "Load images from custom links in a Colorbox" needs more examples such as grouping together quick links on the fly for a slideshow.

nightonfire’s picture

Use the 'rel' attribute (<a rel="xyz" ..>) on a colorbox link to create an image group or gallery. Putting the same value on the rel attribute on multiple colorbox links puts arrows on the bottom of the colorbox to switch between them directly.

rsych’s picture

rel attribute does not work for grouping in Drupal 9. Please use the data-colorbox-gallery instead. And the title attribute for Popup label.
Example:

<a href="image.png" title="Image 1" data-colorbox-gallery="gallery-1" class="colorbox cboxElement" data-cbox-img-attrs="{'alt' : 'Image 1'}">
  <img src="image_thumbnail.png" alt="Image 1">
</a>
outlaw82’s picture

I got some troubles in loading content, from a custom image link in colorbox windows
The solution as you can see this article (with examples) http://www.alessioligabue.it/colorbox-image-load is place the colorbox class in the div

<div>
<p><a class="colorbox" href="/imageaddress/fullquality.jpg" title="div title">
        <img alt="alt image" src="/imageaddress/minified.jpg"
         title="image title" /></a></p>
</div>
s_chandela’s picture

<a class="colorbox-node" href="<?php echo $base_url?>/node/add/boarding?width=800&height=500&iframe=true" role="button">Add A Task</a>

Popup is displaying properly.
But the issue is after pressing SAVE button with out any providing details ( purpose is checking validation ),
System will display huge unformatted json data.
This happens only when adding Content Type data, it goes okay when adding webform.
Is there any task that is being remaining ?
URL will look like after pressing button.
http://www.website.com/colorbox/node/add/boarding?width=800&height=500&destination=node/17

Please help me to sort it out.

Thanks

david@thrale.com’s picture

Took me a while to work out, but with the Markdown Filter module, the syntax to use is ...

[![Alt text](../sites/default/files/pictures/test.jpg "Title")](../sites/default/files/pictures/test.jpg "Title"){.colorbox}

No need for all those WYSIWG / Ckeditor / IMCE complications with Markdown!

benjus’s picture

Hi there,

I can see the template inside the popup the second time I click. First time opens a new page.

Do you have any idea about how to solve that?

This is the link:
print "<a class='colorbox casualty-link colorbox-inline' href='/timeline/casualties-popup/".$conflict."/".$fullDate."'>$casualties[$currentDayPadded]</a>";

This is the template:

<div class="popup-content">
  <h2><?php print $date; ?></h2>
  <div class="cemeteries">

    <?php foreach($cemeteries as $cemetery): ?>
      <div class="cemetery">

        <?php if(isset($cemetery->display_img)): ?>
          <img src="<?php print $cemetery->display_img ?>">
        <?php endif; ?>

        <h3><?php print $cemetery->cemetery_name; ?></h3>
        <span class="casualty-count">(<?php print $cemetery->casualty_count; ?>
          <?php if($cemetery->casualty_count > 1) print 'casualties'; else print 'casualty'; ?>)
        </span>

        <?php if(!is_null($cemetery->cemetery_description)): ?>
          <div class="cemetery-description"><p><?php print $cemetery->cemetery_description ?></p></div>
        <?php endif; ?>

        <span class="view-casualties">View Casualties</span>

        <div class="cemetery-casualties">
          <?php foreach($casualties[$cemetery->cemetery_nid] as $casualty): ?>
            <div class="casualty">
              <ul> 
              <div class="casualty-name">
                <a href="/<?php print drupal_get_path_alias('node/'.$casualty->casualty_nid); ?>" target="_blank"><?php print $casualty->casualty_name; ?></a>
              </div>
              <div class="casualty-rank">
                (<?php print $casualty->rank; ?>)
              </div>
              <div class="casualty-service-number">
                <span class="label">Service Number:</span>
                <?php print $casualty->service_number; ?>
              </div>
              </ul>
            </div>
          <?php endforeach; ?>
        </div>

      </div>
    <?php endforeach ?>

  </div>
</div>

And this is the js working inside the colorbox popup:

jQuery("document").ready(function($){
    $('a.casualty-link').colorbox({
        rel:'nofollow', 
        width: "40%",
        height: "60%",
        href:function(){
            var path = $(this).attr('href');
            path = path.concat(" div.popup-content");
            return (path);
        },
        onComplete:function(){
            $(".view-casualties").click(function() {

                if($(this).hasClass('active')){
                    $(this).removeClass('active');
                    $(this).text('View Casualties');
                    $(this).next(".cemetery-casualties").hide().slideUp( "fast", function() {});
                }
                else{
                    $(this).addClass('active');
                    $(".view-casualties").text('View Casualties');
                    $(".cemetery-casualties").hide();
                    $(this).text('Hide Casualties');
                    $(this).next(".cemetery-casualties").show().slideDown( "fast", function() {});
                }
            });
        }});

    
    

});
benjus’s picture

Hi there,

I solve the problem doing this in the js:

jQuery("document").ready(function($){
    

    $('body').delegate('a.casualty-link','click',function(){
        $(this).colorbox({
        rel:'nofollow', 
        width: "40%",
        height: "60%",
        href:function(){
            var path = $(this).attr('href');
            path = path.concat(" div.popup-content");
            return (path);
        },
        onComplete:function(){
            $(".view-casualties").click(function() {

                if($(this).hasClass('active')){
                    $(this).removeClass('active');
                    $(this).text('View Casualties');
                    $(this).next(".cemetery-casualties").hide().slideUp( "fast", function() {});
                }
                else{
                    $(this).addClass('active');
                    $(".view-casualties").text('View Casualties');
                    $(".cemetery-casualties").hide();
                    $(this).text('Hide Casualties');
                    $(this).next(".cemetery-casualties").show().slideDown( "fast", function() {});
                }
            });
        }});
    });
corE’s picture

There seems to be some discussion and interest about grouping links and showing multiple images in one colorbox slideshow. In one post it is was suggested to use the 'rel' attribute to group links, and I found this didn't work in my installation (D8). The link configuration that did work for me was...

<p><a class="colorbox" data-colorbox-gallery="AAA" data-entity-type="file" href="/sites/YourSide.com/files/yourImage1.JPG">link1</a></p>

<p style="display:none;"><a class="colorbox" data-colorbox-gallery="AAA" data-entity-type="file" href="/sites/YourSide.com/files/yourImage2.JPG">link2</a></p>

Noticed that in the second link I set it's style to display:none, in case you just want to have one link to open the slideshow.

I also had to turn off Mobile detection on colorbox settings in order to get it to work on the cell phone.

And the Enable - Colorbox load and inline in the settings? Where is that for D8?

... I will spare you all my rant about my how long I spent dinking around trying to get a slideshow to work, when I just happened upon this solution... and how everything seems to be half baked and filled with inaccuracies these days. It seems that something referencing this should have been put into the documentation. However... there is nothing like the relief of finally getting something to work after hours of maddening frustration and wild goose chases - it's kind of like that feeling of relief you get after you vomit!

Hope this helps someone.

Capricorn1’s picture

I know I'm a couple of years late to the party, but I'm just now converting a Drupal 7 site to Drupal 8.

The data-colorbox-gallery="AAA" attribute you found is a sanity-saver. The data-entity-type="file" attribute might be the default (and therefore, optional here).

If you happen to use a thumbnail image to open the gallery, that's the typical:

<a class="colorbox" data-colorbox-gallery="gallery1" href="/images/gallery001_image001.jpg" title="Original item before nuclear exposure. (Click the image to move to the next image.)"><img alt="Original item before nuclear exposure - 1 0f 5 thumb" src="/images/gallery001_image001_t.jpg" /></a>

<a class="colorbox" data-colorbox-gallery="gallery1" href="/images/gallery001_image002.jpg" title="Item after 0.01 seconds nuclear exposure. (Click the image to move to the next image.)"><img alt="Item after 0.01 seconds nuclear exposure - 2 0f 5 thumb" src="/images/gallery001_image002_t.jpg" /></a>

and so on. Note I want to see a string of thumbnails, so I don't use the style="display:none;" paragraph attribute. I have these in CSS that strings a group of thumbnails the same line, and clicking any one of those takes me directly to the full image.

hihu’s picture

not work on drupal 9 :(

Not Found
The requested URL /modules/contrib/colorbox/images/admin/example_stockholm_syndrome.png was not found on this server.

my js locate at: /libraries/colorbox/jquery.colorbox-min.js