This isn't exactly a "problem" I'm trying to solve, just something I've noticed about bootstrap columns when I work with them.
It seems that bootstrap's "scaffolding" sizes "reset" somehow when I put a column inside a column.
For example:
Let's say I have a col-md-9, and I want two columns inside of it, which together span the entire width of the "parent" column.
I would have thought that I would set the width of the two columns at 4.5 (col-md-4.5), but when I do this, the two smaller columns behave as though they are nested inside a col-md-12... In other words, they fill up 3/4ths of the col-md-9, not 100%. If I give them the class of col-md-6, they will together span the whole width of their parent (col-md-9).
Here is a visual representation of what this would looks/behave like:
Again, this isn't really a 'problem' that I need solved per se, just something I was curious about because it seems to conflict with what I've read in a lot of bootstrap documentation.
First things first - there is no col-md-4.5 unless you've made your own custom CSS class for that. But perhaps you did something like col-md-4 and then a col-md-5 to add to a total of 9 and saw the results that you observed.
When I first started learning bootstrap I was baffled because I would do similar things with their grid system. If I had a col-md-9 then I expected that the children could never exceed 9. My elements were never positioned right. Then I learned how it really works.
Basically you can think of the children of an col-*-* as "resetting" themselves, or in other words they always will add up to 12 even if their parent is a col-xs-6 (or whatever else).
Here is a fiddle I have made.
The code for the fiddle is here:
<div class="container">
<div class="col-xs-12 red-outline">
<div class="col-xs-4 blue-outline">
col-xs-4
</div>
<div class="col-xs-5 blue-outline">
col-xs-5 for a total of 9
</div>
</div>
</div>
<div class="container padding">
<div class="col-xs-12 red-outline">
<div class="col-xs-6 green-outline">
col-xs-6
</div>
<div class="col-xs-6 green-outline">
col-xs-6 for a total of 12
</div>
</div>
</div>
<div class="container padding">
<div class="col-xs-12 red-outline">
<div class="col-xs-6 green-outline">
<div class="col-xs-3 black-outline">
3
</div>
<div class="col-xs-3 black-outline">
3
</div>
</div>
<div class="col-xs-6 green-outline">
<div class="col-xs-6 black-outline">
6
</div>
<div class="col-xs-6 black-outline">
6
</div>
</div>
</div>
</div>
What you can see when you run this fiddle is the red top bar represents the parent col-xs-12. In the first example, the blue add's up to 9, and doesn't take up the full width.
You can see in my second example the green add's up to 12, and does take up the full width.
Now for the third example we have some serious inception stuff going on. We have a col-xs-12 and then the two green col-xs-6's and then on the left hand side we have two col-xs-3's which add up to 6 (and as you can see, they do not take up the full width). On the right hand side we have the two col-xs-6's inside of a col-xs-6 which is inside of a col-xs-12. It's a bit of a mind bender, but just understand that you should have everything add up to 12, and not add up to whatever width the parent is.
Related
I'm new to coding and tried to build a small responsive website with Spectre.css.
I want 3 cards in a row and if the viewport gets too small, they should go in the next row.
Currently they just shrink but don't go in the next row.
Spectre.css provides media queries but I'm not sure how to actually use the provided structure...
See Flexbox Grid and Responsive
Tried to figure it out myself but... mhhh :D
<div class="container grid-xl">
<div class="columns">
<div class="card column col-3"></div>
<div class="card column col-3"></div>
<div class="card column col-3"></div>
</div>
</div>
I've found the answer myself.
Instead of adding only col-3 I had to add more than one argument.
Like col-3 col-3 col-md-5 col-sm-11 to make the line breaks.
Sounds logic...
Now it works as intended.
I have a razor page with a three column grid such that:
#for (int i = 0; i < Model.Questions.Count; i++)
{
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-6 align-self-center order-first">Question Text</div>
<div class="col-xs-12 col-sm-8 col-md-8 col-lg-4 align-self-center">Fixed Width Table</div>
<div class="col-xs-12 col-sm-4 col-md-4 col-lg-2 align-self-center order-last">Other Text</div>
</div>
}
The text in the first column is long enough to stretch over 2 lines in the lg screen size, and everything is fine and lined up. But on some questions the text is short and the first column shrinks, throwing alignment out of whack.
How can i fix this?
Attached a screenshot (this is in Arabic, one of the languages the page handles, which evidently is more compact than English...).
The problems I had were solved with two measures which I will record here for the benefit of anyone with similar 'strange' issues with bootstrap 4 columns.
a) DO NOT set margins left or right on divs that are rows or columns.
b) Use .w-100 on rows that have columns with variable length content in them.
I want both my main content area and the first element of my navbar to start offset from the left by 2rem. However, this is causing me a lot of headache with child columns.
I can't figure out how to add a global offset of 2rem, without also having to change all calculations of child columns to account for the change. Example:
<div class="row">
<!-- outer content div to offset all content elements -->
<div class"col-lg-offset-2">
<div class="row">
<!-- shouldn't this refer to the inner row? -->
<div class="col-lg-6>
This is 6
</div>
<div class="col-lg-6>
And 6 more...and content is getting cut-off from the right
</div>
</div>
</div>
</div>
What I was hoping to get, is to continue using all child divs width settings the same and to have their size compared relative to their new parent width, which is 2rem less (or to the left).
As far as I know, you should set the size when setting offset. So if you offset your .col-lg-* with x, then you should also reduce the size of it with x.
See my example:
<div class="row">
<div id="main" class="col-xs-offset-2 col-xs-10">
<div class="row">
<div class="col-xs-6">
This is 6
</div>
<div class="col-xs-6">
And 6 more...and content is not gettin cut-off from the right
</div>
</div>
</div>
</div>
So what I did, was to offset the div #main, but I also made the size smaller to compensate for the offset. Say the offset is by 2 cols, so the size was also made less by 2 cols. So instead of col-xs-12 taking the entire with, col-xs-10 no takes the entire width because it has an offset of 2 cols.
Like #jae.phoenix already said columns within a row should always add up to 12. So when you offset the column by 2 the other column should take the other 10 unless you want to center it then you should keep in mind "2" offset should be taken from the other side that would leave 8. Offset also stays with the column meaning if it is applied in the xs size it will be applied to all sizes above it example sm md lg xlg; SO keep that in mind. here is my answer:
<div class="container">
<div class="row">
<div class="col-sm-10 col-sm-offset-2">
<div class="row">
<div class="col-sm-6">
This is 6
</div>
<div class="col-sm-6">
And 6 more...and content is not gettin cut-off from the right
</div>
</div>
</div>
</div>
Keeping the column to start offset as sm rather that xs leaving some room when screen size gets to small. Hope this helps.
I'm currently working with bootstrap, but there's just a few things that confuse me about the grid management.
In this case, my grid structure tends to go (as an example)..
<div class="container">
<div class="row">
<div class="col-xs-6">
<div class="row">
<div class="homeBox">
</div>
</div>
</div>
<div class="col-xs-6">
<div class="row">
<div class="homeBox">
</div>
</div>
</div>
</div>
</div>
(Apologies for poor formatting)
This works fine for full width elements (col-xs-12), however if i'm trying to have two elements next to each other (col-xs-6) with a row before adding the content, they simply touch each other in the middle, and if adding a background it makes it seem like one element. Here's a JFiddle showing what I mean. If i was to add a row before the homebox, it makes them touch. I can also just not add a row after the col-xs-x, but then it won't align correctly to the other full width elements on the page.
In sites I'm working on, I workaround this by adding a 'leftRow/rightRow' class for small devices onwards, but this seems a bit of a bodged way of doing it.
What's the correct way to approach this?
Cheers.
I am doing a products page using Bootstrap and most of the images appear fine, but the last line on the two bottom rows checker in a smaller screen size.
To let you know what I mean I've included an image here.
Here is the code of one of the rows that are not working right:
<div class= "row">
<div class="col-sm-6 col-md-3">
<img src="images/seasonal_1.jpg">
</div>
<div class="col-sm-6 col-md-3">
<img src="images/seasonal_2.jpg">
</div>
<div class="col-sm-6 col-md-3">
<img src="images/seasonal_3.jpg">
</div>
<div class="col-sm-6 col-md-3">
<img src="images/seasonal_4.jpg">
</div>
</div>
This usually happens when you have some padding or border that is increasing the size of the element.
Grids work on the basis of % so if you have 2 elements that width each equal 50% but then you add something like some padding or a border you are going over a total of 100% and the element is pushed to the next line.
My advice (given I don't have the actual code) would be to review your styles using the browser dev tools to see what styles are being applied when this happens. You should be able to track down the offending property.
Hope this is useful even if not a definitive answer.
So I eventually fixed the problem by making the all the images the exact same size/dimensions (some were a few pixels off). Just thought I'd follow up on this.
Also I later discovered that if you have an extra line of text or two underneath the picture that might throw the pattern off.