Considering a grid that contains fixed size items (not my choice), and is not fixed size.
I would like to centralize the container (.grid-container
) to have same side margin no matter how many items it have and how they are placed inside container as long as they are left-aligned.
If it isn’t possible to do this only with CSS how can I do using JavaScript?
It’s not necessary to use twitter-bootstrap grid system like the demo code.
Here a code to illustrate: https://embed.plnkr.co/F6niiwaosnO8tr0ss4XF/
.container {
background-color: #2B3643;
}
.grid-container {
padding: 30px 30px 0 30px;
}
.grid-item-wrapper {
width: 200px;
margin-bottom: 15px;
}
.grid-item {
margin-bottom: 15px;
}
.grid-item > .item-desc {
padding: 8px;
background-color: #333333;
color: #CFCFCF;
}
<div class="container">
<div class="grid-container row">
<!-- grid items -->
</div>
</div>
<template id="tmpl_grid_item">
<div class="grid-item-wrapper col-xs-3">
<div class="grid-item">
<div class="item-picture">
<img src="http://lorempixel.com/350/200/food/" style="width: 100%;" />
</div>
<div class="item-desc">
Lorem Ipsum
</div>
</div>
</div>
</template>
2
Answers
If you are willing to use absolute positioning and CSS3, there is a way.
HTML:
CSS:
Here is a working fiddle. Hope it helps!
Just so that the code is out there in case it’s useful, you can get the same behavior as F. Versehgi’s answer without absolute positioning. This uses flexbox (so requires IE11, but any other browser should be fine) and some media queries (to account for the Bootstrap responsive container).
Note: You’ll want to view the JSFiddle or CodePen link below, because the Stack Snippet is not working properly, for some reason: