0

I have a fixed positioned DIV called Lightbox. My problem is that the close button doesn't stay on the top right, when I scroll the content.

How can I achieve that the close button stays on the top right corner?

Fiddle Link

.lightbox { position: fixed; background: white; top: 50%; left: 50%; width: 600px; height: 400px; border: 1px solid black; margin-left: -300px; margin-top: -200px; z-index: 10000; overflow-y: auto; } .close-btn { position: absolute; top: 0; right: 0; background: black; color: white; font-weight: bold; border-radius: 100%; width: 20px; height: 20px; text-align: center; } .item { width: 250px; display: inline-block; border: 1px solid blue; height: 300px; background: lightblue; }
<div class="lightbox"> <div class="close-btn">x</div> <div class="items"> <div class="item">Item</div> <div class="item">Item</div> <div class="item">Item</div> <div class="item">Item</div> </div> </div>

3
  • change position:fixed to .close-btn jsfiddle.net/dgw8tj5r/2 Commented Feb 6, 2015 at 12:48
  • Use position fixed for the close-button. Commented Feb 6, 2015 at 12:50
  • @SathishS Sorry, but the button should be on the top right of the lightbox div and not of the html element. Preferable is a pure CSS solution. Commented Feb 6, 2015 at 12:51

4 Answers 4

4

Make the item div scrollable instead of the lightbox, then the close button will stay absolutely positioned in the top right corner.

Here is the CSS that I changed:

.lightbox { overflow: hidden; } .close-btn { top: 5px; right: 20px; } .items { width: 600px; height: 400px; overflow-y: auto; } 

JSFiddle: http://jsfiddle.net/dgw8tj5r/4/

Sign up to request clarification or add additional context in comments.

Comments

2

You can achieve a sticky button to your lightBox div by adjusting your HTML a bit and adding a container to your lightBox content:

<div class="lightbox"> <div class="close-btn">x</div> <div class="container"> <div class="items"> <div class="item">Item</div> <div class="item">Item</div> <div class="item">Item</div> <div class="item">Item</div> </div> </div> </div> 

Then instead of the .lightbox div, you add your width, height, overflow properties on this new .container.

.container{ overflow-y: auto; width: 600px; height: 400px; } 

Now your close-btn will not be included in the scrolling part.

JSFiddle demo

EDIT: Benjamin's answer is a more efficient version since you actually already have a containing div: .items. You can use that instead of adding a new one.

2 Comments

Anshelin this is not fixing the issue I think. OP clearly wants a fixed position inside her fixed element, which is harder but not impossible.
I see! I think this is what OP was looking for :)
1
  1. Draw light box with proper position.
    2.Add close button and container inside light box ans close button position where you need. 3.Finally added items inside container.

thats it.

thanks.

Fixed here

<div class="lightbox"><div class="close-btn">x</div> <div class="lightboxdv"> <div class="items"> <div class="item">Item</div> <div class="item">Item</div> <div class="item">Item</div> <div class="item">Item</div> </div> </div> </div> .lightbox { position: fixed; background: white; top: 50%; left: 50%; border: 1px solid black; margin-left: -300px; margin-top: -200px; z-index: 10000; overflow:hidden; } .lightboxdv{ width: 560px; height: 400px; overflow-y: auto; } .close-btn { position: absolute; top: 0; right: 20px; background: black; color: white; font-weight: bold; border-radius: 100%; width: 20px; height: 20px; text-align: center; } .item { width: 250px; display: inline-block; border: 1px solid blue; height: 300px; background: lightblue; } 

1 Comment

Please consider adding a bit of explanation to your code, so other viewers can understand more easily how it works.
0

All you need to do is change .close-btn position to fixed.

.close-btn { position: fixed; } 

I hope it works!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.