1
votes

I am trying to vertically align the white boxes so they are at the middle of the picture. I tried using align-items: center; but then I would need to add height to the row class which I don't want to.

http://codepen.io/VK72m/pen/ZeLaNx

<div class="container">
 <div class="row">
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
</div>
 <div class="row">
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
</div>

 body {
  margin: 0;
  padding: 0;
  font-size: 100%;
}

.container {   
  align-items: center;    
  margin: 0;
  padding: 0;
  height: 19em;
  width: 20em;
  background-color: black;
  background: url("https://images.pexels.com/photos/340779
  /pexels-photo- 340779.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb");
  background-position: -22em ;
}

.row {
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
}

.box {
  margin: 0;        
  padding: 0;
  height: 3em;
  width: 3em;
  margin: 0.9em;      
  background-color: white;
}
4

4 Answers

1
votes

add flex to the container:

.container {
    display: flex;
    flex-direction: column;
    justify-content: center;
}
0
votes

justify-content: center

also here is a great resource: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

0
votes

It defines the default behavior for how flex items are laid out along the cross axis on the current line. You can think of it as the justify-content version for the cross-axis (perpendicular to the main-axis).

The align-items property accepts 5 different values:

  • flex-start: cross-start margin edge of the items is placed on the cross-start line
  • flex-end: cross-end margin edge of the items is placed on the cross-end line
  • center: items are centered in the cross-axis
  • baseline: items are aligned such as their baselines align
  • stretch (default): stretch to fill the container (still respect min-width/max-width)

The following figure helps understand how flex items are layed out depending on the align-items value.

enter image description here

align-items: flex-start | flex-end | center | baseline | stretch

.flex-container {
  align-items: flex-start;
}

http://codepen.io/HugoGiraudel/pen/2bfe2c024739bdd4098572f87d1bf585

0
votes

Try This Simple Flexbox system

.flex-container {
  padding: 0;
  margin: 0;
  list-style: none;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-flex-flow: row wrap;
  justify-content: space-around;
}

.flex-item {
  background: orange;
  padding: 5px;
  width: 200px;
  height: 150px;
  margin-top: 10px;
  line-height: 150px;
  color: white;
  font-weight: bold;
  font-size: 3em;
  text-align: center;
}
<ul class="flex-container">
  <li class="flex-item">1</li>
  <li class="flex-item">2</li>
  <li class="flex-item">3</li>
  <li class="flex-item">4</li>
  <li class="flex-item">5</li>
  <li class="flex-item">6</li>
</ul>