I tried to create CSS grid like bootstrap col.
I want to add a padding of 15px from the left and the right. But when I add the attribute it breaks the float (makes it to not stand side by side).
Here is the example:
https://jsfiddle.net/t29q1gcL/
Why didn't it work?
.grid-4{
float: left;
width: 40%;
background: red;
padding: 0 15px;
}
.grid-6{
float: left;
width: 60%;
background: blue;
padding: 0 15px;
}
add this to your css, reference link http://www.w3schools.com/cssref/css3_pr_box-sizing.asp
* {
box-sizing:border-box;
-webkit-box-sizing:border-box;
-ms-box-sizing:border-box;
-moz-box-sizing:border-box;
}
check with the working fiddle https://jsfiddle.net/d8mrdz7x/
Add: "box-sizing: border-box;"
.grid-4{
float: left;
width: 40%;
background: red;
padding: 0 15px;
box-sizing:border-box;
}
.grid-6{
float: left;
width: 60%;
background: blue;
padding: 0 15px;
box-sizing:border-box;
}
.clearfix{
clear: both;
}
This is because you set width for content area, and content area is inside the padding, border and margin. So when you specify padding, total width would be 60% + 15px.
You can get around it by using nested cell:
/* remove padding from grid-X classes */
.cell {
padding: 0 15px;
}
<div class="grid-6">
<div class="cell">
<p>content for grid6</p>
</div>
</div>
(I've forked your jsfiddle here: https://jsfiddle.net/9ss09b15/)
You can also add box-sizing:border-box; to your grid-X classes, which will make it include padding and border in width, but it still won't include margin.
If padding is a constant, you can use calc() function to assign a calcutated width. Check below example.
.grid-4 {
float: left;
width: calc(40% - 30px);
/* 30px = 15px(padding-right) + 15px(padding-left) */
background: red;
padding: 0 15px;
}
.grid-6 {
float: left;
width: calc(60% - 30px);
background: blue;
padding: 0 15px;
}
.clearfix {
clear: both;
}
<div class="grid-4">
<p>content for grid4</p>
</div>
<div class="grid-6">
<p>content for grid6</p>
</div>
<div class="clearfix"></div>
Updated fiddle: https://jsfiddle.net/t29q1gcL/11/
Use box-sizing:border-box; style. Because The box-sizing property is used to tell the browser what the sizing properties (width and height) should include.
In width calculation formula is width = margin-left -padding-left + width + margin-right -padding-right + border
In your css style total width morethan 100% You have use 60% , 40% and padding 30px. so, it's break.
Use CSS3 box-sizing Property
The box-sizing property is used to tell the browser what the sizing properties (width and height) should include.
Should they include the border-box? Or just the content-box (which is the default value of the width and height properties)?
Instead of calculating width by including padding and border, the box-sizing property in combination with the border-box value uses the width property as the actual rendered width.
Example:
.sidebar {
box-sizing: border-box;
width: 200px;
padding: 20px;
border: 1px solid #DDD;
}
Any padding or border that’s applied will not be added to the rendered width. Instead, it will automatically subtract from the space that’s available in the content area. This results in code that is far more readable. Here’s an image that helps illustrate how box-sizing: border-box calculates widths.
Reference
https://css-tricks.com/box-sizing/
http://www.w3schools.com/cssref/css3_pr_box-sizing.asp
http://blog.teamtreehouse.com/box-sizing-secret-simple-css-layouts
.grid-4{
float: left;
width: 40%;
background: red;
padding: 0 15px;
box-sizing: border-box;
}
.grid-6{
float: left;
width: 60%;
background: blue;
padding: 0 15px;
box-sizing: border-box;
}
.clearfix{
clear: both;
}
<div class="grid-4">
<p>content for grid4</p>
</div>
<div class="grid-6">
<p>content for grid6</p>
</div>
<div class="clearfix"></div>
Related
My problem is that I am trying to use padding in my CSS so that the two divs inside my div are responsive at 50% each. But together they obviously are bigger than 100%. I know this is probably the paddings fault, but I don't know how to fix it.
CSS:
.columns {
max-width:100%;
width:100%;
display:inline-block;
text-align:left;
}
.col1 {
width:50%;
float:left;
padding-left:100px;
}
.col2 {
width:50%;
float:right;
padding-right:100px;
}
HTML:
<div class="columns">
<div class="col1">
</div>
<div class="col2">
</div>
</div>
By default the box model will use padding and border to expand an element beyond a specified width. To keep the paddings/borders from pushing outward, and contain them inward, use box-sizing: border-box;
.columns {
max-width: 100%;
width: 100%;
display: inline-block;
text-align: left;
}
.col1 {
width: 50%;
float: left;
padding-left: 100px;
}
.col2 {
width: 50%;
float: right;
padding-right: 100px;
}
.col1,
.col2 {
box-sizing: border-box;
}
<div class="columns">
<div class="col1">
</div>
<div class="col2">
</div>
</div>
In situations like these, it's useful to put this rule at the beginning of your styles:
* {
box-sizing: border-box;
}
It sets everything to box-sizing: border-box;, which means that the borders and paddings are included in the width/height settings, i.e. a container with width: 200px, border: 1px and padding 10px will really be 200px wide, including borders and padding (and not 222px, as it would be without box-sizing: border-box).
I have an elementary problem with my web that is driving me nuts.
Imagine that I have a container div which I set to be max-width 1024 px. Then I have two more divs inside it that are width:50% each of them. I try to add a padding to the insider divs, but then it overflows above the 1024 max-width pixels of the container div.
My doubt is, if the padding are like inner-margins, they are part of the element, wouldn't it be supposed to be counted inside that 50% width? In other words, I want the element + the padding to be 50%, not just the element.
Just to make things clearer,
This is how it looks now :
... and this is how it should look. :
So, what should I do?
The box model it's like this:
Total width = width + padding + border
If you have got a 1000px of width + 10px of padding it will be a 1020px of total width.
To avoid this you can use this property:
box-sizing: border-box;
More info:
https://developer.mozilla.org/es/docs/Web/CSS/box-sizing
You need to set box-sizing: border-box to your inner div's so the width given includes your padding.
If you intend to use margin, that will not do it.
For that you can use calc(), so if your inner div's will have a 5px margin, it would look like this
width: calc(50% - 10px);
And you can combine them ...
.outer {
width: 500px;
background: red;
padding: 20px 0;
overflow: hidden;
}
.inner {
width: calc(50% - 10px);
height: 100px;
margin: 5px;
padding: 10px;
box-sizing: border-box;
background: blue;
float: left;
}
<div class="outer">
<div class="inner">
</div>
<div class="inner">
</div>
</div>
You can use manual width something like ::
<div style="width:1024px">
<div style="width:502px;padding:5px"></div>
<div style="width:502px;padding:5px"></div>
</div>
Yes the paddings are like inner-margins, they are part of the element's actual size.
You should calculate
total element width = width + left padding + right padding + left border + right border + left margin + right margin
e.g.
The two <div> elements above end up with different sizes in the result (because div2 has a padding specified).
.div1 {
width: 300px;
height: 100px;
border: 1px solid blue;
}
.div2 {
width: 300px;
height: 100px;
padding: 50px;
border: 1px solid red;
}
<div class="div1">This div is smaller (width is 300px and height is 100px).</div>
<br>
<div class="div2">This div is bigger (width is also 300px and height is 100px).</div>
For your problem you will have to use box-sizing: border-box;
The CSS3 box-sizing property allows us to include the padding and border in an element's total width and height.
If you set box-sizing: border-box; on an element padding and border are included in the width and height:
See below example
.div1 {
width: 300px;
height: 100px;
border: 1px solid blue;
box-sizing: border-box;
}
.div2 {
width: 300px;
height: 100px;
padding: 50px;
border: 1px solid red;
box-sizing: border-box;
}
<div class="div1">Both divs are the same size now!</div>
<br>
<div class="div2">Hooray!</div>
I'm using this method to get my footer at the bottom of the page properly.
However, when I add a border to my footer, I end up with a scroll bar regardless of the content on the screen. My confusion is that:
I thought borders functioned outside padding but inside margins, such that like padding they do not effect any layout external to the div.
Is this wrong?
Here is my skeleton html:
<body>
<div class="wrapper">
<div id="top"></div>
<div id="body">
<div id="box1"></div>
<div id="box2"></div>
</div>
<div class="push"></div>
</div>
<div class="footer"></div>
</body>
And here is the relevant CSS:
#top
{
height: 105px;
border-bottom-style: solid;
border-bottom-color: #044E97;
border-bottom-width: 7px;
}
#body
{
margin-top: 25px;
width: 100%;
background-color: white;
color: #282828;
font-size: 85%;
}
#box1
{
width: 460px;
float: left;
margin-left: 25px;
margin-right:75px;
}
#box2
{
margin-left: 25px;
margin-top: 15px;
padding-top: 0%;
padding-bottom:0%;
margin-bottom:45px;
width: 350px;
height: 320px;
float:left;
border-top-style: solid;
border-top-color: #FFFFFF;
border-top-width: 10px;
}
html
{
height: 100%;
}
body
{
min-height: 100%;
background-color: white;
margin: 0;
}
html, body
{
min-height:100%;
position:relative;
}
.wrapper
{
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -3em;
}
.footer, .push
{
height: 3em;
clear: both;
}
.footer
{
width:100%;
background-color: #0563A1;
border-top-style: solid;
border-top-color: #044E91;
border-top-width: 8px;
color: white;
font-size: 77%;
padding-top:.3em;
padding-bottom:.3em;
}
If I change the footer div to not have padding, the scroll bar clears.
This assumption is incorrect:
I thought borders functioned outside padding but inside margins, such that like margins they do not effect any layout
Margins and borders do affect layout—it is just that they are positioned outside the padding. The hierarchy of spacing starts from explicitly defined dimensions (width and height), followed by paddings, then borders, then margins.
If borders and margins did not affect layout, it would then be impossible to create spacing between elements (no margins) or that borders of adjacent elements will overlap (borders taking up no additional space).
The issue you are facing is that borders are computed not as part of the width or height—when you leave a 3em space at the bottom of your body, the footer that is 3em high will fill the space. But when you add borders and/or padding to it, it will add an additional vertical height (sum of top padding of 8px, and top and bottom borders of 0.3em each) to the defined height, causing it to exceed 3em and hence trigger an overflow.
To force your footer to stick to 3em, you can either use box-sizing: border-box to force the height attribute to take into account border widths and padding, or height: calc(3em - 0.6em - 8px) to manually reduce the height of the footer so the sum of height, top padding and top+bottom border widths remains at 3em total.
Change your box-model to border-box, like this:
html{box-sizing: border-box;}
Let me know if it helps.
I have a row of divs that each contain one image. The image height is set to 100% for each image and each div has a padding-left of 20px, except for the last div which has no padding. Because the last div does not have padding and the img width is set to 100%, the last image appears taller than the other images. I would like to fix this but I am not sure how.
Here is a jsfiddle that shows a visual representation of the problem.
HTML :
<div class="grid">
<div class="col-1-4">
<img src="" />
</div>
<div class="col-1-4">
<img src="" />
</div>
<div class="col-1-4">
<img src="" />
</div>
<div class="col-1-4">
<img src="" />
</div>
</div>
CSS :
img {
width: 100%;
}
*, *:after, *:before {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
margin: 0px;
}
[class*='col-'] {
float: left;
padding-right: 20px;
}
[class*='col-']:last-of-type {
padding-right: 0px;
}
.grid {
width: 100%;
max-width: 940px;
min-width: 755px;
margin: 0 auto;
overflow: hidden;
}
.grid:after {
content: "";
display: table;
clear: both;
}
.col-1-4 {
width: 25%;
}
I'd switch to a (percentage-based) margin for your gutters, like so:
[class*='col-'] {
float: left;
margin-right: 4%;
}
[class*='col-']:last-of-type {
margin-right: 0;
}
.col-1-4 {
width: 22%;
}
Example: http://jsfiddle.net/sknf7/3/
You have this in your CSS
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
border-box means the size of an element is its height/width and its padding. So if your image is 100px tall with 10px padding at top and bottom, it'll only show as 80px tall. This is what's causing your issue.
The alternative is content-box, where the padding is added on to the height or width. So 100px + 10px padding top and bottom would be 120px tall instead.
One way to fix it is make the divs content-box, and slightly narrow as a result (to compensate for increased padding width).
[class*='col-'] {
box-sizing: content-box;
-moz-box-sizing:content-box;
}
.col-1-4 {
width: 23%;
}
It's because you use box-sizing: border-box;, you can either remove this or use margin instead of padding. (either way you need to adjust your grid)
border box removed:
http://jsfiddle.net/sknf7/1/
margin instead of padding:
http://jsfiddle.net/sknf7/2/
Paul's answer certainly works, however you may want to consider improving the compatibility of/shortening your CSS by re-working this a bit. Those *col wildcard selectors won't work in IE8 or earlier
.col-1-4 {
width: 22%;
margin-right: 4%;
float: left;
}
.col-1-4:last-child
{
margin-right: 0;
}
The above would get you the same effect
I want to make three columns in HTML+CSS, first 15%, second 70% and third 15%. The problem is that with my code, third column is wrapping down when i resize window. I've written such CSS for my website:
.maincont {
margin-left: 0px;
margin-right: 0px;
width: 100%;
}
.lcol,
.rcol,
.content {
display: inline;
float: left;
position: relative;
margin-left: 10px;
margin-right: 10px;
}
.lcol {
width: 15%;
background-color: red;
}
.rcol {
width: 15%;
background-color: green;
}
.content {
width: 70%;
background-color: blue;
}
HTML code:
<body>
<div class="maincont">
<div class="lcol">
</div>
<div class="content">
</div>
<div class="rcol">
</div>
</div>
</body>
What am i doing wrong? Thanks in advance.
#Maccath is absolutely correct. Instead of changing any of your numbers however, might I suggest adding this to the top of your CSS file:
* {
-moz-box-sizing: border-box;
-ms-box-sizing: border-box;
box-sizing: border-box;
}
Support for everything newer than IE8 - your widths and heights will incorporate paddings.
Also, change your margin numbers in your CSS to padding instead and you'll get your desired result.
The margins on .content are your problem. Margins are adding to the width of your overall content, so it's 20px over 100% width in total, which is why it's forcing the columns to wrap.
I would advise to use a percentage margin on .content instead. Reduce .content's width to, say, 66% and then set the margin to ... 0.66% (weird maths since it's relative). This however does have the disadvantage that the gaps between your columns aren't going to be consistent based on the width of the browser window.
remove
margin-left: 10px;
margin-right: 10px;
it resizes div size and summary it is more than 100%