I'm trying to improve my project's HTML structure and I found something along these lines:
<div class="full-sidebar">
<div class="col-xs-12"></div>
</div>
Where "full-sidebar" is a custom class.
What I've been wondering is: is it a good idea to do so? Would I be better off in wrapping the column divs in a row? It just feels "wrong" done in the current way.
I know of at least one reason to use a row.
"cols-" are floated and wrapping them in a row adds a clear after all columns. This prevents subsequent content from potentially overlapping with your columns. Additionally, it also ensures the row's height is equivalent to the height of the column content. If you don't clear the columns' container, then the container will not include the column in its own height.
Note that "col-" is only floated at widths greater than 992px. So you'll only run into content overlap issues above that limit. See this fiddle.
HTML:
<div class="norow">
<div class="col-md-6"></div>
<div class="col-md-6"></div>
</div>
CSS:
.row {
border: 2px solid yellow;
}
.norow {
border: 2px solid green;
}
.col-md-6 {
height: 50px;
border: 2px solid red;
}
Also, as others have mentioned, col padding and margins are designed to work in conjunction with row padding and margins.
You should always wrap <div class="col-xs-12"></div> inside <div class="row"></div>.
Reason-1
That's the bootstrap grid-system. We use bootstrap to make a layout clean and responsive. It doesn't create any harm if you do so.
Reason-2
If you don't wrap within <div class="row"></div> it will create unnecessary problem in near future once your layout grows long. You won't be able to keep a track of your layout.
so, in general it is good to wrap it inside <div class="row"></div>, if you don't have a solid reason for not doing it.
Hope this helps!
Actually it is not standard procedure to use bootstrap column classes outside the row. It might disturb the layout of the column design. In bootstrap css file each column class has assigned different width percentage according to their use. If you use column classes outside the row class it may form different or disturbed layout than actual layout prescribed by the bootstrap. So it is not a good practice to do.
Grid System Rules
Some Bootstrap grid system rules specified by w3Schools.com
Rows must be placed within a .container (fixed-width) or .container-fluid (full-width) for proper alignment and padding
Use rows to create horizontal groups of columns
Content should be placed within columns, and only columns may be immediate children of rows
Predefined classes like .row and .col-sm-4 are available for quickly making grid layouts
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
Grid columns are created by specifying the number of 12 available columns you wish to span. For example, three equal columns would use three .col-sm-4
Is using twitter bootstrap column classes outside rows a good idea?
No.
Bootstrap col-* should only be used within row. From the docs..
Use rows to create horizontal groups of columns.
Content should be placed within columns, and only columns may be immediate children of rows.
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.
Also see: Bootstrap Rows and Columns - Do I need to use row?
Related
Is that possible to wrap div inside a row? I know it's possible but I just want to know it's a bad practice or not. I'm not using any col-md classes inside row.
Please see the example below..
<div class="row">
<div class="home-bg">
1
</div>
</div>
It's a bad practice because .row is only meant to contain grid col* as specifically stated 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.
http://getbootstrap.com/docs/4.1/layout/grid/#how-it-works
Your question is the same as asking can I have a div wrapping another div? The class row is just a bootstrap class with some css rules. Nothing more nothing less.
You will need to do this so many times. You will mix divs with classes from different libraries, with custom classes that you will have created and with so many different types of elements.
There isn't a question of good practice or bad practice. If you need some bootstrap functionality that already exists from a class, it's a good practice to use the existing functionality. If not then do whatever you want.
You can do this, but the question is why do you want to? All the row class does is add negative margins and the necessary flex properties to allow the column grid to work, seeing as you don't need the column grid, do you need the negative margins? If not then don't add the rowclass. Having a non-column grid class as the child of row isn't going to break your site, but it's important to bare in mind it may cause some undesired design bugs due to the negative margins it adds.
You can learn more about the Bootstrap column grids here.
I just started using Bootstrap 3. I am having a difficult time
understanding how the row class works.
Is there a way to avoid the padding-left and padding-right?
<div class="row" style="background:#000000">
<div class="col-xs-4 .col-xs-offset-1">
col
</div>
<div class="col-xs-2">
col
</div>
<div class="col-xs-2">
col
</div>
<div class="col-xs-2">
col
</div>
</div>
http://jsfiddle.net/petran/rdRpx/
In all grid systems, there are gutters between each column. Bootstrap's system sets a 15px padding on both the left and the right of each column to create this gutter.
The issue is that the first column should not have half a gutter on the left, and the last should not have half a gutter on the right. Rather than use some sort of .first or .last class on those columns as some grid systems do, they instead set the .row class to have negative margins that match the padding of the columns. This "pulls" the gutters off of the first and last columns, while at the same time making it wider.
The .row div should never really be used to hold anything other than grid columns. If it is, you will see the content shifted relative to any columns, as is evident in your fiddle.
UPDATE:
You modified your question after I answered, so here is the answer to the question you are now asking: Add the .container class to the first <div>. See working example.
With bootstrap 3.3.7 this problem is solved wrapping the .row with .container-fluid.
See my reply below to similar post.
Why does the bootstrap .row has a default margin-left of -30px?
You basically use "clearfix" instead of "row". It does the exact same as "row" excluding the negative margin.
I used the row class inside the container class and still had the some problem. When I added margin-left: auto; margin-right: auto; to the .row class it worked fine.
#Michelle M. should receive full credit for this Answer.
She said in one of the Comments:
Adding the 'mx-auto' class in bootstrap 4 fixed the overflow issue for
me.
You would need to update your first div Element like so:
<div class="row mx-auto" style="background:#000000">
No need to do this for all Nested-Rows (if you have them).
Just add mx-auto to the most-outer row (or Rows) to avoid the Vertical-Scrollbar.
Do not Override the behavior of all Bootstrap Rows by adding a "row" class to replace the Margins.
For any future developers debugging this problem:
Bootstrap sets the padding for row columns, so none of the contents of a row should appear outside the container. If you're experiencing this and you are using bootstrap's grid system correctly using the col-... classes, it's likely that you have additional CSS somewhere resetting the padding on the columns.
I have a question about what would cause the following error in bootstrap 3.
Earlier I was writing some HTML and using CSS to style and was using bootstrap 3 for the rows and the columns.
Now I had everything inside of a fluid container and the container had margins etc. It has a standard header, and each row was split into two columns where each size has a width of 6 ie col-lg-6 .... inside of the container and then I had labels and inputs inside of my rows. Just a basic layout.
Now, whenever I tried to give a bottom border to each row I noticed that after inspecting the element the row was actually outside of the designated
columns / div.
However, when I changed the class from row to row-fluid it works perfectly. Why is this? Also, I noticed that if I make a clear-fix div after using row-fluid it fixes some apparent spacing issues, is this a requirement when using row-fluid or is my code just messed up somewhere.
Asking, because I thought row-fluid was only a bootstrap 2 and not a 3 thing, or am I mistaken?
I would include a fiddle showing you exactly what I'm talking about, but don't have the code available atm.
TWBS-2
http://getbootstrap.com/2.3.2/scaffolding.html#global
TWBS-3
http://getbootstrap.com/css/#grid-intro
The <div> nested under your <div class="row"> carry the attributes container width dependent on viewport size, including all gutters and padding.
Comparing TWBS-2 and TWBS-3, TWBS-3's grid framework is designated as fluid for all viewport sizes. .row-fluid is not documented as an option, but I would refer to their version history for those specific changes. If the version history of the <div class="row-fluid"> applies, then the gutters and padding of its children's <div> columns becomes full-width to the size of the container rather than fixed-width as designated by the rules of the TWB-S3 grid system without .row-fluid designation.
I am trying to create a simple two column layout which has one big image followed by two columns, left one for image right one for text.
But I'm not able to align the image column with the top big image.
I can add a class with -15px margin but then there is misalignment in XS screen size.
I can add another -15px margin for xs screen also then I might have to fix something else. This is such a basic layout issue, I have a feeling I am doing something wrong.
Can you guys suggest what I am doing wrong and how should I approach two column layout in Bootstrap 3?
Here is the Bootply Link
The <div class="col-sm-12"> in the lower box causes the trouble. You have two options here:
Just delete the div
Add an additional <div class="row"> after the <div class="col-sm-12">
You were essentially creating a nested grid and bootstrap requires you to begin every new nesting level with a new <div class="row">
See: http://www.bootply.com/sh13QtYpFP
#bspellmeyer is correct. When nesting columns in Bootstrap, you need to add a row and then your nested columns.
HERE IS WHY
The grid works with 3 parts: a container, a row and column(s)...
The container has 15px of padding. The row negates the container padding with -15px of margin. Columns have 15px of padding, which pull the content away from the edges of the container and create a consistent 30px gutter.
The purpose for adding 15px of padding that is only negated by the negative row margins seems silly, but it is essential to allow for nesting columns inside of other columns! Note in the diagram below how the nested columns indicated by the red outline fits neatly into the enclosing column without getting additional padding applied.
I promise that once this sinks in, making the grid behave exactly how you want every time will be a breeze.
You need to add :
.custom-left {
padding: 0;
}
to your .custom-left class. Add the same to your .custom-right class if you want to align this div with the top image as well.
Updated Bootply: http://www.bootply.com/iQYEpYqXJZ
I have some data (a list) that I need to display in a layout with rows and columns. On my desktop I have room for it to have 5 columns, but my mobile device only has enough width for one column. How can I display this data so that if there isn't room for another column, the next item just starts a new row.
I tried using "float: left;" and it's very close to what I want, but when there isn't any more room and it starts the next row, I need the columns to still line up.
update: The problem with float:left; is that the items don't line up into columns. Since the items are user-editable I don't have any way of knowing ahead of time how wide they might be.
update2: Removed references to table and instead described what a table looks like.
This shows an example of how the markup is and what I have attempted. It also has a manually built table that shows what I'm trying to get it to do.
http://jsfiddle.net/7rrcQ/
If you want them to line up in columns, then obviously for a column its elements must have always the same width. If you don't want to touch floated elements width, then you can wrap them in another div and then fix that div width.
Otherwise calculate dinamically with javascript floated element width and apply to them margins to line up with the column. But anyway you have to have in mind some column width, known a priory or calculated from first dynamically loaded element in that column.
If you want to adjust number of columns to the page width, then all column elements from all columns must have the same width if you want them to line up, because one element may be from a column or from another depending on the page width. If this width is in percentages, then there will be always same number of columns independently of the page width. In the other case (in pixel, em's), column elements will "jump" to the next row when page is gets more narrow.
I hope this is what you were looking for.. http://jsfiddle.net/7rrcQ/5/
<div class="newContainer">
<div class="tdElement">Lorem</div>
<div class="tdElement">ipsum</div>
<div class="tdElement">dolor</div>
<div class="tdElement">sit</div>
<div class="tdElement">amet,</div>
<div class="tdElement">consectetur</div>
<div class="tdElement">adipiscing</div>
<div class="tdElement">elit.</div>
</div>
Your Css
.container {
overflow: auto;
width: 100%;
}
.tdElement{
border:1px solid black;
float:left;
width:100px;
}
It is not possible to achieve the result without setting the items to a specific width (in some units). A table (whether in HTML or using display: table etc. in CSS) can get its column widths allocated by browsers according to the width requirements of the items, but then you need to have content divided in columns in a specific manner.
To make the amount of columns depend on the available total width, you would need scripting and guesswork.
If equal-width items are acceptable (and I don’t see why not) and if you can make a reasonable guess on the maximum width in em unit, you could use your current (div and span markup) and only the following style sheet:
.item {
border: solid 1px;
padding: 0 0.15em;
float: left;
width: 5.5em;
text-align: right;
}
The main problem is that adjacent borders won’t collapse. Sorry, I have no solution to that.