-1

can you help me here in this issue? I have this div which contains 7 divs. Currently, using the flex box, I get this output

enter image description here

However, what I want to happen should be like this:

enter image description here

Below are some codes that I roughly have

.Container {
  text-align: center;
  margin-left: 10px;
  margin-right: 10px;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: space-around;
  align-items: center;
  align-content: center;
}
.Items {
  margin-bottom: 7px;
  width: 80px;
  height: 80px;
  text-align: center;
  font-size: 12px;
  line-height: 10px;
  border: 0px;
  padding-bottom: 7px;
  align-self: center;
}
<div class="Container">
  <div class="Items">
    <img height="40px" width="40px" src="http://img10.deviantart.net/af75/i/2014/242/1/9/kawaii_potato_by_hashtagpony-d7xbs1t.png" />
    <br/>
    <span>Icon 1</span>
  </div>
  <div class="Items">
    <img height="40px" width="40px" src="http://img10.deviantart.net/af75/i/2014/242/1/9/kawaii_potato_by_hashtagpony-d7xbs1t.png" />
    <br/>
    <span>Icon 1</span>
  </div>
  <div class="Items">
    <img height="40px" width="40px" src="http://img10.deviantart.net/af75/i/2014/242/1/9/kawaii_potato_by_hashtagpony-d7xbs1t.png" />
    <br/>
    <span>Icon 1</span>
  </div>
  <div class="Items">
    <img height="40px" width="40px" src="http://img10.deviantart.net/af75/i/2014/242/1/9/kawaii_potato_by_hashtagpony-d7xbs1t.png" />
    <br/>
    <span>Icon 1</span>
  </div>
  <div class="Items">
    <img height="40px" width="40px" src="http://img10.deviantart.net/af75/i/2014/242/1/9/kawaii_potato_by_hashtagpony-d7xbs1t.png" />
    <br/>
    <span>Icon 1</span>
  </div>
  <div class="Items">
    <img height="40px" width="40px" src="http://img10.deviantart.net/af75/i/2014/242/1/9/kawaii_potato_by_hashtagpony-d7xbs1t.png" />
    <br/>
    <span>Icon 1</span>
  </div>
  <div class="Items">
    <img height="40px" width="40px" src="http://img10.deviantart.net/af75/i/2014/242/1/9/kawaii_potato_by_hashtagpony-d7xbs1t.png" />
    <br/>
    <span>Icon 1</span>
  </div>
</div>

UPDATE Thank you for those who replied. However, I forgot to add these things.

  1. I want that the icons be spread out evenly that's why I need the justify-content: space-around
  2. I also want that the icons be horizontally at the center that's why I used the align-items: center; align-content: center;

The problem is this: Is it possible to have both?

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Musikero31
  • 3,115
  • 6
  • 39
  • 60

4 Answers4

1

Instead of justify-content: space-around use justify-content: flex-start.

Demo: http://jsfiddle.net/rsvmsku6/


Update (based on comments):

For guidance creating a grid that is horizontally centered, but the child elements are left-aligned, see these posts:

Community
  • 1
  • 1
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
  • You want the icons aligned-left with space in between (like in your second image), but you want the container centered horizontally on the screen? – Michael Benjamin Oct 30 '15 at 00:24
0

Remove the following css from Container:

justify-content: space-around;
Dominic
  • 62,658
  • 20
  • 139
  • 163
0
justify-content: space-around;

That's your problem. If you remove that line, the items will display as you want them too

JSFiddle

space-around:

items are evenly distributed in the line with equal space around them

flex-start (default)

items are packed toward the start line

SlashmanX
  • 2,489
  • 18
  • 16
0

You dont have a need for some of the elements.

  • align-items
  • align-content

and you also need to set the justify-content to be flex-start which will always set it to the left.

.Container {
  text-align: center;
  margin-left: 10px;
  margin-right: 10px;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: flex-start;
  width: 320px;
}
.Items {
  margin-bottom: 7px;
  width: 80px;
  height: 80px;
  text-align: center;
  font-size: 12px;
  line-height: 10px;
  border: 0px;
  padding-bottom: 7px;
  align-self: center;
}
<div class="Container">
  <div class="Items">
    <img height="40px" width="40px" src="http://img10.deviantart.net/af75/i/2014/242/1/9/kawaii_potato_by_hashtagpony-d7xbs1t.png" />
    <br/>
    <span>Icon 1</span>
  </div>
  <div class="Items">
    <img height="40px" width="40px" src="http://img10.deviantart.net/af75/i/2014/242/1/9/kawaii_potato_by_hashtagpony-d7xbs1t.png" />
    <br/>
    <span>Icon 1</span>
  </div>
  <div class="Items">
    <img height="40px" width="40px" src="http://img10.deviantart.net/af75/i/2014/242/1/9/kawaii_potato_by_hashtagpony-d7xbs1t.png" />
    <br/>
    <span>Icon 1</span>
  </div>
  <div class="Items">
    <img height="40px" width="40px" src="http://img10.deviantart.net/af75/i/2014/242/1/9/kawaii_potato_by_hashtagpony-d7xbs1t.png" />
    <br/>
    <span>Icon 1</span>
  </div>
  <div class="Items">
    <img height="40px" width="40px" src="http://img10.deviantart.net/af75/i/2014/242/1/9/kawaii_potato_by_hashtagpony-d7xbs1t.png" />
    <br/>
    <span>Icon 1</span>
  </div>
  <div class="Items">
    <img height="40px" width="40px" src="http://img10.deviantart.net/af75/i/2014/242/1/9/kawaii_potato_by_hashtagpony-d7xbs1t.png" />
    <br/>
    <span>Icon 1</span>
  </div>
  <div class="Items">
    <img height="40px" width="40px" src="http://img10.deviantart.net/af75/i/2014/242/1/9/kawaii_potato_by_hashtagpony-d7xbs1t.png" />
    <br/>
    <span>Icon 1</span>
  </div>
</div>
Stewartside
  • 20,378
  • 12
  • 60
  • 81