1

I have two div tags in a container. I want to use flex box and align the first div tag to the top and the other be centered relative to the container. How can I do this?

Using align-items: flex-start only moves it to the left when flex-direction is column. What is the equivalent css to move it to the top?

codepen: https://codepen.io/GMSg68/pen/aGpOpG

.box {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 500px;
  border: 1px solid blue;
}

.box div {
  width: 100px;
  height: 100px;
  border: 1px solid darkgray;
}

.box div:nth-child(1) {
  /*  How do I push the first div tag to the top? */
  background-color: lightgray;
}

.box div:nth-child(2) {
  background-color: lightblue;
}
<div class="box">
  <div>One</div>
  <div>Two</div>
</div>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
user3137124
  • 515
  • 1
  • 7
  • 13

1 Answers1

2

Add margin top and bottom auto to the nth-child(2) div. Then add a Y transform of -50% to align it to the centre. This assumes the two divs are the same height.

.box div:nth-child(2){
  background-color: lightblue;
  margin-top: auto;
  margin-bottom: auto;
  transform: translateY(-50%);
}
Willans
  • 143
  • 5