How to center a div horizontally and vertically in CSS?

clock icon

asked 4 months ago Asked

message

2 Answers

eye

92 Views

 I'm struggling to perfectly center a div both horizontally and vertically using CSS. I've tried various methods, including using flexbox and positioning, but can't seem to get it right. Any insights or best practices?

2 Answers

One way to achieve this is by using the following CSS code:

 

.container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}

One way to achieve this is by using the following CSS code:

.center-div {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

Write your answer here

Top Questions