bootstrap container-fluid - remove margins the right way (overflow) - css

How can I remove all margins from boostrap container-fluid class and its rows?
.container-fluid { padding: 0;}
This does basically what I want, but it adds 20px overflow to body. So should I just do this:
body, html { overflow-x: hidden; }
Do something with .container-fluid > .row

To be specific about your question:
The .row has a negative left and right margin equal to the left/right padding value of the col-*-*, that is why there are horizontal scrollbars when you fiddle with the grid without understanding how it works. If you manipulate the column classes with zero padding on the left and right or with some other value, the negative margin on the .row must be equal to the the padding on the left and right of the column classes. The .container also has padding that matches the value of the column classes to prevent the scrollbars.
So the answer is: .container-fluid > .row -- make the margin:0 on the left and right if you remove the padding on the left and right of the column classes. If all is zero, then you can just adjust the .container or .container fluid with zero padding on the left and right, but if you use different values > 15px L & R, then it's a different story as the .container/.container-fluid will need to be adjusted if the left and right padding on the columns is greater than 15px.
There are no margins on the col-*-* it's padding which is quite different when you use box-sizing:border-box globally as Boostrap 3 does.
If you want a tight grid, remove all padding on the left and right of all column classes and then remove the negative margin on the left and right of the .row, and then you can remove the left and right padding on the .container.
DEMO: http://jsbin.com/jeqase/2/
Removes all padding and negative margin for a tight grid and full width of the .container with any surrounding element (body, html, whatever) with the class .alt-grid:
.alt-grid [class*="col-"] {padding-left:0;padding-right:0}
.alt-grid .row {margin-left:0;margin-right:0}
/* container adjusted */
.alt-grid .container {width:100%;max-width:none;padding:0;}
You can also do this with .container-fluid - the only thing to zero out is the left and right padding.

If you want to remove margin, overriding the Bootstrap class or div (container-fluid, html, body) is not the best thing to do. I think it's better to create a separate class and add it in elements.
If you want to remove all margins :
.remove-all-margin{
margin:0 !important;
}
If you want to remove all margins and paddings :
.remove-all-margin-padding{
margin:0 !important;
padding:0 !important;
}

it is always a better way to add a custom class to the elements that you want to get rid of the margins rather than overriding all bootstrap elements.
row.no-margin {
margin:0 !important;
}

Related

Horizontally Center a <div> that is not fixed

I want to horizontally center a column in a responsive grid I'm trying to make. I have the grid set up, and it works just fine. But I want to make a .class that will take a column and center it horizontally in the parent (.row). I would use this class to take a one-column-row and center the column in said row. I can center the text using the text-align property, but I want the entire column to be centered that way if I add a border/background to the column it will be centered in the row, not just the column's content.
Here is the codepen.
Given that you are setting a width on the column, simply use margin:0 auto to center it. You would need to overwrite float:left with float:none though..
UPDATED EXAMPLE HERE
.center {
text-align: center;
margin:0 auto;
float:none;
}
Alternatively, if the element doesn't have a fixed width set on it, you could simply make it an inline-block element and add text-align:center to the parent. Perfect for dynamically varying widths.

Trying to position elements does not work

I would like to position 5 column blocks,each containing text in a row.I have tried to create a wrapper class which has a width of 1600px,a padding of 30px to the left and the right and two classes that will align content to the left and the right respectively,each of these classes are in nested divs within the wrapper class.I gave each div a width of 300px and a height of 300px,the first four divs align next to each other while the last one goes below and to the right.The CSS and HTML is here.
EDIT:
The major issue got fixed when:
.wrapper
{
width:1600px;
height:auto;
padding:0px 30px 0px 30px;
}
But this seems to exceed the screen resolution needing me to scroll horizontally to view all the content,how do I deal with different screen resolutions?
try to give width:1600px;
.wrapper
{
width:1600px;
height:auto;
padding:0px 30px 0px 30px;
}
http://jsfiddle.net/sKsZN/
Try using float:left and display:inline-block for all the column <div>. Also make sure that the total width of those column don't exceed to .wrapper's width.
You should use float:left for your .content-right class. http://jsfiddle.net/sDyC5/2/
Wrapper element css width property has set to wrong value. You must set the properly unit (px|%|em|ex). And also display: inline-block and float propeties should not use together.

