Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a div over other content and in the center of webpage using CSS?

Tags:

css

How do I display a div over other content of webpage? This div must be centered vertically and horizontally as in this example:

enter image description here

I tried this:

<div style = "position: absolute; top: 50%; left: 50%;">DIV content</div>

In this example only the left corner is centered and not div itself.

like image 991
tfe Avatar asked Jul 15 '11 11:07

tfe


People also ask

How do I center a div in the middle of the screen CSS?

You can do this by setting the display property to "flex." Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.

How do I overlay a div in CSS?

By using a div with style z-index:1; and position: absolute; you can overlay your div on any other div . z-index determines the order in which divs 'stack'. A div with a higher z-index will appear in front of a div with a lower z-index . Note that this property only works with positioned elements.

How do I make a div appear above everything else?

Set the DIV's z-index to one larger than the other DIVs. You'll also need to make sure the DIV has a position other than static set on it, too. position relative and z index set to 999999999999999999999999999999999999999999999999999.

How do I put content in the middle of the page in CSS?

To just center the text inside an element, use text-align: center; This text is centered.


1 Answers

The following should work.

div{
    position:fixed;
    width:500px;
    height:600px;
    margin:-300px auto auto -250px;
    top:50%;
    left:50%;
    text-align:center;
}

The -300 pixels and -250 pixels are the negative half of the height/width of the div respectively.

There may be a better way of doing this, but this is a method I used a while back.

like image 180
Chris Avatar answered Nov 02 '22 23:11

Chris