If you're looking to center an element on a web page, here's a simple method that can help:
In your HTML -
<div class="content">
</div>
You can name this div
whatever you like as long as it's the element you want to be in the center of the page. Then, in your CSS, add the following -
html, body {
width: 100%;
height: 100%;
background: #000;
}
.content {
width: 50%;
height: 50%;
position: relative;
top: 50%;
transform: translateY(-50%);
background: #fff;
margin: 0 auto;
}
Setting the width and height of html and body at 100% gives your content something to center from.
position: relative;
top: 50%;
transform: translateY(-50%);
This part is crucial. It tells the browser to vertically align the content
within its parent element along the Y axis. The parent element in this case is the body
. To ensure horizontal alignment, include
margin: 0 auto;
Feel free to play around with the height
and width
values to see how the centring holds up. Your content should remain centered regardless of its dimensions.