@Jahan-Shah
Posted
Hi Matin 👋. Weldone on solving the challenge, however, I'm here to answer your question on centering a div.
Let's say we have the following HTML:
<div class="parent">
<div class="child">Some Content</div>
</div>
Let's talk about centering the div in CSS: There are three commonly used ways we can use to center a div in CSS, you can use whichever method that's suitable.
-1st:
.parent {
position: absolute;
top: 50%; //centers the child div vertically
left: 50%; // centers the child div horizontally
transform: translate(-50%, -50%); //first value for x-axis translation and second for y-axis
}
-2nd:
.parent {
display: flex;
justify-content: center; //centers child div horizontally
align-items: center; //centers child div vertically
margin-inline: auto; //you can use this too instead of justify-content
}
you can read more about Flexbox here.
-3rd:
.parent {
display: grid;
place-items: center; // centers div both vertically and horizontally in grid;
}
you can read more about Grid here.
Note: there are a lot of ways to center a div, but you can use these three to get your work done easily. But if you are curious to find other ways too you can search for them on the Internet.
I hope you find this helpful 😄
Marked as helpful
@MachineCode0101
Posted
@Jahan-Shah Thank you Jahan for the clear and multiple ways of solving the problem! They all were helpful!