is bootstrap less mixin .makeColumn smaller than a span? - html

I'm using bootstrap with less and I'm currently trying to make web semantic.
HTML part:
<!-- our new, semanticized HTML -->
<div class="article">
<div class="main-section">...</div>
<div class="aside">...</div>
</div>
Less part:
/* its accompanying Less stylesheet */
.article {
.makeRow(); // Mixin provided by Bootstrap
.main-section {
.makeColumn(10); // Mixin provided by Bootstrap
}
.aside {
.makeColumn(2); // Mixin provided by Bootstrap
}
}
But when I take a look at the rendered html page... my article take less space than a span12
I can also put a .makeColumn(10) and .makeColumn(4) and it will stay on the same line.
It's like it was a 14 grid column and not a 12 grid column.
Am I missing something?

No. If your required number of columns is n, the .makeColumn mixin and the .span (#grid > .core > .span) mixin both calculate width as follows:
(#gridColumnWidth * n) + (#gridGutterWidth * (n-1))
which is the width you'll need to set your element to a width of n columns on the grid.
If n = 6, it calculates all column widths and gutter widths from the left edge of your grid to the right-hand edge of column 6. 6 columns and 5 gutters.
.span only does width, .makeColumn also adds a float: left, and has an optional #offset argument that which isn't important to your current problem, but which allows you to add columns to the left of the element by adding a margin: left.
As #sody says above, wrapping your existing elements in a .container should fix your problem.
And I agree with #kleinfreund on using the html elements where you can:
<div class="container">
<article>
<div class="main-section">Bootstrap rocks
<aside>
By the way...
</aside>
</div>
</article>
</div>
I hope that helps.

Related

Empty columns in responsive grid?

I'm trying to learn the responsive grid and am having difficulty grasping how to place items within the "columns." I'm working with 12 of them, something that looks like the one here: http://www.w3schools.com/css/css_rwd_mediaqueries.asp
For example, I'd like to have one row with the name of the website on the left and the navigation bar on the right side:
<div class="row">
<div class="col-1">empty column acting as margin; do i need a div for this?</div>
<div class="col-2" id="name">
website name
</div>
<div class="col-4">another set of empty columns</div>
<div class="col-4" id="navbar">
Home
About
Page 3
</div>
<div class="col-1">empty column as margin</div>
</div>
How would I be able to achieve this? The divs with "empty columns" in them are clearly wrong, but I'm not sure how to get the proper widths. The link above says that the number of columns in each row should add up to 12; does this mean I need to specify empty columns, too?
Mayor browsers ignore width for empty divs, a simple way to avoid this behavior is adding to empty divs.
<div> </div>
The best solution using grid layout is to utilize its' power and not add empty elements in html (DOM).
In this example we want to have 12 columns in total, same width, where first and last are empty, as well mid four columns:
1 2 4 4 1
Html code:
<div class="grid-example">
<div class="col-2" id="name">
website name
</div>
<div class="col-4" id="navbar">
Home
About
Page 3
</div>
</div>
CSS code:
.grid-example {
...
grid-template-areas: ". nm nm . . . . nvb nvb nvb nvb .";
}
.grid-example #name {
grid-area: nm;
}
.grid-example #navbar {
grid-area: nvb;
}
Here is what this means:
In html there are only useful elements without redundant empty ones.
CSS is master here. Child div's are assigned to grid area with (short) names. Now using property grid-template-areas, #name div is set to spread on two columns, #navbar div on four columns. Dots sets respective columns empty. That's it.
see at CodePen >>
The similar principle could be used for rows, columns doesn't have to be the same, element could be assigned to any cells area etc. Basically grid is very useful to spread any element over any grid area.
Note: .row class has flex layout so not needed here.
The total number of columns should add up to 12 for a row as each column equals 1/12 of 100% (8.333%)
In your example you have the correct number of columns:
col-1 | column count: 1
col-2 | column count: 3
col-4 | column count: 7
col-4 | column count: 11
col-1 | column count: 12
Total Columns = 12
For the columns to display inline the column divs must have additional css to override the default display: block;
Option 1 (display inline block):
[class*="col-"] {
display: inline-block;
margin-right: -0.2em; /* small hack to fix inline block spacing */
}
Option 2 (float):
[class*="col-"] {
float: left;
}
If you go the float method you will need to use a clearfix.
Update
I would recommend Option 1 as you will run into less issues with clearfixes and empty columns. See this codepen
Another css only solution similar to Boris's good answer (https://stackoverflow.com/a/48133942/4236417) but using grid-column rather than grid-template-areas.
.grid-example {
display: grid;
grid-template-columns: repeat(12, 1fr);
grid-column-gap: 1%;
border: 1px solid black;
}
#name {
grid-column: 2 / 5
}
#navbar {
grid-column: 8 / 12
}
<div class="grid-example">
<div id="name">
website name
</div>
<div id="navbar">
Home
About
Page 3
</div>
</div>
Don't forget that css grid columns actually refer to the lines between the columns so grid-column: 8/12 runs from beginning of the 8th column ("Grid Track") until the beginning of the 12th column/Grid Track.
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout/Basic_Concepts_of_Grid_Layout#grid_tracks

Twitter bootstrap grid columns touching eachother?

