Tips for adjusting the placement of enlarged images within colorbox

I recently discovered colorbox and decided to use it as my lightbox solution.

However, I encountered a challenge with positioning the background (#cboxOverlay). I wanted to move it 120px to the left so that some content from the original page would still be visible beneath.

#cboxOverlay{background:#abc;    left:120px; )

This adjustment seemed to work fine.

My next issue is centering the expanded images within the new size of the background. Currently, they appear in the center of the screen instead. I've experimented with various CSS modifications but haven't found a satisfactory solution yet.

#colorbox{ ... margin:auto !important;}

Unfortunately, this approach didn't yield the desired results.

If anyone has any suggestions on how to achieve this, I would greatly appreciate it.

Thank you.

Answer №1

When centering an object using the margin: 0 auto; technique, there are three essential properties to consider:

margin: 0 auto;
display: block / table / box;
width: [anythingOtherThanAuto];

Check out this Example Fiddle

Alternatively, you can also use:

transform: translateX(50%);

Or my personal favorite method (though it may not be very useful for centering):

position: relative / absolute / fixed;
right: 15%;
left: 15%;
top: 15%;
bottom: 15%;

This last option is more suited for achieving a precise background overlay fit.

Answer №2

Here is the CSS code to be added, as it's quite lengthy and doesn't have a specific place to go: Unfortunately, I'm having trouble figuring out how to apply this to colorBox. Below is the colorbox.css file provided by them. I've highlighted where I made an adjustment to move the Overlay left by 120px. Now, the challenge is how to re-center #colorbox.

'code'
/*
    Colorbox Core Style:
    The following CSS is consistent between example themes and should not be altered.
*/
#colorbox, #cboxOverlay, #cboxWrapper{position:absolute; top:0; left:0; z-index:9999; overflow:hidden;}
#cboxWrapper {max-width:none;}
#cboxOverlay{position:fixed; width:100%; height:100%;}
#cboxMiddleLeft, #cboxBottomLeft{clear:left;}
#cboxContent{position:relative;}
#cboxLoadedContent{overflow:auto; -webkit-overflow-scrolling: touch;}
#cboxTitle{margin:0;}
#cboxLoadingOverlay, #cboxLoadingGraphic{position:absolute; top:0; left:0; width:100%; height:100%;}
#cboxPrevious, #cboxNext, #cboxClose, #cboxSlideshow{cursor:pointer;}
.cboxPhoto{float:left; margin:auto; border:0; display:block; max-width:none; -ms-interpolation-mode:bicubic;}
.cboxIframe{width:100%; height:100%; display:block; border:0;}
#colorbox, #cboxContent, #cboxLoadedContent{box-sizing:content-box; -moz-box-sizing:content-box; -webkit-box-sizing:content-box;}

/* 
    User Style:
    Change the following styles to modify the appearance of Colorbox.  They are
    ordered & tabbed in a way that represents the nesting of the generated HTML.
*/
#cboxOverlay{background:#fff;     left:120px; <!-- CHANGED --> }
#colorbox{outline:0;}
    #cboxContent{margin-top:32px; overflow:visible; background:#000;}
        .cboxIframe{background:#fff;}
        #cboxError{padding:50px; border:1px solid #ccc;}
        #cboxLoadedContent{background:#000; padding:1px;}
        #cboxLoadingGraphic{background:url(images/loading.gif) no-repeat center center;}
        #cboxLoadingOverlay{background:#000;}
        #cboxTitle{position:absolute; top:-22px; left:0; color:#000;}
        #cboxCurrent{position:absolute; top:-22px; right:205px; text-indent:-9999px;}

        /* these elements are buttons, and may need to have additional styles reset to avoid unwanted base styles */
        #cboxPrevious, #cboxNext, #cboxSlideshow, #cboxClose {border:0; padding:0; margin:0; overflow:visible; text-indent:-9999px; width:20px; height:20px; position:absolute; top:-20px; background:url(images/controls.png) no-repeat 0 0;}

        /* avoid outlines on :active (mouseclick), but preserve outlines on :focus (tabbed navigating) */
        #cboxPrevious:active, #cboxNext:active, #cboxSlideshow:active, #cboxClose:active {outline:0;}

        #cboxPrevious{background-position:0px 0px; right:44px;}
        #cboxPrevious:hover{background-position:0px -25px;}
        #cboxNext{background-position:-25px 0px; right:22px;}
        #cboxNext:hover{background-position:-25px -25px;}
        #cboxClose{background-position:-50px 0px; right:0;}
        #cboxClose:hover{background-position:-50px -25px;}
        .cboxSlideshow_on #cboxPrevious, .cboxSlideshow_off #cboxPrevious{right:66px;}
        .cboxSlideshow_on #cboxSlideshow{background-position:-75px -25px; right:44px;}
        .cboxSlideshow_on #cboxSlideshow:hover{background-position:-100px -25px;}
        .cboxSlideshow_off #cboxSlideshow{background-position:-100px 0px; right:44px;}
        .cboxSlideshow_off #cboxSlideshow:hover{background-position:-75px -25px;}


'code'

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

cycle2.js is executing before the DOM elements are fully loaded through an AJAX request

After successfully using tumblr's v1 json api to fetch images from posts, I have encountered an issue with triggering the cycle 2 plugin. It seems to be firing too soon after the images are placed into the DOM. For reference, here is a working fiddle ...

Scroll overflow can be hidden

I'm working on a project with a div containing scrollable content, but I'm struggling to get the header to overflow its parent element. It seems that using overflow: scroll is causing the element to have overflow: hidden as well. Any assistance ...

Utilize Materialize css with Aurelia for dynamic styling

Looking to incorporate a Materialize CSS select dropdown in the template file order.html. The code snippet is as follows: <div class="row"> <div class="input-field col s12"> <select multiple> <option value="" dis ...

Guide on hiding a div element when clicked outside of it?

How can I create a dropdown feature in Khan Academy where it disappears when clicked outside but stays visible when clicked inside, using JavaScript/CSS/HTML? The current implementation closes the dropdown even on internal clicks. The code includes an even ...

What is the best way to create a background image that remains hidden behind content as you scroll through a webpage?

I am looking to recreate the effect seen on the background image of this website , where the image gets covered by content as you scroll down. ...

Issue with implementing for loop in jQuery

Here is a snippet of code I am working on: $(document).on("click", "#farm_0", function(){ console.log($("#farm_0").attr("my_id")); }); $(document).on("click", "#farm_1", function(){ console.log($("#farm_1").attr("my_id")); }); Initially, the console ...

PHP failing to send emails from my website

My website has a contact form, but the form data is being appended to the URL and the JSON is not being sent to the PHP mail handler. Below you can find my code for the form. HTML <form novalidate="novalidate" style="width:60%;margin-left:auto;margin- ...

When invoking a function, jQuery may return an empty value

I am currently tweaking my jQuery script to retrieve a specific value upon page refresh in order to capture the return value. Initially, I attempted the following: var email_number = ''; // check if page refreshed or reloaded if (performance.n ...

What is the best method to invoke a function recursively with a delay of 1 second following the completion of an ajax

I am facing a situation where I need to implement a delay of 1 second after an ajax request is completed, regardless of the outcome. Instead of calling a method every second, I specifically want to call a function 1 second after the ajax request finishes. ...

Using JavaScript or JQuery, move an image from one location to another by removing it and adding

Firstly, feel free to check out the JSFiddle example My task involves moving an image with the class "Full" after a div with the class "group-of-buttons" using JavaScript. Here is the snippet of HTML code: <img src="http://i.telegraph.co.uk/multimedia ...

PHP: Link to logo in different folder by including 'nav.php'

I am facing an issue with my nav.php file: <div> <!-- there's a lot of code here so I want to write it once and include it in all pages within the main folder as well as subfolders <img src="logo.png"> </div> The structur ...

Tips for incorporating form validation into a modal in ASP.NET MVC

My task involves designing a form within a modal window with one mandatory field. The user should be able to submit the form only after filling out the required field; if left empty, an error message must display. What is the best approach to implement t ...

Get a Blob as a PNG File

Hope you had a wonderful Christmas holiday! Just to clarify, I am new to JS and may make some unconventional attempts in trying to download my Blob in PNG format. I am facing an issue with exporting all the visual content of a DIV in either PDF or image ...

Integrating PHP and Ajax for form submission can be enhanced by

Exploring the PHP and jQuery Ajax approach for creating a simple login system. I am aiming to use ajax to submit parts of a form and receive an array as a response. I am still unsure if my implementation is correct. Below is the PHP code snippet: if($ac ...

What could be the reason for my post jQuery Ajax request sending JSON data?

After downloading some code, I came across the following fragment: function FetchCommentsBySessionIDWCF_JSON() { varType = "POST"; varUrl = "service/CommentSessionIDWCFService.svc/FetchCommentsByPost"; varData = ' ...

jqgrid now features inline editing, which allows for the posting of only the data that

My jqGrid includes editable columns, and I am looking for a way to only post the data of columns where changes have been made. Here is an example of my colModel: colModel: [{ name: 'I_PK', index: 'u.I_PK ...

Scrolling horizontally with scrollbar functionality

My goal is to create a webpage with a horizontally scrollable DIV element in the center, where the scrollbar appears only at the bottom of the DIV, not at the bottom of the entire page. I am wondering if it is possible to have a scrollbar placed at the ve ...

execute a postback to refresh UpdatePanels

My webpage has a jQuery modal that pops up when a button is clicked. After performing some logic in the WebMethod called by this button, I need to refresh multiple update panels on the page to show the changes made. Although I am not very familiar with Up ...

Utilizing JQuery to Invoke a WebMethod with Parameters in C#

I have an HTML hyperlink that I want to trigger in jQuery when it is clicked. <a href="#" data-toggle="dropdown" id="notID" class="dropdown-toggle f-s-14"> <i class="fa fa-bell"></i> <span runat=" ...

Style the labels on the axis of Frappe Charts with colors (potentially utilizing the appropriate CSS selector)

Is it possible to style the x and y axis labels in a Frappe chart with different colors? https://i.stack.imgur.com/A3vUq.png While trying to identify the CSS selectors using Chrome DevTools, I found that a single text element (representing an x axis labe ...