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).
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 can't figure out how to make the grids to extend to the container's full width no matter how many columns there are
<div class="container">
<div class="row">
<div class="col-md-2 first"></div>
<div class="col-md-9 second"></div>
</div>
</div>
In the example above are displayed 2 columns inline and the .row having full width.Yet if i remove the first div(keeping the exact same code), the second div will change its position to left and will keep the original width.I need it to stretch to the full width of the container.The reason i need this is because i'll have some php conditional expression that will prevent it from showing,in which case i want the template to change and the second div to be full width and position on the middle.
I found an answer with the following CSS which indeed worked but it also messed the responsive structure and i'll have to patch it up till no end probably:
CSS:
.row{
display: table;
width: 100%;
}
.first, .second{
display: table-cell;
float: none;
}
.second{
width: 100%;
}
Since i'm a newbie with Bootstrap, i was wondering if there is any built in class which solves this problem? Thank you
In this case, just have the first div as col-2 and the second one as col.
col will be 100% width if col-2 is missing, but if it is there, col will fill the remaining columns.
Check out the Grid system documentation.
I'm not sure why you're using display:table. Just use Bootstrap 4 auto-layout grid, and the second will fill the width if the first is missing/hidden.
"Auto-layout for flexbox grid columns also means you can set the width
of one column and have the sibling columns automatically resize around
it."
https://www.codeply.com/go/igkSq57Vwn
<div class="row">
<div class="col-md-2 first"></div>
<div class="col-md second"></div>
</div>
i have same problem . and you can fix it by set (margin : 0px) for row (by style attribute or id in css file) and set (padding : 0px) for col (by style attribute or id in css file)
<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.
I'm a beginner in css and I have a little problem. I tested different methods to handle a responsive 4 div grid with css, and I failed.
I want to responsively arrange the 4 divs as an grid with 2 columns and, if the display is to narrow it should be floating to a one column layout.
Here is a sketch of the responsive grid:
Here is a simple responsive grid with 4 div boxes in plain CSS and HTML it aranges from two to one columns when the browser width becomes smaller :
DEMO (resize the result window to see the effect)
Note that the max-width value on the #container is set to 450px so that 2 blocks + their margin can fit by width in two colmuns.
When the widow is smaller than 450px the width of the #container adapts to the window width and as the block can't fit anymore, they rearage to one column.
#container {
text-align: center;
max-width: 450px;
margin: 0 auto;
}
.block {
width: 200px;
height: 200px;
margin: 10px;
display: inline-block;
background: #00CC99;
}
<div id="container">
<div class="block">1</div>
<div class="block">2</div>
<div class="block">3</div>
<div class="block">4</div>
</div>
You may want to check out Bootstrap, and specifically their Grid System. You can easily accomplish what you want with that. Otherwise, you'd want to look into writing your own CSS Media Queries to handle the different screen sizes.
Here's a JSFiddle showing how this can be achieved using Bootstrap. Just drag the side of the Result container to make it smaller and you can see the blocks shift. This may need some tweaking but it should get you going.
<div class="row">
<div class="col-sm-2 col-sm-offset-3 col-xs-12">
<div class="block">1</div>
</div>
<div class="col-sm-3 col-xs-12">
<div class="block">2</div>
</div>
<div class="col-sm-2 col-sm-offset-3 col-xs-12">
<div class="block">3</div>
</div>
<div class="col-sm-3 col-xs-12">
<div class="block">4</div>
</div>
</div>
In the above code, I'm creating a Bootstrap Grid which uses 12 columns. You can specify the column width at different screen sizes, for example the class col-sm-2 is saying use 2/12ths of the width for small screen sizes, then offset it 3 to center it. col-xs-12 says to use the full width for extra small screen sizes (essentially moving each block to its own row for extra small screens). Note the row class as well which is also Bootstrap specific.
Hopefully this helps!
Bootstrap is a great tool to do this as the above answerer said. Bootstrap allows you to position items in a grid layout (as you want).
Another way to do this is create media queries in css that will take effect when the browser has a smaller or larger min-width.
I recommend using Bootstrap as all of the heavy lifting is done for you and you would just have to make small tweaks to ensure it looks like you want it to.
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;
}