Scale right column in grid to 100% of left column - html

I'm using foundations grid. I've got a grid with two columns and would like to have the content of the right column scale to 100% the height of the left column.
I made a JSBin to illustrate my problem (you have to open the output in a seperate tab for some reasons).
That is how it looks:
div {
border: 1px solid #CCC;
}
<link href="//cdnjs.cloudflare.com/ajax/libs/foundation/5.0.3/css/normalize.min.css" rel="stylesheet"/>
<link href="//cdnjs.cloudflare.com/ajax/libs/foundation/5.0.3/css/foundation.min.css" rel="stylesheet"/>
<script src="//cdnjs.cloudflare.com/ajax/libs/foundation/5.0.3/js/vendor/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/foundation/5.0.3/js/foundation.min.js"></script>
<div class="row" style="width:50%">
<div class="small-8 columns">
<div class="row">
left column first row
</div>
<div class="row">
left column second row
</div>
<div class="row">
left column third row
</div>
<div class="row">
left column fourth row
</div>
<div class="row">
left column fifth row
</div>
<div class="row">
left column sixth row
</div>
<div class="row">
left column seventh row
</div>
<div class="row">
left column eigth row
</div>
<div class="row">
left column ninth row
</div>
</div>
<div class="small-4 columns">
<div class="row">
this is supposed to scale to 100% of the size of the left column
</div>
</div>
</div>

Try like this: Demo
CSS:
div {
border: 1px solid #CCC;
}
#row_box{
display:table;
}
.child{
display:table-cell;
background-color:red;
}
.child:nth-child(2)
{
background-color:yellow;
}
HTML:
<div class="row" style="width:50%">
<div id="row_box"> // added id row_box
<div class="large-8 child"> // added class child & removed columns
<div class="row">
left column first row
</div>
<div class="row">
left column second row
</div>
<div class="row">
left column third row
</div>
<div class="row">
left column fourth row
</div>
</div>
<div class="large-4 child"> // added class child & removed columns
<div class="row">
this is supposed to scale to 100% of the size of the left column
</div>
</div>
</div>
</div>
Note:
This -ve margin is coming from your css file.

Related

Bootstrap columns stacking when they shouldn't be

I'm trying to learn bootstraps grid system. From what I understand about the grid system, the code below should produce 2 columns. One column with 3 rows on the left, and one column with 1 row on the right and they should never stack. Unfortunately, this is not the case and rather than never stacking, they are always stacked and no matter what I try, I can't get the result I want. Does anyone see any issues with this code or is it something deeper that I need to look into?
I thought it might be something to do with my display/viewport size, but http://viewportsizes.com/mine/ says my viewport size is 1707 x 961, which from what I understand should not be small enough to force the columns to stack.
<head>
<meta name="viewport" content="width=device-width" />
<title>Overhead</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="col-6">
<div class="row">
Top Left
</div>
<div class="row">
Middle Left
</div>
<div class="row">
Bottom Left
</div>
</div>
<div class="col-6">
Right
</div>
</div>
</body>
You should first define rows before columns. For example the code below will create a row and 2 equal columns.
<body>
<div class="container">
<div class="row">
<div class="col-md-6">
</div>
<div class="col-md-6">
</div>
</div>
</body>
here is the bootply fiddle link
https://www.bootply.com/WtWRhCVl8e
Columns go into rows, you have it the other way around. For instance:
<div class="container">
<div class="row">
<div class="col-md-6">
left column
</div>
<div class="col-md-6">
right column
</div>
</div>
<div class="row">
<div class="col-md-4">
left column
</div>
<div class="col-md-4">
middle column
</div>
<div class="col-md-4">
right column
</div>
</div>
</div>
This will create two rows, the top row with two columns, each 50% of the row, and the bottom row with three columns, each 33.33% of the row.
I hope this helps.
A row has a negative margin of -15px on both side of the element. A column has a positive padding of 15px on both sides to match the negative margins created by the row. So, when you create a row, it should be immediately followed by a column.
I think you are looking for the one below.
<div class="container">
<div class="row">
<div class="col-xs-6">
<div class="row">
<div class="col-xs-12">
Row 1 on Col 1
</div>
</div>
<div class="row">
<div class="col-xs-12">
Row 2 on Col 2
</div>
</div>
<div class="row">
<div class="col-xs-12">
Row 3 on Col 3
</div>
</div>
</div>
<div class="col-xs-6">
<div class="row">
<div class="col-xs-12">
Row 1 on Col 2
</div>
</div>
</div>
</div>
</div>
Take a look at this pen that I have created for you.
https://codepen.io/vijayrkumar/pen/JpZROy
<div class="row">
<div class="col-md-6">
Left column
</div>
<div class="col-md-6">
Middle Column
</div>
<div class="col-md-6">
Right Column
</div>
</div>

How to stack Bootstrap row inside of another row?