I am currently working on my own responsive grid with columns. I am currently using 12 columns and I give each column a multiplier of 100/12 and subtract a margin on each column. So I make each column a little bit smaller so I can fit in a margin-left and make gutters this way.
#for $i from 1 through $column-count {
.column-#{$i} {
width: (((100 + $gutter-width) / $column-count) * $i) - $gutter-width;
}
}
This leaves me with something like this:
.column-1 {
width: 7.41667%; }
.column-2 {
width: 15.83333%; }
.column-3 {
width: 24.25%; }
etc.
This way I can set a margin-left for each column and just remove the very first margin-left in each row with the first:child selector
Twitter bootstrap however uses a column grid where all columns touch eachother and they use padding to kind of fake column gutters. However I tried some fooling around with bootstrap and you run into a problem when you actually set like a background for a column. Immediately you can see that the columns will touch. How do people prevent this from happening?
I guess using another element would inside the column would help? This however would get a bit messier than I would like probably.
I am asking this because I am looking for a solution since my grid can only go from default columns to 100% width columns when you downsize it.
grid: http://titan.ravenwebdesign.nl/
I would like to be able to add something like .mobile-column-6 to certain grid columns in a manner that bootstrap has with it's grid tiers of classes.
This would be helpful in for example having 2 columns next to each other when in downsized view. Except I'm currently using rows and every first child of my row has the margin removed. So it would't work like this:
<div class="row">
<div class="column-3 mobile-column-6"></div>
<div class="column-3 mobile-column-6"></div>
<div class="column-3 mobile-column-6"></div>
<div class="column-3 mobile-column-6"></div>
</div><!-- .row -->
Because of my responsive design which says that the first column will have it's margin removed I can't simply turn it into a 2 columns next to eachother design.
Hopefully somebody can help me out here although I understand this problem is extremely hard for me to explain like this.

Margin between columns using Neat

It appears to me that the default behavior for Neat should be that each span column should have a margin (or gutter) between adjacent span columns. Every example that I've found online simply installs Neat, does a quick demo, and the result has a gutter between adjacent elements. No settings changed.
Would anyone know why that is not happening for me? I have a clean install of Bourbon and Neat. My html is as follows...
<footer class="col-2">
<section class="left">
content
</section>
<section class="right">
content
</section>
</footer>
My sass looks like...
.col-3 {
#include outer-container;
.left {
#include span-columns(6);
}
.right {
#include span-columns(6);
}
}
Here's a link to the rendered output:
So I actually have 2 questions.
Why are the columns stacked on top of each other, even if I float the left column?
Why are my columns ignoring the gutter between each column?
It looks like you misnamed your class (div has class .col-2 and scss has .col-3) and since you nested your classes the column mixin isn't applied to child div.
code works with matching class names here http://sassmeister.com/gist/0c1963fef94a14d5268f
The issue was related to my normalize stylesheet. Had some rules overriding Neat.

How to achieve float: top in CSS/HTML

If you can read the Headings ... one's called JWT, the other Alela Diane.. how do I get "Alela Diane" to fill up the space between them ( no puns intended )
The CSS property for these div's is set to display: inline-block.
The HTML - >
<div id="shastra_display">
<div class="shastra_post">
There are multiple div's like this containing the Alela Diane's and JWT's etc.
</div>
</div>
The CSS - >
#shastra_display
{
width: 880px;
}
#shastra_display div
{
display: inline-block;
vertical-align: text-top;
}
.shastra_post
{
width: 270px;
background-color: light-grey;
}
Is it always going to be just two
columns? – thirtydot
It's two columns because the width of
the parent box allows only two to fit.
– Zach
So, the number of columns changes depending on the browser width.
That means you can't "cheat" and do it like this (as suggested by #Stefy):
http://jsbin.com/atimu4
Other than a fixed number of columns, CSS can't do it. See this answer for a comparision of the ideas:
CSS Floating Divs At Variable Heights
You will have to use JavaScript. There's already a convienient jQuery plugin: jQuery Masonry
Some interesting demos:
http://masonry.desandro.com/demos/animating-jquery.html
http://masonry.desandro.com/demos/adding-items.html
You should probably use a 2-column template in order to display the items properly.

CSS Multiple multi-column divs

I have a bunch of items (text, image, mixed content, etc) that I want to display. The user can define what row and what column that item appears in. For example, in row 1, there could be two items/columns, both images. In row two, there could be three items / columns, one with an image, two others as pure text. Oh, and the user may specify the width of any particular column/image/item.
I have a solution that uses multiple tables that works. In essence, each row is a new table. This works for the most part.
I'm wondering if I can use just divs?
Now my CSS foo is lacking, and I tried to copy examples from the web, and I haven't been able to get it working. Right now I have something like this:
[for each row]
[div style="float: none"]
[for each column]
[div style="float: left"]
[content]
[/div]
[/div]
[br]
But everything is overlapping each other.
I've also tried using "position: relative", but things look even more borked.
So can divs actually be used for multiple rows and different number of columns?
They sure can! The basic effect (it sounds like) you're looking for is like so:
#wrapper {
width: 900px;
}
.item {
height: 200px;
width: 200px;
margin: 10px;
float: left;
}
<div id="wrapper">
<div class="item">Something</div>
<div class="item">Something else</div>
<div class="item">Something cool</div>
<div class="item">Something sweet</div>
<div class="item">Something just ok</div>
</div>
So what this would do is set up a fixed-width container (the #wrapper) and fill it with "blocks". Because each has a fixed width and is floated left, they'll line up in a grid. Because of the width/margin I've set for each, you should get 4 per row. If you need spacers, just put in blank DIVs to get the content on the right row/column.
The 960 Grid System is designed to accomplish things just like this. Take a look at http://960.gs/ they have plenty of examples of what you can do with 960.
For the unindoctrinated, it defines two types of layouts 12 column or 16 column. Each column is a predefined width with predefined gutters between them. You can then use the built in css styles to have a div span any number of the columns. It's incredibly powerful for layouts where different sections of the page using different layouts.