Search:

move the Shadowbox.js close button to the upper-right

Shadowbox.js is the coolest JavaScript lightbox I’ve come across. The only thing I wanted to change was to put the “Close” button in the upper-right corner, where most users are used to looking for it. After crusing their forums, I discovered I wasn’t the only one that wanted this feature. So, I dove into the code and made the necessary adjustments, and of course, afterwards, I decided to document it here so that others could use it too. Here’s how to do it…

 

First, find this part in the Shadowbox.js code that writes the HTML into the page…


<div id="sb-container"><div id="sb-overlay"></div><div id="sb-wrapper"><div id="sb-title"><div id="sb-title-inner"></div></div><div id="sb-wrapper-inner"><div id="sb-body"><div id="sb-body-inner"></div><div id="sb-loading"><div id="sb-loading-inner"><span>{loading}</span></div></div></div></div><div id="sb-info"><div id="sb-info-inner"><div id="sb-counter"></div><div id="sb-nav"><a id="sb-nav-close" title="{close}" onclick="Shadowbox.close()"></a><a id="sb-nav-next" title="{next}" onclick="Shadowbox.next()"></a><a id="sb-nav-play" title="{play}" onclick="Shadowbox.play()"></a><a id="sb-nav-pause" title="{pause}" onclick="Shadowbox.pause()"></a><a id="sb-nav-previous" title="{previous}" onclick="Shadowbox.previous()"></a></div></div></div></div></div>

 

and edit it – moving the HTML for the “X” (close) button…

<a id="sb-nav-close" title="{close}" onclick="Shadowbox.close()"></a>

…into the upper div #sb-title, just after #sb-title-inner, like so…


<div id="sb-container"><div id="sb-overlay"></div><div id="sb-wrapper"><div id="sb-title"><div id="sb-title-inner"></div><a id="sb-nav-close" title="{close}" onclick="Shadowbox.close()"></a></div><div id="sb-wrapper-inner"><div id="sb-body"><div id="sb-body-inner"></div><div id="sb-loading"><div id="sb-loading-inner"><span>{loading}</span></div></div></div></div><div id="sb-info"><div id="sb-info-inner"><div id="sb-counter"></div><div id="sb-nav"><a id="sb-nav-next" title="{next}" onclick="Shadowbox.next()"></a><a id="sb-nav-play" title="{play}" onclick="Shadowbox.play()"></a><a id="sb-nav-pause" title="{pause}" onclick="Shadowbox.pause()"></a><a id="sb-nav-previous" title="{previous}" onclick="Shadowbox.previous()"></a></div></div></div></div></div>

 

then, add this stuff to your Shadowbox.css file AFTER all the other styles…


#sb-title-inner {
    width: 90%;
}
#sb-nav-close {
    float: right;
    width: 16px;
    height: 16px;
    display: block;
    margin: -20px 0px 0px 0px;
    background: url(close.png) 2px 0px no-repeat;
    cursor: pointer;
}

 

…and that’s it.

Let me know in the comments if this worked for you…

 

comments

14 Responses to “move the Shadowbox.js close button to the upper-right”

  1. Darren on February 2nd, 2011

    Nice work on this, thanks! FYI, I had to make the margin -50px instead of -20px to get it to work for me. And of course, the URL of the close PNG may need to be changed for anyone that has it in a different location.

  2. admin on February 2nd, 2011

    Hi Darren,
    thanks for pointing out the image path – i’ve changed it in the example to be more generic.

    ————————-

    Also – i noticed something else since i wrote this post – and its probably why you had to use -50px on the top margin. The -20px setting in my code assumes that there will be a value in the “title” attribute of the <a> tag that wraps the image, like so…

    <a href="path/to/someimage.jpg" rel="shadowbox" title="My Great Image">

    if it looks like this…

    <a href="path/to/someimage.jpg" rel="shadowbox" title="">

    …or this…

    <a href="path/to/someimage.jpg" rel="shadowbox">

    …you’ll probably have to use the -50px top margin you used.

    The problem with this approach is that – eventually, maybe some of your images WILL have titles, and then everything will look all messed up. The easiest solution is to keep the -20px top margin and just put something in the title attribute, like so…

    <a href="path/to/someimage.jpg" rel="shadowbox" title="&nbsp;">

    I hope this helps (someone out there).

    Best,
    Dustin

  3. Brad on February 8th, 2011

    This was extremely helpful! Thank you so much, I was over thinking this problem for the past few hours and a quick look here made me realize it.

    Thanks!

  4. Eric on February 24th, 2011

    Thanks Dustin, nice fix, saved me from having to figure that all out. I wanted to add that I actually played a little more with the css and popped a circle button kinda over top of the corner of the window (like u see commonly) by setting the

    #sb-wrapper {overflow:visible; (+everything else)}

    and the

    #sb-wrapper-inner {z-index:-1;(+everything else)}

    and the

    #sb-nav-close {
    float: right;
    width: 42px;
    height: 42px;
    display: block;
    /*margin: -25px 5px 0 -20px;*/
    position:absolute;
    top:0px;
    right:-20px;
    background-image:url(‘close.png’);
    cursor: pointer;
    overflow:visible;
    visibility:visible;
    z-index:1000;
    }

    getting rid of the margins saved the ie box-model issue with positioning and I just used top and right and a position of :absolute;

    Thank god for Firefox Firebug.

  5. anon on March 9th, 2011

    it is 7am – i have just spent the entire night working on a project due at 10am. for the last 3hrs i have been pulling my hair out trying to do what i thought would be a simple problem – moving the close button to the top right.

    Then i found your blog, you jsut saved me, i cannot thank you enough.

    -50px margin worked for me due to no title

    word!!

  6. Thadeus Jones on March 14th, 2011

    Hey, thanks for the tips! I’ve been meaning to move my close button for a while and found this article much more useful than what is currently out there.

    Keep up the good work!

  7. Brian on March 31st, 2011

    Worked perfectly. Thanks for documenting this.

  8. Claudio on May 31st, 2011

    Worked like a charm!
    Thank you so much for sharing this tip

    Regards

  9. Lisa on June 20th, 2011

    Thanks for this great tip! Almost had to give up on Shadowbox because client didn’t like x in bottom corner. WHEW! Thanks!

  10. Damian on June 27th, 2011

    nice one, thanks for this – worked perfectly for me, tried loads of other solution that didn’t work.

    This worked first go.

    Thanks again.
    D

  11. Barry on July 7th, 2011

    Worked great on the first try! Thank you VERY much for posting this!

  12. Navaid on September 20th, 2011

    Awesome workaround. Thanks!!

  13. Georgia Gibbs on December 16th, 2011

    Thank you for this. Clear instructions and easy to do!

  14. Rene Pustka on December 27th, 2011

    Worked perfectly. Thanks you. hi from Czech Republic

Leave a Reply




 
     
  • need my help?

    Interested in having me be a consultant on your next project?  to email me.

  •  
  • what i’m reading...

  • Run Windows on Mac OS X with no reboot!ING DIRECT Orange Savings Account- Apply Today!
    BasecampGo Daddy $6.99.com sale 125x125

    Order DIRECTV Today for $29.99 a month