I have a Bootstrap page on which I'm trying to stack different boxes.
Imgur - Image of boxes (sorry, not enough rep to upload images directly)
The green boxes are the ones currently in position, and the red ones are the ones I am having issues with. I'm using the following code (simplified) to get the green boxes:-
<div class="container">
<div class="row">
<div class="col-xs-3" style="height:100px; background-color:green;">
</div>
<div class="col-xs-3" style="height:100px; background-color:green;">
</div>
<div class="col-xs-6" style="height:200px; background-color:green;">
</div>
</div>
</div>
I'm basically trying to create another two <div class="col-xs-3" style="height:100px; background-color:green;"> that will go underneath the current two, while also keeping the large box to the right.
I thought it would be an easy fix with a new row, or using float:left / right, but none of that seems to be working out.
One way to do it would be to make two 50% width columns and then divide the column on the right into two rows with two more columns in each:
.green-lrg {
background-color:green;
height:215px;
}
.green-sml {
background-color:green;
height:100px;
}
.red-sml {
background-color:red;
height:100px;
}
.col-xs-6 .row {
padding-top:15px;
}
.col-xs-6 .row:first-child {
padding-top:0;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<!-- LEFT COLUMN -->
<div class="col-xs-6">
<div class="green-lrg"></div>
</div>
<!-- RIGHT COLUMN -->
<div class="col-xs-6">
<div class="row">
<div class="col-xs-6">
<div class="green-sml"></div>
</div>
<div class="col-xs-6">
<div class="green-sml"></div>
</div>
</div>
<div class="row">
<div class="col-xs-6">
<div class="red-sml"></div>
</div>
<div class="col-xs-6">
<div class="red-sml"></div>
</div>
</div>
</div>
</div>
</div>
Here is simple example.
HTML
<div class="container">
<div class="row">
<div class="col-xs-6 box box-big">
</div>
<div class="col-xs-6">
<div class="row">
<div class="col-xs-6 box box-small">
</div>
<div class="col-xs-6 box box-small">
</div>
<div class="col-xs-6 box box-small">
</div>
<div class="col-xs-6 box box-small">
</div>
</div>
</div>
</div>
</div>
and CSS:
.box {
background-color:green;
border:5px solid white;
}
.box-big {
height:200px;
}
.box-small {
height:100px;
}

How do I access certain column in bootstrap grid system?

In the bootstrap grid system, there are 12 columns and col - *- * class is used to group together certain number of columns. But when I want to use the first 3 columns and then just the last column how do I do that, that is, how can I use certain columns and not others in a single row class?
Like when I make a page header, I give the title on the left hand side and certain other text on the right side of the header, I assume I can use the grid system here effectively, given that I can access certain columns.
Use the .offset-* class (.col-md-offset-* class for versions older than 4.0.0). For instance, occupy first 4 cols, and only the last 2 cols as follows:
<div class="row">
<div class="col-md-4">.col-md-4</div>
<div class="col-md-2 offset-md-6">.col-md-6 .offset-md-2</div>
</div>
Bootstrap v4.0.0-alpha.6
.b { background: #CCC; height: 80px; }
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<div class="container">
<div class="row">
<div class="b col-sm-4"></div>
<div class="b offset-sm-6 col-sm-2"></div>
</div>
</div>
Bootstrap v3.3.7
.b { background: #CCC; height: 80px; }
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="b col-xs-4"></div>
<div class="b col-xs-offset-6 col-xs-2"></div>
</div>
</div>
First create 3 columns, then 8 columns, after that create the last column and you write the code in only first three and last one like this. think of it like a graph sheet.
<div class="col-md-3">
<!-- things you want in first three columns: code here-->
</div>
<div class="col-md-8">
<!-- leave this blank-->
</div>
<div class="col-md-1">
<!-- things you want in last column : code here-->
</div>
Use with col-md-offset-*.
like this
<div class="row">
<div class="col-md-4">.col-md-4</div>
<div class="col-md-4 col-md-offset-4">.col-md-4 .col-md-offset-4</div>
</div>
<div class="row">
<div class="col-md-3 col-md-offset-3">.col-md-3 .col-md-offset-3</div>
<div class="col-md-3 col-md-offset-3">.col-md-3 .col-md-offset-3</div>
</div>
<div class="row">
<div class="col-md-6 col-md-offset-3">.col-md-6 .col-md-offset-3</div>
</div>
If you want three column next to each other and only use the last one you can simply just put in content in the last one like this:
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4"></div>
<div class="col-md-4">Content here</div>
</div>
If I understand your question correctly, you want to be able to single out specific columns in your row of 12.
<div class='row'>
<div id='first' class='col-sm-4'>
<p>Column 1</p>
<div>
<div id='second' class='col-sm-4'>
<p>Column 2</p>
<div>
<div id='third'class='col-sm-4'>
<p>Column 3</p>
<div>
</div>
If you want to select specific columns, you could give them an id to uniquely identify them. If you would want to select the first and third column and change their background color to lightblue you could do this:
#first, #third{
background-color: lightblue;
}
You will have to use the .col-[xs|md|lg|xl]-offset-* classes in bootstrap, see here
For example, if you want the layout of your page to have only the first 3 columns and the last column containing elements just do:
<div class="row">
<div class="col-md-3">
<span>some text</span>
</div>
<div class="col-md-offset-8 col-md-1">
<span>some more text</span>
</div>
</div>
<div>
<div class="col-md-3" style="text-align:left">
<h1>Title</h1>
</div>
<div class="col-md-3"></div>
<div class="col-md-3"></div>
<div class="col-md-3" style="text-align:right">
<p>Description</p>
</div>

small gap between two divs

I am using Bootstrap 3 and I am having issues to make small gap between two divs. I have tried offset but still its not working here is my code
.box1 {
background-color:black;
width:150px;
height:150px;
}
<div class="container">
<div class="row">
<div class="col-md-1 col-md-offset-0">
<div class="box1">
</div>
</div>
<div class="col-md-1 col-md-offset-1">
<div class="box1">
</div>
</div>
</div>
</div>
The problem is if set second div col-md-offset-0 it gets too close first div and if I set to 1 or above it sets very big gap. I want very small gap
Edited:
Try adding margin with a custom class-name on the col divs.
like so
<div class="container">
<div class="row">
<!-- custom-spacer is a custom class-name. to add extra margin. -->
<div class="col-md-1 col-md-offset-0 custom-spacer">
<div class="box1">
</div>
</div>
<div class="col-md-1 col-md-offset-1">
</div>
</div>
</div>
<style>
.box1 {
background-color:black;
width:150px;
height:150px;
}
.custom-spacer {
margin-right: 40px; /* Increase or decrease the margin as required. */
}
</style>
There could be one problem with this approach, because you are increasing the gutter size, you will not be able to create 12 cols within the same row.

Bootstrap fluid row columns with different height

Dynamically I create a set of boxes with slightly different height and want to appear 2, 3 or 4 of them (depending on the screen size) at the same row. I tried the following markup with bootstrap:
<div class="container-fluid">
<div class="row-fluid">
<div class="col-xs-6">
Two<br>Lines
</div>
<div class="col-xs-6">
Two<br>Lines
</div>
<div class="col-xs-6" style="background-color: red">
Three<br>Lines<br>Jup
</div>
<div class="col-xs-6">
Two<br>Lines
</div>
<div class="col-xs-6">
Two<br>Lines
</div>
<div class="col-xs-6">
Two<br>Lines
</div>
</div>
</div>
My problem is the space below my third column.
A B
C D
C E <- Should wrap to next row, because of C
F G
Can you tell how to achieve this? I recognized the advice to use a clearfix but I guess this attempt will cause problems and ugly code when using a different count of columns.
Thanks for your answers.
The problem
The problem is that all bootstrap columns try to float left.
From MDN on floats:
when an element is floated it is taken out of the normal flow of the document. It is shifted to the left or right until it touches the edge of its containing box or another floated element.
So when you have uneven heighted elements, the correct behavior is to stack them to the side.
Luckily, there are a lot of way to correct this to the anticipated behavior:
Using CSS Clear property
From MDN on Clear:
The clear CSS property specifies whether an element can be next to floating elements that precede it or must be moved down (cleared) below them
For this exact structure, you can apply clear to every other row using the nth-child selector:
.col-xs-6:nth-child(odd) {
clear: both;
}
Note: The nth-child selector won't work in IE8
Demo in jsFiddle / Stack Snippets:
.col-xs-6:nth-child(odd) {
clear: left;
}
.col-xs-6 {
border: 1px solid grey;
}
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="col-xs-6 label-warning">
Three<br/>
Lines<br/>
Jump
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
</div>
</div>
Using .clearfix
The Bootstrap way to do this is to use the clearfix class which applies the following styles:
.clearfix:before,
.clearfix:after {
content: " ";
display: table;
}
.clearfix:after {
clear: both;
}
Note: You can use selectively target different screen sizes by combining this with the responsive utility classes (i.e. .visible-*-*, .hidden-*)
Demo in jsFiddle / Stack Snippets:
.col-xs-6 {
border: 1px solid grey;
}
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="clearfix"></div>
<div class="col-xs-6 label-warning">
Three<br/>
Lines<br/>
Jump
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="clearfix"></div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
</div>
</div>
Using .row
You can also just wrap every pair of columns into a new row. This is the least reusable, but if every set of items forms a logical group, it does create semantic markup.
Demo in jsFiddle / Stack Snippets:
.col-xs-6 {
border: 1px solid grey;
}
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
</div>
<div class="row">
<div class="col-xs-6 label-warning">
Three<br/>
Lines<br/>
Jump
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
</div>
<div class="row">
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
</div>
</div>
Fixing the height
Depending on your use case, you might just benefit from making everything the same height. This would work well if you had similar content in every column and wanted a consistent looking grid.
.col-xs-6 {
height: 7rem;
}
Demo in jsFiddle / Stack Snippets:
.col-xs-6 {
height: 7rem;
}
.col-xs-6 {
border: 1px solid grey;
}
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="col-xs-6 label-warning">
Three<br/>
Lines<br/>
Jump
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
</div>
</div>