How to center a div across all browsers and behind this div there should be a transparent background layer covering entire screen of browser like lightbox.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you give the div a fixed width, it’s easy to use negative margins:
Without a fixed height, you cannot center the div vertically without JavaScript. With a dynamic height, you can vertically center the div using a snippet like this (in jQuery):
As for the transparent overlay, just give it an absolute position and a full width and height:
If you can ditch IE6 support, you can simply use
position: fixedinstead ofabsolute, that way the divs will be centered even if the user scrolls the page, and even when JavaScript is turned off.