Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flexbox align item vertical 1/3

Tags:

css

flexbox

Now I learned how to vertical centering an item with flexbox, but how can I align the item to the 1/3 position vertically? thanks

like image 649
wong2 Avatar asked Jan 24 '17 03:01

wong2


2 Answers

Assuming you mean the space below the flex item to be 3 times the space above, you can add pseudo-elements with flex-grow 1 and 3:

#container {
  display: flex;
  flex-direction: column;
  height: 200px;
  background: #aaf;
}
#container > div {
  height: 50px;
  background: #afa;
}
#container::before {
  content: '';
  flex: 1;
}
#container::after {
  content: '';
  flex: 3;
}
<div id="container">
  <div>Content</div>
</div>
like image 175
Oriol Avatar answered Oct 24 '22 16:10

Oriol


You can use transform: translate

#container {
  display: flex;
  flex-direction: column;
  height: 180px;
  background: gray;
}
#container > div {
  position: relative;
  top: 33%;
  transform: translateY(-33%);
  background: lightgray;
}
<div id="container">
  <div>Content</div>
</div>
like image 35
Asons Avatar answered Oct 24 '22 17:10

Asons