I am trying to make a grid system with inspiration from this Bootstrap 3 template:
Link to template
I can see in the code that the 4 big grid elements are controlled with setting a height. I can make the first 2 grid items, but I am lost right now how to make the 2 small columns.
Here is the code what I made until now:
[Link to my demo model][3]
<style>
.bg-img {
background-position: center center;
background-size: cover;
background-repeat: no-repeat;
}
.item1 {
height:600px;
}
.item2 {
height:300px;
}
.item3 {
height: 300px;
}
</style>
<section>
<div class="container-fluid">
<div class="row">
<div class="col-sm-6 bg-img item1" style="background-image:url(https://placehold.it/600x600;)"></div>
<div class="col-sm-6 bg-img item2" style="background-image:url(https://placehold.it/300x600;)"></div>
</div>
</div>
</section>
Can anybody help me?
Best regards.
I hope this code will help you. I make this by Grid css.
.grid-container {
display: grid;
grid-gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-item {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px;
font-size: 30px;
}
.item1 {
grid-column: 1 / span 2;
grid-row: 1;
}
.item2 {
grid-column: 3;
grid-row: 1 / span 2;
}
<div class="grid-container">
<div class="grid-item item1">1</div>
<div class="grid-item item2">2</div>
<div class="grid-item item3">3</div>
<div class="grid-item item4">4</div>
</div>
I hope this will solve your issue.
I replaced your images with a simple background-color, but overall it should be fully responsive. (It's not 100% perfect - left container is wider than the right container)
Some helpful links: bootstrap overview, bootstrap grid
Note: view example on full page :)
body {
padding: 15px;
}
.item-1 {
height: 600px;
background: blue;
}
.item-2 {
height: 600px;
padding-right: 0;
/* container right */
background-color: white;
}
.item-3 {
height: 292.5px;
margin-bottom: 7.5px;
background-color: green;
}
.item-4 {
padding: 0 15px;
}
.item-5 {
height: 292.5px;
margin-top: 7.5px;
border-right: 7.5px solid white;
background-color: purple;
}
.item-6 {
height: 292.5px;
margin-top: 7.5px;
border-left: 7.5px solid white;
background-color: yellow;
}
/* Small devices (landscape phones, less than 768px) */
#media (max-width: 767.98px) {
.item-2 {
padding: 0 !important;
margin: 0;
height: auto;
}
.item-3,
.item-5,
.item-6 {
border: 0;
padding: 0;
margin: 0;
height: 300px;
}
/* To prevent responsiveness issue, caused by .row bootstrap class */
.no-margin {
margin: 0;
}
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid">
<div class="no-margin row">
<div class="item-1 col-md"></div>
<div class="item-2 col-md">
<div class="item-3 col-md"></div>
<div class="item-4 row">
<div class="item-5 col-md"></div>
<div class="item-6 col-md"></div>
</div>
</div>
</div>
</div>
Related
I'm a bit new to front end and doing my best to figure things out. I have been building a relatively simple landing page and all looking great except when I try to use a css grid. The text (people's names) is changing the size of the images so that none of them are the same size (see the attached image). It seems that the images directly above and below each other grow together if the text is changed for one. While the images next to each other left and right change inversely. Removing all text makes them the same size.
Here is a fiddle for a runnable copy.
I would really like to set the grid to take up a percentage of the container it's in if it's possible. For the life of me I can't find what I am missing. Is there some way that I can set all images to stay the same size as one another?
I will try to include the relevant pieces of CSS.
.team {
padding: 66px 0 80px 0;
background-image: url("assets/pattern-team.png");
background-repeat: no-repeat;
background-size: contain;
background-position: center center;
width: 100%;
}
.team .container {
display: flex;
}
.team-heading {
width: 30%;
margin-right: 80px;
}
.team-heading p {
margin-top: 17px;
}
.team-members {
width: 70%;
display: grid;
grid-template-areas: "name1." "name2 name3";
gap: 30px;
row-gap: 30px;
}
.name1 {
grid-area: name1;
}
.name2 {
grid-area: name2;
}
.name3 {
grid-area: name3;
}
enter code here .team-member {
cursor: pointer;
}
.team-member .team-img {
box-shadow: 10px 10px #7CE04E;
border: 2px solid black;
}
.team-member h4 {
margin: 15px 0 5px 0;
}
.team img {
display: block;
}
<!-- Our team -->
<section class="team">
<div class="container">
<div class="team-heading">
<h2>Our team</h2>
<p>Meet the cats behind the magic.</p>
</div>
<div class="team-members">
<!-- Griff -->
<div class="team-member griff">
<div>
<img class="team-img" src="https://upload.wikimedia.org/wikipedia/commons/6/62/Solid_red.svg" />
</div>
<h4>Griff</h4>
<p>description</p>
</div>
<!-- Brenden -->
<div class="team-member brenden">
<div>
<img class="team-img" src="https://upload.wikimedia.org/wikipedia/commons/6/62/Solid_red.svg" />
</div>
<h4>Brenden</h4>
<p>description</p>
</div>
<!-- Thu -->
<div class="team-member thu">
<div>
<img class="team-img" src="https://upload.wikimedia.org/wikipedia/commons/6/62/Solid_red.svg" />
</div>
<h4>Thu</h4>
<p>description</p>
</div>
</div>
</div>
</section>
Thanks in advance for any pointers or even a suggestion of where to start searching!
Simply add column templates:
grid-template-columns: 1fr 1fr;
h4 {
font-size: 33px;
line-height: 43px;
}
.team {
padding: 66px 0 80px 0;
background-image: url("assets/pattern-team.png");
background-repeat: no-repeat;
background-size: contain;
background-position: center center;
width: 100%;
}
.team .container {
display: flex;
}
.team-heading {
width: 30%;
margin-right: 80px;
}
.team-heading p {
margin-top: 17px;
}
.team-members {
width: 70%;
display: grid;
/*Set 2 eqaul columns*/
grid-template-columns: 1fr 1fr;
grid-template-areas: "griff ." "brenden thu";
gap: 30px;
row-gap: 30px;
}
.griff {
grid-area: griff;
}
.brenden {
grid-area: brenden;
}
.thu {
grid-area: thu;
}
.team-member .team-img {
box-shadow: 10px 10px #7CE04E;
border: 2px solid black;
}
.team-member h4 {
margin: 15px 0 5px 0;
}
.team img {
display: block;
}
<!-- Our team -->
<section class="team">
<div class="container">
<div class="team-heading">
<h2>Our team</h2>
<p>Meet the cats behind the magic.</p>
</div>
<div class="team-members">
<!-- Griff -->
<div class="team-member griff">
<div>
<img
class="team-img"
src="https://upload.wikimedia.org/wikipedia/commons/6/62/Solid_red.svg"
/>
</div>
<h4>Griff</h4>
<p>description</p>
</div>
<!-- Brenden -->
<div class="team-member brenden">
<div>
<img
class="team-img"
src="https://upload.wikimedia.org/wikipedia/commons/6/62/Solid_red.svg"
/>
</div>
<h4>Brenden</h4>
<p>description</p>
</div>
<!-- Thu -->
<div class="team-member thu">
<div>
<img class="team-img" src="https://upload.wikimedia.org/wikipedia/commons/6/62/Solid_red.svg"/>
</div>
<h4>Thu</h4>
<p>description</p>
</div>
</div>
</div>
</section>
I want divide my HTML Page into 4 different vertical sections .
I want each section to have a different background color, for that I used div but it each background color does not cover the sides of each section.
** My aspire end result:
I don't want to see the color red of the body background color in the html.
body {
background-color: red;
}
.intro {
background-color: #674AB3;
}
.edu {
background-color: #A348A6;
}
.Skills {
background-color: #9F63C4;
}
.end {
background-color: #9075D8;
}
<div class="intro">
<hr>
</div>
<div class="edu">
<hr>
</div>
<div class="Skills">
<hr>
</div>
<div class="end">
<hr>
</div>
.flex-container {
display: flex;
justify-content: center;
align-items: center;
}
.flex-container>div {
margin:30px;
}
.flex-container hr {
width: 100px;
height: 100px;
padding: 5px;
margin: 10px;
border-color: #FFF;
box-shadow: none;
border-width: 5px;
}
.intro {
background-color: #674AB3;
}
.edu {
background-color: #A348A6;
}
.Skills {
background-color: #9F63C4;
}
.end {
background-color: #9075D8;
}
<div class="flex-container">
<div class="intro"><hr></div>
<div class="edu"><hr></div>
<div class="Skills"><hr></div>
<div class="end"><hr></div>
</div>
Set margin: 0 for body, it has a defualt margin.
Set <hr>'s margin to 0.
Set height for each div to be 25vh (vertical height).
body {
background-color: red;
margin: 0;
}
.intro {
background-color: #674AB3;
height: 25vh;
}
.edu {
background-color: #A348A6;
height: 25vh;
}
.Skills {
background-color: #9F63C4;
height: 25vh;
}
.end {
background-color: #9075D8;
height: 25vh;
}
hr {
margin: 0;
}
<div class="intro">
<hr/>
</div>
<div class="edu">
<hr/>
</div>
<div class="Skills">
<hr/>
</div>
<div class="end">
<hr/>
</div>
You could try using grid! might as well make it responsive :D
This is to have 4 sections laying one next to another, to make them stack in the vertical direction, change:
grid-template-columns: repeat(4, 1fr);
to:
grid-template-rows: repeat(4, 1fr);
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: #00000000; /* transparent color */
display: grid;
grid-template-columns: repeat(4, 1fr); /* 4 vertical sections */
height: 100vh;
margin: auto;
}
.intro {
background-color: #674AB3;
}
.edu {
background-color: #A348A6;
}
.Skills {
background-color: #9F63C4;
}
.end {
background-color: #9075D8;
}
<div class="intro">
<p>intro</p>
</div>
<div class="edu">
<p>edu</p>
</div>
<div class="Skills">
<p>Skills</p>
</div>
<div class="end">
<p>end</p>
</div>
Something like this?
body {
background-color: red;
}
.intro {
height:200px;
background-color: #674AB3 !important;
}
.edu {
height:200px;
background-color: #A348A6 !important;;
}
.Skills {
height:200px;
background-color: #9F63C4 !important;;
}
.end {
height:200px;
background-color: #9075D8 !important;;
}
<div class="intro">
<hr>
</div>
<div class="edu">
<hr>
</div>
<div class="Skills">
<hr>
</div>
<div class="end">
<hr>
</div>
You can try this approach as well.
body {background-color:transparent;}
.intro {
background-color: #674AB3;
height:100vh;
width:100%;
}
.edu {
background-color: #A348A6;
height:100vh;
width:100%;
}
.Skills {
background-color: #9F63C4;
height:100vh;
width:100%;
}
.end {
background-color: #9075D8;
height:100vh;
width:100%;
}
.wrapper {
display:grid;
height:100vh;
width:100%;
}
<div class="wrapper">
<div class="intro">
Hello
</div>
<div class="edu">
Hello
</div>
<div class="Skills">
Hello
</div>
<div class="end">
Hello
</div>
</div>
You can simmply use display: flex to the parent container which is flex-container
like
<div style="display: flex;">
<div class="intro"><hr></div>
<div class="edu"><hr></div>
<div class="Skills"><hr></div>
<div class="end"><hr></div>
</div>
<div class="intro">
</div>
<div class="edu">
</div>
<div class="Skills">
</div>
<div class="end">
</div>
</div>
body {
background-color: red;
}
.main{
display: flex;
grid-template-columns: repeat(4,1fr);
width:100vw;
}
.intro {
background-color: #674AB3;
width: 25%;
height: 75vh;
}
.edu {
background-color: #A348A6;
width: 25%;
height: 75vh;
}
.Skills {
background-color: #9F63C4;
width: 25%;
height: 75vh;
}
.end {
background-color: #9075D8;
width: 25%;
height: vh;
}
grid
<div class="main">
<div class="intro">
</div>
<div class="edu">
</div>
<div class="Skills">
</div>
<div class="end">
</div>
.main{
display: flex;
grid-template-columns: repeat(4,1fr);
width:100vw;
}
.intro {
background-color: #674AB3;
width: 25%;
height: 75vh;
}
.edu {
background-color: #A348A6;
width: 25%;
height: 75vh;
}
.Skills {
background-color: #9F63C4;
width: 25%;
height: 75vh;
}
.end {
background-color: #9075D8;
width: 25%;
height:75vh;
}
I'm having issues having full width divs that have different % widths and also keeping the content fixed within it and lining it up with another full width div. The JS Fiddle should be better at explaining this.
<header> <!-- 100% width -->
<div></div> <!-- 100% width; max-width: 1000px; margin: auto -->
</header>
The above works fine if it was a single column.
I want to have two divs below it, one taking up 33% and the other taking up 67% and keeping the content within these lined up similar to how the above is working. The max-width div is the visible content container. So if you were viewing the site on a large screen everything would be edge to edge, but the content within would be framed in the middle.
Sample fiddle, where the divs with 2 and 3 should take up the same amount of space as the div above it. http://jsfiddle.net/qtLe7o8f/1/
header {
background: blue;
padding: 15px 0;
}
header div {
max-width: 500px;
background: red;
margin: auto;
}
section.one {
float: left;
width: 33%;
background: green;
padding: 15px 0;
}
section.one div {
background: red;
float: right;
}
section.two {
float: left;
width: 67%;
background: orange;
padding: 15px 0;
}
section.two div {
background: red;
float: left;
}
<header>
<div>
1
</div>
</header>
<section class="one">
<div>
2
</div>
</section>
<section class="two">
<div>
3
</div>
</section>
I think this is what you're going for? It's setup with some nested flexbox definitions so that the outer containers stretch edge to edge, but the child elements stick to a max-width and share the available space.
header {
background: blue;
padding: 15px 0;
display: flex;
justify-content: center;
}
header div {
flex: 0 0 500px;
background: red;
}
.container {
display: flex;
justify-content: center;
background-color: #ace;
}
.content {
display: flex;
flex-flow: row wrap;
width: 500px;
}
section.one {
flex: 0 0 33%;
background: green;
padding: 15px 0;
}
section.two {
flex: 0 0 67%;
background: orange;
padding: 15px 0;
}
section.one div, section.two div {
background: red;
}
<header>
<div>
1
</div>
</header>
<div class="container">
<div class="content">
<section class="one">
<div>
2
</div>
</section>
<section class="two">
<div>
3
</div>
</section>
</div>
</div>
If you're looking to extend the orange and green background colors to the edges of the desktop screen this should do it for your. This will also reorder your columns for mobile devices thanks to the use of a Bootstrap Grid
header {
background: blue;
padding: 15px 0;
}
.custom-container {
background: linear-gradient(to right, green 0%,green 50%,orange 50%,orange 50%,orange 100%);
}
.section-1-container {
background: red;
}
.row .section-2-container {
background: green;
padding-top: 15px;
padding-bottom: 15px;
padding-left: 0px;
padding-right: 0px;
}
section.one div {
background: red;
}
.row .section-3-container {
background: orange;
padding-top: 15px;
padding-bottom: 15px;
padding-left: 0px;
padding-right: 0px;
}
section.two div {
background: red;
}
header .row>div,
section>div {
text-align: center;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<header>
<div class="container">
<div class="row">
<div class="col-sm-12 section-1-container">
1
</div>
</div>
</div>
</header>
<div class="custom-container">
<div class="container">
<div class="row">
<div class="col-sm-4 section-2-container">
<section class="one">
<div>
2
</div>
</section>
</div>
<div class="col-sm-8 section-3-container">
<section class="two">
<div>
3
</div>
</section>
</div>
</div>
I have a page that is 50/50 wide. The left half has a row with six divs.
Criteria:
6 squares must always remain square.
First 5 squares should have margin/padding to right for separation.
All six squares must stay on same single row. If I can get that to work i can make the needed adjustments for responsiveness in smaller viewports.
Cross browser compatible for newest version of ie, chrome, and firefox.
My codepen: https://codepen.io/johnsontroye/pen/zzNVBr
Image:
<body>
<div class="container">
<div class="column" style="margin-right: 20px">
<div class="flex-container">
<div class="flex-item">
<div class="flex-item-inner">
<div class="flex-item-inner-content">
L1
</div>
</div>
</div>
<div class="flex-item">
<div class="flex-item-inner">
<div class="flex-item-inner-content">
L2
</div>
</div>
</div>
<div class="flex-item">
<div class="flex-item-inner">
<div class="flex-item-inner-content">
L3
</div>
</div>
</div>
<div class="flex-item">
<div class="flex-item-inner">
<div class="flex-item-inner-content">
L4
</div>
</div>
</div>
<div class="flex-item">
<div class="flex-item-inner">
<div class="flex-item-inner-content">
L5
</div>
</div>
</div>
<div class="flex-item">
<div class="flex-item-inner">
<div class="flex-item-inner-content">
L6
</div>
</div>
</div>
</div>
</div>
<div class="column" style="margin-left: 20px; border: 1px black solid; height: 500px">
Other stuff
<div>
</body>
.container {
display: flex;
flex-direction: row;
padding: 25px;
border: 2px red solid;
}
.column {
width: 100%;
height: 100%;
float: left;
}
.flex-container {
padding: 0;
font-size: 0;
border: 1px solid black;
box-sizing: border-box;
}
.flex-item {
position: relative;
display: inline-block;
height: 0;
width: 100%;
padding-top: 100%;
border: 1px black solid;
font-size: 20px;
color: black;
font-weight: bold;
text-align: center;
box-sizing: border-box;
}
#media (min-width: 480px) {
.flex-item {
width: 33.3333%;
padding-top: 33.3333%;
}
}
#media (min-width: 768px) {
.flex-item {
width: 16.6666%;
padding-top: 16.6666%;
}
}
.flex-item-inner {
position: absolute;
display: flex;
justify-content: center;
align-items: center;
top: 0;
bottom: 0;
right: 0;
left: 0;
margin-right: 25px;
background: white;
border: 1px solid red;
box-sizing: border-box;
}
.flex-item-inner-content {
border: 1px solid orange;
}
.flex-item:last-child .flex-item-inner {
margin-right: 0;
color: green;
}
The main trick here is to make the div a square.
Normally one set a width, the height to 0 and a padding that equals to the width
.square {
height: 0;
width: 33%;
padding-bottom: 33%;
background: lightgray;
}
<div class="square">
<div>
Content
</div>
</div>
Now, when we add display: flex, we can't use padding with percent (Firefox bug) and we can't use height with percent since we used height: 0.
To overcome these issues when can use viewport units vw instead, and with that we can also use height instead of padding to keep it squared.
So instead of setting a width like this, calc((100% / 6) - 10px);, to spread 6 items equally with a gutter about 10px wide, we use viewport units like this calc(( (50vw - 65px) / 6) - 10px);
The 50vw is half the browser width, the 65px is the sum of the container's left/right padding, 50px, plus the 15px gutter between the columns.
This also allows us to skip the extra flex-item-inner element, skip using position: absolute on the content element, and, as we didn't use percent for the height on the flex-item, we can do like this to center the content
.flex-item-content {
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
And the end result is this
Fiddle demo
Stack snippet
.container {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
padding: 25px;
border: 2px red solid;
}
.column {
flex-basis: calc(50% - 15px);
}
.flex-container {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.flex-item {
position: relative;
flex-basis: calc(( (50vw - 65px) / 6) - 10px);
height: calc(( (50vw - 65px) / 6) - 10px);
background: white;
border: 1px solid red;
overflow: hidden;
}
.flex-item-content {
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.flex-item:last-child .flex-item-content {
color: green;
}
.column .other {
padding: 15px;
border: 1px solid black;
padding-bottom: 35px;
}
.column.left .other {
margin-top: 10px;
}
.column.right .other:nth-child(n+2) {
margin-top: 10px;
}
#media (max-width: 768px) {
.flex-item {
flex-basis: calc(( (50vw - 65px) / 3) - 10px);
height: calc(( (50vw - 65px) / 3) - 10px);
}
.flex-item:nth-child(n+4) {
margin-top: 12px;
}
}
#media (max-width: 480px) {
.flex-item {
flex-basis: calc(( (50vw - 65px) / 2) - 10px);
height: calc(( (50vw - 65px) / 2) - 10px);
}
.flex-item:nth-child(n+3) {
margin-top: 15px;
}
}
<div class="container">
<div class="column left">
<div class="flex-container">
<div class="flex-item">
<div class="flex-item-content">
L1
</div>
</div>
<div class="flex-item">
<div class="flex-item-content">
L2
</div>
</div>
<div class="flex-item">
<div class="flex-item-content">
L3
</div>
</div>
<div class="flex-item">
<div class="flex-item-content">
L4
</div>
</div>
<div class="flex-item">
<div class="flex-item-content">
L5<br>L5
</div>
</div>
<div class="flex-item">
<div class="flex-item-content">
L6
</div>
</div>
</div>
<div class="other">
Other stuff - left
</div>
</div>
<div class="column right">
<div class="other">
Other stuff - right
</div>
<div class="other">
Other stuff - right
</div>
</div>
</div>
This is doable with some fairly simple code, as long as the parent column's width is consistent at 50%-ish, and that the space between squares don't have to be strictly equal to a certain value. The vw (viewport width percentage) unit allows for a consistent size to be applied to both width and height of an element.
Here is an example that I boiled down to the fewest elements, and some notes help to move it in to your codebase.
Experiment with .flex-item's height and flex-basis (third value of flex) to get a size you like.
No padding or margin values are needed because justify-content: space-between; helpfully calculates that for us.
Using a line-height equal to the height of .flex-item would allow for an inner element with display: inline-block; and vertical-align: middle; to be centred.
.column {
width: 48vw;
height: 48vw;
padding: 1vw;
border: 1px solid #ccc;
}
.flex-container {
display: flex;
flex-flow: row;
justify-content: space-between;
}
.flex-item {
height: 6vw;
line-height: 6vw;
text-align: center;
border: 1px solid #ccc;
flex: 0 0 6vw;
}
<div class="column">
<div class="flex-container">
<div class="flex-item">
L1
</div>
<div class="flex-item">
L2
</div>
<div class="flex-item">
L3
</div>
<div class="flex-item">
L4
</div>
<div class="flex-item">
L5
</div>
<div class="flex-item">
L6
</div>
</div>
</div>
Only in the latest browsers? CSS Grid to the rescue! It's got great support in the latest versions. You may need some vendor prefixes still; check on CanIUse for the details.
Here it is as a fork: https://codepen.io/jackmakesthings/pen/MoJNNV
.container {
display: flex;
flex-direction: row;
padding: 25px;
border: 2px red solid;
}
.column {
width: 100%;
height: 100%;
float: left;
}
.grid-row {
display: grid;
grid-gap: 10px; /* set this to whatever space you need between boxes */
grid-template-columns: repeat(6, 1fr); /* grid autosizes 6 columns */
}
.row-item {
grid-column: 1 / 7; /* to span the whole row */
border: 1px solid;
padding: 10px;
}
.grid-item {
position: relative;
border: 1px solid;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
}
/* This is a nifty trick for getting those fixed aspect ratio boxes. */
.grid-item:before {
content: '';
float: left;
width: 0;
height: 0;
padding-bottom: 100%;
}
.grid-item:after {
display: table;
clear: both;
}
/* Responsive grid changes? Sure! */
#media (max-width: 1000px) {
/* We just have to change the grid template: */
.grid-row {
grid-template-columns: repeat(3, 1fr);
}
/* Unexpected thing I ran into - you also have to change this, or the grid stays big enough to accommodate the old 6-column-sized row-item. Makes sense, but vexed me for a minute! */
.row-item {
grid-column: 1 / 4;
}
}
<div class="container">
<div class="column" style="margin-right: 20px">
<div class="grid-row">
<div class="grid-item">L1</div>
<div class="grid-item">L2</div>
<div class="grid-item">L3</div>
<div class="grid-item">L4</div>
<div class="grid-item">L5</div>
<div class="grid-item">L6</div>
<div class="row-item">some other thing</div>
<div class="row-item">and another</div>
</div>
</div>
<div class="column" style="margin-left: 20px; border: 1px black solid; height: 500px">
Other stuff
<div>
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>
If you are willing to use absolute positioning and CSS3, there is a way.
HTML:
<div class="grid">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
CSS:
.grid{
position: absolute;
width: auto;
left: 50%;
-ms-transform: translateX(-50%); /* IE 9 */
-webkit-transform: translateX(-50%); /* Safari */
transform: translateX(-50%);
}
.item{
width: 50px;
height: 50px;
background: #fff;
float: left;
margin: 20px;
}
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:
JSFiddle example
CodePen example
$(document).ready(function() {
function fillGrid() {
var $gridContainer = $('.grid-container');
var htmlGridItem = $('#tmpl_grid_item').html();
$gridContainer.empty();
for (var i = 0; i < 20; i++) {
var $gridItem = $(htmlGridItem);
$gridContainer.append($gridItem);
}
};
fillGrid();
});
.container {
background-color: #2B3643;
}
.container * {
box-sizing: content-box;
}
.grid-container {
padding-top: 30px;
padding-bottom: 0px;
display: flex;
flex-wrap: wrap;
margin: 0px auto;
}
.grid-item-wrapper {
width: 200px;
margin-bottom: 15px;
}
.grid-item {
margin-bottom: 15px;
}
.grid-item > .item-desc {
padding: 8px;
background-color: #333333;
color: #CFCFCF;
}
body {
padding-top: 60px;
}
#media (min-width: 762px) and (max-width: 991px) {
.grid-item-wrapper:nth-child(3n+1) {
margin-left: 15px;
}
}
#media (min-width: 992px) {
.grid-item-wrapper:nth-child(4n+1) {
margin-left: 9px;
}
}
#media (min-width: 1200px) {
.grid-item-wrapper:nth-child(4n+1) {
margin-left: 109px;
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<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>