100% wide table inside div doesn't like margins

It seems that when I have a table inside a div and I set the table to 100% width and give it some margin, it seems to disregard the rightside margin. Here is the fiddle for it:
http://jsfiddle.net/gFQGb/
The width applies to the actual content of the element, so you have a table with 100% wide content, and on top of that you add some margins pushing the width over 100%, thus the right side of the table extends beyond the parent's right edge. Probably you should go with padding on the parent instead of margin on the table, or an additional wrapper <div> with just the margin.
just add padding: 10px; to the .inner class and remove margin from the table.
Here is the demo
That's how the CSS default box model works: width of the element (defined via width: x) + borders + margins + padding = total amount of space it takes up.
http://css-tricks.com/the-css-box-model/
You can change the box-model by using box-sizing: border-box, which will cause the width: 100% to include your paddings/borders.
http://css-tricks.com/box-sizing/

CSS three inline elements with align from left to right, how to occupy all available width

I have a slider, with a div that contains the controls previous, start/stop sliding, next.
I set "text-align" to left, center and right, respectively, and display to "inline". I have no idea, now, how to fill the whole width.
Here is the markup:
<div id="external_promo_controls">
<div id="promo_previous"></div>
<div id="promo_auto_controls"></div>
<div id="promo_next"></div>
</div>
And the CSS:
#external_promo_controls div{
display: inline;
}
#promo_previous {text-align: left;}
#promo_auto_controls {text-align: center;}
#promo_next {text-align: right;}
I guess I could have it done with floating elements, however I got quite a mess trying to have a correct combination of elements being on one line and centering the element in the middle. I’d like to avoid positioning elements with pixel value, since it would break as soon as I change text, font, or size or the container, which are all events that will eventually occur.
simple to use floats - this fiddle should set you on the right path:
http://jsfiddle.net/Q4paq/
New example with DIVs containg a link with a hover state.
http://jsfiddle.net/Q4paq/1/
try this. I am not sure it will work or not but suppose all three inner divs has width of 100px each then
outerDiv specify the width to be 300px with no margin and padding
innerDiv width:100px and margin and padding are 0px and float : left.
if you want to specify margin and padding then summation of all three innerDiv's width + padding + margin + border * 3 should be the width of the outer div.

Why don't margin-top: auto and margin-bottom:auto work the same as their left and right counterparts?

If I set the CSS margin properties of a div like so:
div { margin-left: auto; margin-right: auto; }
I get a div which is centered horizontally in the page, like so.
However, if I change the CSS to this:
div { margin-top: auto; margin-bottom: auto; }
my div is not vertically centered. I don't need to know a workaround (plenty of solutions are available) but I would like to know the reason for this behaviour. Why don't margin-top and margin-bottom work in the same way? What am I missing?
The short answer is the spec says so.
10.6.2 Inline replaced elements, block-level replaced elements in normal flow, 'inline-block' replaced elements in normal flow and floating replaced elements
If 'margin-top', or 'margin-bottom' are 'auto', their used value is 0.
http://www.w3.org/TR/CSS2/visudet.html#Computing_heights_and_margins
Assuming we are talking about auto margins within a Flexbox..
The reason that margin-left and margin-right set to auto will center an item is because the width by default is 100% of the available container for a block element.
The height on the other hand attempts to fill as little as the space as possible, so margin-top and margin-bottom as auto will default to 0. BUT, if your element is within an element with a fixed height, then margin-top and margin-bottom will be able to calculate the center based on that height.
Ex. http://jsfiddle.net/jwz76e3g/24/

Resources