<div>
<div class="row">
<div class="col-xs-12">
Is it true that 'row' and 'col-xs-12' will cancel each other?
If above answer is yes, why I can see we use row and col class in Bootstrap official website (https://v4-alpha.getbootstrap.com/layout/grid/)?
If above answer is yes, Why?
Any reference document I can read about?
Updates: 'Cancel' means they have opposite effect.
In a sense, yes, they cancel each other out, meaning that the padding added by the parent of a .row will be eliminated with the negative margins of .row.
Let's take a look at even one of the examples from the Bootstrap 4 documentation, with some additional CSS:
/* Warning! It is generally a bad idea to style the Bootstrap elements such as .col and .row, but this is done only for the purpose of demo. */
.col {
background-color: lightgray;
border: 1px solid darkgray;
}
.row {
background-color: lightblue;
}
.container {
background-color: lightgreen;
padding-left: 30px !important;
padding-right: 30px !important;
}
<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="col">
1 of 2
</div>
<div class="col">
1 of 2
</div>
</div>
<div class="row">
<div class="col">
1 of 3
</div>
<div class="col">
1 of 3
</div>
<div class="col">
1 of 3
</div>
</div>
</div>
Let's consider why the effect of cancelling happens: By default, Bootstrap 4 is styling the container and column classes with a padding of 15px on each side (X axis), and the row class with a negative margin of 15px. The effect of negative margins is the opposite of padding, adding to the size of element instead of shrinking it.
Therefore, the effect of having 15px side-spacing from .container, minus 15px from .row, and finally 15px from the columns, will result in a neat way of spacing the elements centrally on the page. This effect cannot be seen in the first example, as the background styling is applied directly to the column. For observing this effect, we can add markup into the columns so that the column spacing can be observed:
/* Warning! It is generally a bad idea to style the Bootstrap elements such as .col and .row, but this is done only for the purpose of demo. */
.col {
background-color: lightgray;
border: 1px solid darkgray;
}
<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="col">
<div class="bg-info px-2">
<span>Gray color is</span>
</div>
</div>
<div class="col">
<div class="bg-warning px-2">
<span>the actual spacing.</span>
</div>
</div>
</div>
</div>
Without having that spacing on the columns will make the elements inside the columns stick to the edges of the page, which is not generally desirable. By omitting the negative margin on the .row, on the other hand, will potentially result into excessive spacing for the columns, so less content will fit in – this is also why it is very important to have proper markup for the Bootstrap Grid System, as otherwise its use will result in undesirable results.
Useful links for learning more:
Bootstrap 4 Grid System (the one you have even mentioned)
The Definitive Guide to Using Negative Margins (Smashing Magazine)
Padding (CSS-Tricks) – also look into box-sizing rule
So I have no idea in what sense do you mean "cancel" out. The only thing that cancels out is the row negative margin on the sides with the padding of the columns. This allows the spacing between the columns without having it on the outer sides.
To get a sense what the grid system provides there is a nice article about it even though it's a little bit old:
https://webdesign.tutsplus.com/articles/all-about-grid-systems--webdesign-14471
There are many other. Also on bootstrap.
No, it's not true, the col class is used inside a row class.
The row class defines the space that will be used together but the col class defines the spatial arrangement in that row.
Related
one take col-md-4, second col-md-8, but the second with a picture is not 100% width, there are gaps on the left and right sides, could anyone please advise how to remove gaps and make image full size ? Thanks. Here is screenshot
.upperDiv{
height: 100px;
width: 100%;
background: red;
margin-top: 20px;
margin-bottom: 20px;
}
.fixed-content {
width: 100%;
height: 100px;
object-fit: cover;
}
<div class="container">
<div class="row upperDiv">
<div class="col-md-4" style="background: #005AA1;">
</div>
<div class="col-md-8">
<img src="assets/libled.jpg" class="fixed-content">
</div>
</div>
</div>
Bootstrap put that padding for you to better align your content, you can remove it by inserting p-0 (padding = 0px) class name as I remember
<div class="container">
<div class="row upperDiv">
<div class="col-md-4 p-0" style="background: #005AA1;">
</div>
<div class="col-md-8 p-0">
<img src="assets/libled.jpg" class="fixed-content">
</div>
</div>
</div>
Looking at your HTML, you are using bootstrap's grid system (hinted by the col-md-X classes). The gap you see in your example is caused by the padding applied to the cells of the grid system to create the gutter.
You have two possibilities:
You put the picture as a background instead, since padding is part of the element, the picture will cover this space too.
You remove the gutter.
1 is pretty self explanatory so I'll go straight to two. You can read about the .no-gutter helper class. It needs to be applied to a row and will effectively remove all gutters for the columns in it. But that means you'll loose the gutter on your left column too. You could also remove the padding with a custom class that sets padding-left:0 !important;padding-right:0 !important; This will effectively remove the gutter for the specified column element.
Whatever the option you choose, remember that cols are not meant to be used directly for the styling. They are here to help you create columns in which to put your visual elements. Although I pointed 3 different approaches to your problem, the only "pure" solution is to use the .no-gutter. Others might have weird visual impacts such as making the gutter effectively only half wide (since the left col participates in half the gutter too) and will not look right if there are other columns near it.
I'm having troubles understanding bootstrap's grid system, based on Murach's .Net book explanation.
Please read the whole post, I know how to fix this (<div class="col-lg-12">Col x</div>, does the trick), I'm just wondering why bootstrap works this way.
I did copy an example from a book, where Column a and Column b should use the 12 columns that bootstraps provides to work with its grid system, or that's what I got from the book, at least. Also, this very same code
<main class="container">
<div class="row">
<div>Column a</div>
<div>Column b</div>
</div>
<div class="row">
<div class="col-md-4">Column c</div>
<div class="col-md-8">Column d</div>
</div>
<div class="row">
<div class="col-md-4 col-sm-6">Column e</div>
<div class="col-md-8 col-sm-6">Column f</div>
</div>
</main>
outputs those divs occupying the 12 columns in the book's screenshot (First Column 1 and Column 2 "group" are Column a and Column b in my code).
But this is what I get from it
unless of course I apply the trick I said earlier in the post.
This is what I specified in the head section
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="Content/bootstrap.min.css" rel="stylesheet" />
<script src="Scripts/jquery-3.0.0.min.js"></script>
<script src="Scripts/bootstrap/min.js"></script>
<link href="style.css" rel="stylesheet" />
This is style.css:
.container
{
padding-top:1em;
}
.row
{
margin-bottom: 2em ;
}
.row div
{
border: 1px solid black ;
padding: 0.5em ;
background-color:lightgrey ;
}
The behavior of the DIV's is based on flexbox. The Bootstrap 4 row class is display:flex, so when you have a simple DIV inside it simply follows default flexbox behavior. This isn't 1 column unit, it simply takes the width of the content since neither grow or shrink is specified.
The Bootstrap 4 col-* classes have specific properties design to work inside the parent row.. such as flex-basis, max-width, padding, etc...
https://www.codeply.com/go/qvHVlTJshv
Read more in the Bootstrap docs...
"Rows are wrappers for columns. Each column has horizontal padding
(called a gutter) for controlling the space between them. This padding
is then counteracted on the rows with negative margins... In a grid
layout, content must be placed within columns and only columns may be
immediate children of rows"
I know this is a silly question, but I can't seem to find an answer on the net for this. For bootstrap, I know you use rows and cols to specify the size of the row. But if I have something like this:
<div class='row'>
<div class='col-md-12'></div>
</div>
Is there any point in adding that col-md-12? I would of thought just sticking the row class is enough if you wanted the full length of the row??
Any advice on this will be greatly appreciated.
Thanks
Yes, you probaly want the col. Inspect it with your browser's developer tools. You'll see margin /padding and other styling gets applied to .col-* to get things to line up properly.
The .row class primarily provides the "float clearing" that columns provide.
Columns as you know allow for a variety of "grid layout" widths / sizes, and automatically adjust to responsive browser size needs.
Run the snippet below to see the difference (click the "Full Page" link, otherwise it's compressed into a small view). (Note that I've added borders to rows / cols to highlight what's going on):
.row {
border: 1px solid red;
}
[class^="col-"] {
border: 1px solid black;
}
.other {
border: 1px solid blue;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="col-sm-12">
This is a row with a full-width column.
</div>
</div>
<div class="row">
<div class="col-sm-4">
This row has a few smaller column.
</div>
<div class="col-sm-4">
This row has a few smaller column.
</div>
<div class="col-sm-4">
This row has a few smaller column.
</div>
</div>
<div class="row">
This row has no columns.
</div>
<div class="other">This has no columns nor rows.</div>
</div>
A .row and a .col-X-X are different things. Yes, you will need the column + a row.
If you use Bootstrap 4, you can just use the .col class whereas in Bootstrap 3 you must use .col-X-12 for full width.
From the bootstrap 3 docs
1) Columns create gutters (gaps between column content) via padding. That padding is offset in rows for the first and last column via negative margin on .rows.
2) The negative margin is why the examples below are outdented. It's so that content within grid columns is lined up with non-grid content.
If you want your row to be 100% width, then there is no need to have a .col div within your .row div. However, having one is useful if you want the default Bootstrap .col styling to be applied to it (such as padding and margin).
I have one col, one row, and there three nested columns. Within each column is a div button. I didn't use the bootstrap default button on purpose, and I have created my own, just that you see one column is being eaten.
Please see the image:
I am almost certain there is nothing wrong with HTML. Main div col is set to col-md-1 to test nested responsive elements within smaller scale. Does anyone understand why content(button) is acting as it would be too big for columns and why columns are being eaten in my case?
here is the code:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div class="col-md-1 col-xs-1">
<div class="row" style="padding:0 !important;margin:0 !important;">
<div class="col-md-1 col-xs-1" style="padding:0 !important;margin:0 !important;">
<button class="button" type="button" style="background-color: green; height: 20vmin; width: 100%; border:none; display: block;"></button>
</div>
<div class="col-md-10 col-xs-10" style="padding:0 !important;margin:0 !important;">
<button class="button" type="button" style="background-color: red; height: 20vmin; width: 100%; border:none; display: block;"></button>
</div>
<div class="col-md-1 col-xs-1" style="padding:0 !important;margin:0 !important;">
<button class="button" type="button" style="background-color: black; height: 20vmin; width: 100%; border:none; display: block;"></button>
</div>
</div>
</div>
TL;DR
The problem appears because of the column gutter, which by default is 30px. This means the minimum width of a column will always be 30px, no matter size it would have. Therefore, when you have a column with a width less than 30px, it is actually still going to have 30px as total width, and the columns will eat from each other because of that padding.
In detail
The working snippet
Firstly, I want to show you the working snippet I have managed to get. I have removed the inline styling from the HTML markup and added some CSS classes for better readability and easier understanding.
.pa-0 {
padding: 0 !important;
}
.ma-0 {
margin: 0 !important;
}
.my-button {
display: block;
height: 20vmin;
border: none;
outline: none;
background: transparent;
}
.bg-green {
background-color: green;
}
.bg-red {
background-color: red;
}
.bg-black {
background-color: black;
}
.bg-blue {
background-color: blue;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col-xs-3 pa-0 bg-blue">
<div class="row ma-0">
<div class="col-xs-1 pa-0 bg-green">
<button class="button my-button" type="button">
</button>
</div>
<div class="col-xs-10 pa-0 bg-red">
<button class="button my-button" type="button">
</button>
</div>
<div class="col-xs-1 pa-0 bg-black">
<button class="button my-button" type="button">
</button>
</div>
</div>
</div>
</div>
</div>
The problem
The problem described appears because of the way columns and rows are styled in Bootstrap. As you may already know, columns are having a gutter (padding) of 15px on each side, and rows have a margin of -15px, whose purpose is to remove the padding from the parent container (assuming correct syntax is applied, i.e. parent is a container or column). This means that the spacing created by the parent div is removed with the negative margin from the row.
This is a neat way of creating responsive design, but it also has an issue, which is the one you describe. Because all elements in Bootstrap have box-sizing: border-box, it means that the width of an elements is including the padding.
For example, if you have an column with the width of 100px, its size will be decreased by the gutter, which is by default 30px. Therefore, the actual width of the column will be 70px.
Having that said, let's think what is happening when the width of this column is less than 30px.
Our column is now 20px in width. As we need to decrease this size by the 30px gutter, we would normally get a negative width, which is impossible. Thus, the actual width of the column will be 0, and the padding will still be applied as 30px.
A solution
Simply create some utility classes which removes the padding and margins from the columns and rows. This way, the column will have the size they should, and the buttons will correctly be fit in their parents without being eaten by the padding from other elements.
I have used the same naming for the padding and margin utility classes as in Bootstrap 4. In pa-0, p comes from padding, a from all and 0 from the value set to this attribute.
Notes for your initial snippet
I am not sure the markup you have provided is in the way you normally do it, but it is worth to note:
It is generally a bad practice to have inline-styling set in your HTML markup1; mainly because of the Separation of Concerns principle and the reuse of styling you write. In your case, except for the background, you have used the same styling for buttons in three places - you can just create a new CSS class and use it in all three places.
In the Bootstrap grid system2, the column needs to always be a direct descendant of a row, and the row needs to be either descendant of a container or column, as you can see in the working example. By not having correct Bootstrap markup, unexpected styling behaviour might occur.
Useful links
SO post: What's so bad about inline styling?
Separation of Concerns in Web Development at Wikipedia
The Bootstrap 3 grid system
box-sizing at CSS-Tricks
The Definitive Guide to Using Negative Margins at Smashing Magazine
Bootstrap 4 Spacing utility classes
I'm starting to use Bootstrap 3 and it seems like you use it to define and over all structure but there are probably going to be a lot of containers that get your own custom classes?
I have a container which I've changed the definition of to be fluid so it's now:
.fluid-container {
padding-left: 15px;
padding-right: 15px;
margin-left: auto;
margin-right: auto;
min-width:500px;
}
but when it comes to the rows and sizing them I'm running into some problems here and I'm not sure if it's solved with my own custom classes or leveraging bootstrap in a way I don't know about.
The .fluid-container is going to be a header in this case and inside of it I have a container I'd like to be on the left, and then another container that will be on the right. Normally I'd just make two containers, float one left, the other right and then put a min width on the parent container.
In bootstrap what I've done is this (fiddle here http://jsfiddle.net/hg84F/2/):
<div class="lp">
<div class="lp-shell-head lp-fluid-container" style="border:1px solid red;">
<div class="row">
<div class="col-sm-2" style="border:1px solid blue;">left</div>
<div class="col-sm-2 col-sm-offset-8" style="border:1px solid green;">right</div>
</div>
</div>
</div>
The problem with this is when I make the page width smaller the two containers eventually become full width and stack on top of each other. What I want to have happen is have them stay on their respective sides no matter what and I wasn't sure how to make them do this. Any ideas of how to use bootstrap in this way?
What I want to have happen is have them stay on their respective sides no matter what and I wasn't sure how to make them do this. Any ideas of how to use bootstrap in this way?
http://getbootstrap.com/css/#grid -- More on Bootstrap grid.
<div class="row">
<div class="col-xs-2" style="border:1px solid blue;">left</div>
<div class="col-xs-2 col-xs-offset-8" style="border:1px solid green;">right</div>
</div>
All you're missing, based on your question is the Extra Small (always responsive) column class, which is col-xs-*
So this is a slightly different approach to your question. Based on your questions and comments to JonathanR's answer, I figured you need a little tweak.
Jonathan's solution is valid, but I'm really unsure of how far you want to "squeeze" the page before the "left" and "right" actually touch. Bootstrap uses percentage based widths and margins for the "col-" and "offset-" elements. If you attempt to squeeze it past a certain point, you will begin to see the horizontal scroll. This example solves that issue in a different manner.
Here's my working example: http://bootply.com/101136
<!-- 'Container-fluid' class no longer exists in 3.0, but I use it as a semantic wrapper class -->
<div class="container-fluid">
<!-- Make sure you use include the bootstap 'container' class -->
<div class="lp-shell-head lp-fluid-container container" style="border:1px solid red;">
<div class="row">
<!-- Use one XS col instead of 2 col-xs-2 and the offset -->
<div class="col-xs-12">
<!-- Use two div elements and float them left/right -->
<div class="pull-left" style="border:1px solid blue;">left</div>
<div class="pull-right" style="border:1px solid green;">right</div>
</div>
</div>
</div>
</div>
Minor change in the CSS. Switched your "min-width" to "max-width" in order to prevent your row from expanding past the 500px threshold.
.container-fluid {
border:1px solid purple;
}
.lp-fluid-container {
padding-left: 15px;
padding-right: 15px;
margin-left: auto;
margin-right: auto;
/* Switched to 'max-width' */
max-width:500px;
}