See the following codepen for what I currently have: https://codepen.io/anon/pen/GjWYPO
-
you may select the second div and reset width and margins:
.col-1-3:nth-child(2) {
margin:0 1em;
width:calc(100% / (3/1) - 2em);
}
.container {
font-size: 0;
}
[class|="col"] {
display: inline-block;
vertical-align: top;
position: relative;
font-size: 20px;
}
.col-1-3 {
width: calc(100% / (3/1));
}
/* here update to overwrite width and add margins rules */
.col-1-3:nth-child(2) {
margin: 0 1em;/* whatever , double it before taking it off from width */
width: calc(100% / (3/1) - 2em);
}
/* end update */
.col-2-3 {
width: calc(100%/(3/2));
}
.col-1 {
width: 100%;
}
.bg-blue {
background-color: #42a5f5;
color: #ffffff;
}
.bg-green {
background-color: #66bb6a;
color: #ffffff;
}
<div class="container">
<div class="col-1-3 bg-blue">blue left</div>
<div class="col-1-3" style="font-size:0px;">
<div class="col-1-3 bg-green">green 1</div>
<div class="col-1-3 bg-green">green 2</div>
<div class="col-1-3 bg-green">green 3</div>
</div>
<div class="col-1-3 bg-blue">blue right</div>
</div>
You may also want to take a look at display:flex :
.container{
display:flex;
}
[class|="col"] {
flex:1;/* flex children, spray them evenly */
font-size:20px;
}
.container .container .col-1-3:nth-child(2) {/* select second containere on second level */
margin:0 1em;
}
.bg-blue{
background-color:#42a5f5;
color:#ffffff;
}
.bg-green{
background-color:#66bb6a;
color:#ffffff;
}
<div class="container">
<div class="col-1-3 bg-blue">blue left</div>
<div class="col-1-3 container"><!-- here make it a flex container too -->
<div class="col-1-3 bg-green">green 1</div>
<div class="col-1-3 bg-green">green 2</div>
<div class="col-1-3 bg-green">green 3</div>
</div>
<div class="col-1-3 bg-blue">blue right</div>
</div>
Codepen to play with https://codepen.io/anon/pen/xEqQVg
edit
A third option, float + display:table, bg drawn via faux column . This means to put right and left column in front of the middle container.
.container{
background-color:#42a5f5;
margin:1em 0;
text-align:center;
font-size:20px;
line-height:30px;
text-shadow:0 0 1px gray;
}
.bg-blue {
width:33%;/* whatever how many of those ? */
background-color:#42a5f5;
color:#ffffff;
}
.float-l {
float:left;
margin-right:-15px;/* cause of border-spacing */
}
.float-r {
float:right;
margin-left:-15px;/* cause of border-spacing */
}
.child-container {
display:table;
table-layout:fixed;/* suppose to spray cells evenly where no width is set to cells */
background:linear-gradient(to left,#42a5f5 15px, transparent 15px),linear-gradient(to right,#42a5f5 15px, transparent 15px) white;/* reproduce blue bg part on white for both sides */
width:calc(34% + 30px);
margin:auto;
border-spacing:15px 0;
}
.bg-green{
display:table-cell;
background-color:#66bb6a;
color:#ffffff;
}
<div class="container">
<div class="float-l bg-blue">blue left</div>
<div class="float-r bg-blue">blue right</div>
<div class="child-container">
<div class=" bg-green">green 1</div>
</div>
</div>
<div class="container">
<div class="float-l bg-blue">blue left</div>
<div class="float-r bg-blue">blue right</div>
<div class="child-container">
<div class=" bg-green">green 1</div>
<div class=" bg-green">green 2</div>
</div>
</div>
<div class="container">
<div class="float-l bg-blue">blue left</div>
<div class="float-r bg-blue">blue right</div>
<div class="child-container">
<div class=" bg-green">green 1</div>
<div class=" bg-green">green 2</div>
<div class=" bg-green">green 3</div>
</div>
</div>
<div class="container">
<div class="float-l bg-blue">blue left</div>
<div class="float-r bg-blue">blue right</div>
<div class="child-container">
<div class=" bg-green">green 1</div>
<div class=" bg-green">green 2</div>
<div class=" bg-green">green 3</div>
<div class=" bg-green">green 4</div>
</div>
</div>
codepen https://codepen.io/anon/pen/qaAvjX
讨论(0)
-
I think adding this to your css, would do the job for you.
.col-1-3.bg-green{
width:calc(100%/(3/1) - (3-1)*2px);
margin:0px 2px;
}
讨论(0)
-
.container{
display:flex;
}
.container > div{
flex-grow:1
}
[class|="col"] {
display:inline-block;
vertical-align: top;
position:relative;
font-size:20px;
}
.col-1-3{
}
.col-2-3{
}
.col-1{
}
.bg-blue{
background-color:#42a5f5;
color:#ffffff;
}
.bg-green{
background-color:#66bb6a;
color:#ffffff;
}
.bg-green+ .bg-green{ margin-left:20px}
<div class="container">
<div class="bg-blue">blue left</div>
<div class=" bg-green">green 1</div>
<div class=" bg-green">green 2</div>
<div class=" bg-green">green 3</div>
<div class=" bg-blue">blue right</div>
</div>
Something like this?
This is using flexbox layout. If you want more browser compatibility then I think a good option would be use any css framework.
讨论(0)
-
You can add this to your CSS and it should work (tested):
.bg-green {
width: calc((100% - ((3 - 1)*2%))/(3/1));
margin-right: 2%;
}
.bg-green:last-child{
margin-right: 0;
}
Edited snippet:
.container{
font-size: 0;
}
[class|="col"] {
display:inline-block;
vertical-align: top;
position:relative;
font-size:20px;
}
.col-1-3{
width:calc(100%/(3/1));
}
.col-2-3{
width:calc(100%/(3/2));
}
.col-1{
width:100%;
}
.bg-blue{
background-color:#42a5f5;
color:#ffffff;
}
.bg-green{
background-color:#66bb6a;
color:#ffffff;
}
.bg-green {
width: calc((100% - ((3 - 1)*2%))/(3/1));
margin-right: 2%;
}
.bg-green:last-child{
margin-right: 0;
}
<div class="container">
<div class="col-1-3 bg-blue">blue left</div>
<div class="col-1-3" style="font-size:0px;">
<div class="col-1-3 bg-green">green 1</div>
<div class="col-1-3 bg-green">green 2</div>
<div class="col-1-3 bg-green">green 3</div>
</div>
<div class="col-1-3 bg-blue">blue right</div>
</div>
Just replace "3" with whatever numbers of columns you have and change the margin-right value as you like.
Version with negative margin
Change markup a little bit (similar structure to Bootstrap):
<div class="col-1-3">
<div class="bg-green">green 1</div>
</div>
<div class="col-1-3">
<div class="bg-green">green 2</div>
</div>
<div class="col-1-3">
<div class="bg-green">green 3</div>
</div>
And following part in CSS
.children-has-gutters > div {
padding-left: 15px;
padding-right: 15px;
box-sizing: border-box;
}
.bg-blue {
background-color:#42a5f5;
color:#ffffff;
width:calc((100%/(3/1)) + 15px);
}
.container{
font-size: 0;
}
[class|="col"] {
display:inline-block;
vertical-align: top;
position:relative;
font-size:20px;
}
.col-1-3{
width:calc(100%/(3/1));
}
.col-2-3{
width:calc(100%/(3/2));
}
.col-1{
width:100%;
}
.children-has-gutters{
margin-left:-15px;
margin-right:-15px;
width: calc((100% / (3/1)) + 30px);
}
.children-has-gutters > div{
padding-left:15px;
padding-right:15px;
box-sizing: border-box;
}
.bg-blue{
background-color:#42a5f5;
color:#ffffff;
}
.bg-green{
background-color:#66bb6a;
color:#ffffff;
}
<div class="container">
<div class="col-1-3 bg-blue">blue left</div>
<div class="col-1-3 children-has-gutters" style="font-size:0px;">
<div class="col-1-3">
<div class="bg-green">green 1</div>
</div>
<div class="col-1-3">
<div class="bg-green">green 2</div>
</div>
<div class="col-1-3">
<div class="bg-green">green 3</div>
</div>
</div>
<div class="col-1-3 bg-blue">blue right</div>
</div>
Note that you had to add 15px to each blue column, to avoid issue with container width. You can play around and check what happens without it - width of elements in container would be 100% - 30px (negative margin).
讨论(0)