2

I would like child elements with a fixed width and height to be placed in flexbox one by one with a small indentation between them and a parent element to be centered, but not centering it's child elements.

All my attempts to force the parent element to be centered and stop centering it's child elements failed, among them:

align-self: center;
margin: 0 auto;
justify-content: space-between;

Here is the link for a preview. All I need is parent element to be centered.

Just like that

justify-content: center; aligns parent div, but I dont want last row (if it isn't completely filled with elements) to be centered.

So how can I do that? Thank you in advance.

Saurav Rastogi
  • 9,575
  • 3
  • 29
  • 41

1 Answers1

1

Use justify-content: center; on the parent .grid-view.

Have a look at this snippet below:

.grid-view {
    display: flex;
    padding: 0px;
    margin: 0px;
    align-self: center;
    flex-wrap: wrap;
    align-items: flex-start;
    justify-content: center;
}

.grid-view .item {
    flex-shrink: 1;
    margin: 5px;
    border: 1px solid #333;
    align-self: flex-start;
}

.grid-view .item .item-cover {
    width: 200px;
    border: 3px solid #006699;
    border-radius: 3px;
}
<div class="grid-view"> 
    <div class="item">
        <img class="item-cover" alt="attēls" src="https://pbs.twimg.com/profile_images/660946436801101824/niM7azZS.jpg" />
        <h2 class="item-title">{{Title}}</h2>
        <p class="item-description">{{item description}}</p>
    </div>
    <div class="item">
        <img class="item-cover" alt="attēls" src="https://pbs.twimg.com/profile_images/660946436801101824/niM7azZS.jpg" />
        <h2 class="item-title">{{Title}}</h2>
        <p class="item-description">{{item description}}</p>
    </div>
    <div class="item">
        <img class="item-cover" alt="attēls" src="https://pbs.twimg.com/profile_images/660946436801101824/niM7azZS.jpg" />
        <h2 class="item-title">{{Title}}</h2>
        <p class="item-description">{{item description}}</p>
    </div>
    <div class="item">
        <img class="item-cover" alt="attēls" src="https://pbs.twimg.com/profile_images/660946436801101824/niM7azZS.jpg" />
        <h2 class="item-title">{{Title}}</h2>
        <p class="item-description">{{item description}}</p>
    </div>
    <div class="item">
        <img class="item-cover" alt="attēls" src="https://pbs.twimg.com/profile_images/660946436801101824/niM7azZS.jpg" />
        <h2 class="item-title">{{Title}}</h2>
        <p class="item-description">{{item description}}</p>
    </div>
    <div class="item">
        <img class="item-cover" alt="attēls" src="https://pbs.twimg.com/profile_images/660946436801101824/niM7azZS.jpg" />
        <h2 class="item-title">{{Title}}</h2>
        <p class="item-description">{{item description}}</p>
    </div>
    <div class="item">
        <img class="item-cover" alt="attēls" src="https://pbs.twimg.com/profile_images/660946436801101824/niM7azZS.jpg" />
        <h2 class="item-title">{{Title}}</h2>
        <p class="item-description">{{item description}}</p>
    </div>
    <div class="item">
        <img class="item-cover" alt="attēls" src="https://pbs.twimg.com/profile_images/660946436801101824/niM7azZS.jpg" />
        <h2 class="item-title">{{Title}}</h2>
        <p class="item-description">{{item description}}</p>
    </div>
</div>

Hope this helps!

Saurav Rastogi
  • 9,575
  • 3
  • 29
  • 41
  • But then last elements are centered. I would like them to be on the left. –  Dec 08 '16 at 18:04
  • @po4teda I don't think this is exactly possible for the last row to stay left unless CSS has a way to detect wrapping (which it does not have) ... – kukkuz Dec 08 '16 at 18:10
  • @kukkuz, I've updated my question. Is it possible? –  Dec 08 '16 at 18:17
  • @po4teda I get your point. For this you need to know the item that is getting wrapped, but I don't think there is a way for tracking it. This is what kukkuz is trying to tell you too. – Saurav Rastogi Dec 08 '16 at 18:20
  • IMO you need JS to detect wrapping... – kukkuz Dec 08 '16 at 18:22
  • @SauravRastogi @kukkuz thank you, I appreciate your help. I'm upset that JavaScript is the only way to do that. Will make it with a lot of `float` and inline blocks. –  Dec 08 '16 at 23:41