How do I display a div over other content of webpage? This div must be centered vertically and horizontally as in this example:
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.
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.
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.
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.
To just center the text inside an element, use text-align: center; This text is centered.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With