According to this link, you can use column wrapping with bootstrap.
Here a part of my code:
<div class="row">
<div class="col-12">
1
</div>
<div class="col-7">
2
</div>
<div class="col-12">
3
</div>
<div class="col-6">
4
</div>
<div class="col-6">
5
</div>
</div>
It works, but is it the best solution? Or is it necessary to create a row each time, which would give:
<div class="row">
<div class="col-12">
1
</div>
</div>
<div class="row">
<div class="col-7">
2
</div>
</div>
<div class="row">
<div class="col-12">
3
</div>
</div>
<div class="row">
<div class="col-6">
4
</div>
<div class="col-6">
5
</div>
</div>
Thanks!
No, you no longer need rows. From the Bootstrap 5.0 documentation:
The .col-* classes can also be used outside a .row to give an element a specific width. Whenever column classes are used as non direct children of a row, the paddings are omitted.
So you can use your first method.
Bootstrap rows are set to wrap by default. So if two columns' width sum up greater than 12, the 2nd one will be wrapped into new line:
<div class="container">
<div class="row">
<div class="col-9">
.col-9
</div>
<div class="col-4">
.col-4
<!-- Since 9 + 4 = 13 > 12, this 4-column-wide div gets wrapped
onto a new line as one contiguous unit. -->
</div>
<div class="col-6">
.col-6
<!-- Subsequent columns continue along the new line. -->
</div>
</div>
</div>
If the default behavior serves you well, you don't have to put new rows each time.
The only time you would need new rows is when your intend to make them new rows:
<div class="container">
<div class="row">
<div class="col-5">1</div>
</div>
<div class="row">
<div class="col-6">2</div>
<div class="col-3">3</div>
</div>
</div>
If I don't put a new row for column #2 and #3, column #2 will be on the same row of column #1, by default.
To me, I prefer the 2nd way because that shows my intention of wanting columns to be in new rows. Others don't have to guess or know Bootstrap default behavior.
Related
I am making a project based on the grid. I am using bootstrap, CSS, and HTML for making the grid. I need to display a grid like this Image. But the row span is not working properly. I am using fully div structure, not the table. My code is:
`
<div>
<div class="row">
<div class="col-md-4">
</div>
</div>
<div class="col-md-4">
</div>
<div rowspan="2" class="col-md-6">
</div>
<div class="col-md-6">
</div>
</div>
<div class="col-md-4">
</div>
</div>
</div>
`
First I would suggest using indenting to make it more readable.
You would just create 3 columns next to each other and in the middle one start a new row.
In the code example I create 1 row and 2 columns that are full width, you could also just create 2 rows.
<div class='container'>
<div class='row'>
<div class='col-4 col1'>1</div>
<div class='col-4'>
<div class='row'>
<div class='col-12 col2'>2</div>
<div class='col-12 col3'>3</div>
</div>
</div>
<div class='col-4 col4'>4</div>
</div>
</div>
https://codepen.io/anon/pen/XOYZRG?editors=1100
Use class="short-div" at col-md-6 in both the div. Remove col-md-6 from both the div, also remove rowspan.
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>
I would like to be able to divide a bootstrap gid even more. Without to much hassle or workarounds. What is the best way of creating column 1/24 of a grid.
Fiddle to understand better what I mean: https://jsfiddle.net/Lmm1ra7d/
<div class='row'>
<div class="col-xs-8">8/12</div>
<div class="col-xs-4">4/12</div>
</div>
<br/>
Now I need to transform this to a 0.5/12 => 1/24 What would be the best approach without a hacky fix. Also not to break the responsiveness.
This is the smallest column bootstrap can make 8.3333%
<br/>
<br/>
<div class='row'>
<div class="col-xs-1">1/12</div>
<div class="col-xs-11">11/12</div>
</div>
<br/>
What I really want would be 4%
<br/>
<br/>
<div class='row'>
<div style="width:3%; display: inline-block;">1/24</div><div style="width:96%;display: inline-block;">23/24</div>
</div>
You can just nest another column setup in the col-*-1. If the padding on the nested row/columns is unwanted you can just add a class to override the padding.
<div class="row">
<div class="col-xs-1">
<div class="row">
<div class="col-xs-6"></div>
<div class="col-xs-6"></div>
</div>
</div>
</div>
you can nest a row in a column, so basically you divide your main row in other rows. For example
<div class="row">
<div class="col-sm-3">
<!-- now you have 1/4 -->
<div class="row">
<div class="col-sm-3">
<!-- now you have 1/4 of 1/4 -->
</div>
</div>
</div>
</div>
I am trying to adapt a layout on Bootstrap that I got from the designer. Two columns in the content has a small spacing, that I couldn't achieve with direct column-spacing (as it gives too much gap). Thus, I tried wrapping everything into a row and adding sub-columns inside my main columns. Here in the code, it's more clear:
<div class="container">
<div class="col-md-12 banner"></div>
<div class="row">
<div class="col-md-8">
<div class="col-md-12 leftSide">
</div>
<div class="col-md-12 leftSide">
</div>
</div>
<div class="col-md-4 rightSide">
<div class="col-md-12">
</div>
</div>
</div>
</div>
Now the spacing seems good but as I wrapped it in a row (I think), the right panel overflowed more then the banner.
It can be seen more clear on the fiddle: http://www.bootply.com/gMBrLvaK5C
The problem is the 'rightSide' panel.
How can I keep that spacing between 'leftSide' and 'rightSide', and fix the overflowing of the right column (because the spacing gets too much if I try achieving spacing with columns)? Or what is the best way to achieve that?
this is my personal solution, instead of add a class on the columns, create a div inside of a column, then you can place a div.banner and div.block like my example:
http://www.bootply.com/PEIiDnp9VD
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="banner"></div>
</div>
</div>
<div class="row">
<div class="col-md-8">
<div class="block"></div>
</div>
<div class="col-md-4">
<div class="block"></div>
</div>
</div>
</div>
if you want less space between the columns you can simply override Bootstrap col-md-* class by adding a class on the columns changing the padding left and right.
First of all, you need to follow proper wrapping of rows and columns. If you want consistency you need to make sure all columns are wrapped into a row.
<div class="row">
<div class="col-md-8 leftSide">
<div class="row">
<div class="col-md-12">
</div>
</div>
<div class="row">
<div class="col-md-12">
</div>
</div>
</div>
<div class="col-md-4 rightSide">
<div class="row">
<div class="col-md-12">
</div>
</div>
</div>
</div>
Then apply custom margins and styling to elements inside of the columns, not columns themselves.
http://www.bootply.com/g6eLHRd1tH
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>