I want to make a <div> block which width is 100% and set its position in the middle of the page (horizontal & vertical center), but without setting an exact height number (because I don't know the exact number of <div> height).
Here's my code:
#content {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
text-align: center;
width: 100%;
background: rgba(255, 0, 0, 0.5);
}
<html>
<head>
</head>
<body>
<div id="content">Hello world!</div>
</body>
</html>
The result will be like:
(It set its height to an explicit 100% automatically.)
But what I want is like:
I know I need to specify an exact number of height to achieve this effect. But now I don't know the exact height of the content, because the height of the content should be variable.
So how can I make the height adjust itself automatically?