I would like to vertically align a div (rectangle
), this div doesn't have any parents, i created one (container
) to use the vertical-align
property but i could remove it if someone knows a better way.
I just want to center a rectangle into the body
div (horizontally and vertically) and make it responsive.
Here is what i tried but it doesn't work (the rectangle stay close to the top of the window):
<html>
<head>
<style>
.rectangle {
width: 88%;
height: 87%;
margin: auto;
padding:10px;
background:red;
display:inline-block;
vertical-align:middle;
}
.container {
text-align: center;
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<div class="container">
<div class="rectangle">
</div>
</div>
</body>
</html>
Is it possible to achieve only using CSS or do we need javascript?