-2

I've been trying to vertically center a div, tried both position: absolute and flexbox, neither worked.

Also tried to add styles to both the parent div and child divs, still did not work. It centers horizontally but does not center vertically

here's the HTML for the parent element

<div className="all-questions">{renderQuestions}</div>

here's the CSS for the parent element

.all-questions {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

Thank you!

ameymeow
  • 11
  • 4

2 Answers2

0

You need to assign a height then it will work. Try the below code.

.all-questions {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100vh;
}
0

I like to do it with display: flex;. Source: MDN

* {
  margin: 0;
  padding: 0;
}

.container {
  height: 100vh;
  width: 100vh;
  background: gray;
  display: flex;
  justify-content: center; /* center it horizontally (cross axis) */
  align-items: center; /* center it vertically (main axis) */
}

.v-centered {
  height: 40px;
  width: 40px;
  background: white;
}
<div class="container">
  <div class="v-centered"></div>
</div>
Sebastian
  • 1,321
  • 9
  • 21