DIV width in % and padding - html

I have 2 DIVs next to each other (A to the left and B to the right) within a container of 980px wide. I set a width of 50% for both A and B which works great.
Question: I'd like to add some padding-right to DIV A so that the text in it doesn't touch the text of div B. If I do that I need to adjust the % of the DIV (i.e. to 48%). Is there a way to avoid that? (i.e. get the % automatically adjusted based on the padding)

yeah, you can use the box-sizing property. By setting:
box-sizing: border-box;
The width property will set the total width (including borders and padding), so for example a div with:
div {
width:500px;
padding: 20px;
border: 10px solid blue;
box-sizing: border-box;
}
would have a visual overall width of 500px, rather than a default width of 500 + 40 + 20 = 560px.
For reference: http://css-tricks.com/box-sizing/

You can do it by using one more wrapping div:
<div class="wrapper">
<div class="inner">
<div class="left"></div>
<div class="right"></div>
</div>
</div>
.wrapper { width: 960px; }
.inner { padding: 1em; }
.left, .right { width: 50%; float: left; }
Or use box-sizing that makes calulations very easy.
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
It changes the rendering of paddings and border so that they are included in the width rather than added to it.

If you would like to add padding without reducing the width of the div, you can use: box-sizing: border-box; More info
e.g.
-webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
-moz-box-sizing: border-box; /* Firefox, other Gecko */
box-sizing: border-box; /* Opera/IE 8+ */
This will make your div the desired width (50%), and the padding will be subtracted rather than added to the div. Beware this is a CSS3 property and won't work in all versions of IE:
http://caniuse.com/css3-boxsizing

HTML
<div class="tbl">
<div class="row">
<div class="cell">A</div>
<div class="cell">B</div>
</div>
</div>
CSS:
.tbl{
display:table;
border-spacing:5px;
}
.row{
display:table-row;
}
.cell{
display:table-cell;
width:50%;
}

for width, margin & padding values use % or em values
Have a look at this code
.container {
max-width: 980px;
}
section {
float: left;
margin: 0.0122448; /* 10px ÷ 980px */
width: 0.479591; /* 470px ÷ 660px */
}
aside {
float: right;
margin: 0.0122448; /* 10px ÷ 980px */
width: 0.479591; /* 470px ÷ 980px */
}
for complete tutorial on responsive design -
http://learn.shayhowe.com/advanced-html-css/responsive-web-design

You can apply box-sizing:border-box to the divs with padding.
-moz-box-sizing: border-box;
box-sizing: border-box;
Live demo
From MDN:
border-box:
The width and height properties include the padding and border, but not the margin.
box-sizing is supported all the way back to IE8.

You have to use box-sizing:border-box; beacuse using border-box; will give you a look of box with border & it will manage space for border by itself . you can give width and color of your border by yourself border:2px solid black; . And thus, the content of your both divs can be diffrentiated.

Related

adding padding or margin breaks float

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>

CSS wrong appearance of border-radius on a nested div

Using the following HTML I need to:
Make sure that the border of target div (pink) is adjacent of the wrapper-target red border div.
Must work on any value of border-radius.
Considering that:
I am using box-sizing: border-box; but can be also reset to a default value.
I cannot change the border-radius property of the target div.
*,
*:after,
*:before {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
<div id="wrapper-target" style="position:absolute;top:100px;left:100px;width:250px;height:250px;border-radius:50px;border:25px solid red;">
<div id="target" style="position:relative;width:100%;height:100%;background-color:plum;border-radius:inherit">
</div>
</div>
NOTES:
I do not need to make a circle in this specific example :).
Part 1 of the problem: (Child becoming a round in original demo)
The problem is because of the box-sizing: border-box. When this is set, the defined height, width of the box (250 x 250px) is considered as inclusive of the width of the border and the padding. So, the element's actual content area is only 200px x 200px (excluding 50px for horizontal & vertical borders).
Thus the child div will only have a size of 200px x 200px (this can be verified in Dev tools). When a border-radius of 100px is inherited from parent, it becomes a round as that is half of its dimensions.
So, the border-radius cannot be inherited for the child if the shape has to be maintained. It should be set as 80px (approximate). (Initially I had mentioned that a value of 76px was working better and that I was trying to find out the reason for it - please refer to Part 2 for the reason.)
*,
*:after,
*:before {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
<div id="wrapper-target"
style="position:absolute;
top:100px;left:100px;
width:250px;height:250px;
border-radius:100px;
border:25px solid red;">
<div id="target"
style="position:relative;
width:100%;height:100%;
background-color:plum;
border-radius:76px;">
</div>
</div>
Part 2 of the problem: (even when border-box is removed, it leaves a gap)
This is because the assigned border-radius is the radius of the outer border and not that of the inner border. The inner border radius is calculated as outer border radius minus border thickness.
As per spec:
The padding edge (inner border) radius is the outer border radius minus the corresponding border thickness.
So, the child's border-radius need to be equal to the inner border radius of the parent. That is, the child's border-radius should be 75px (100px - 25px thickness of border).
This is also why a border-radius of 76px worked better than the 80px as mentioned earlier. 76px is closer to 75px than 80px :)
Solution without changing border radius of target:
If border-radius: inherit on the child (target) cannot be changed then the only option is to make the child the same dimensions as parent (using calc), positioning it and then clipping the overflow in parent.
*,
*:after,
*:before {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
<div id="wrapper-target" style="position:absolute;
top:100px;left:100px;
width:250px;height:250px;
border-radius:100px;
border:25px solid red;
overflow: hidden;">
<div id="target" style="position:relative;
width:calc(100% + 50px);height: calc(100% + 50px);
top: -25px; left: -25px;
background-color:plum;
border-radius:inherit;">
</div>
</div>
Try adding same bg color of target div to main div.
<div id="wrapper-target" style="position:absolute;top:100px;left:100px;width:250px;height:250px;border-radius:50px;border:25px solid red; background-color:plum;">
<div id="target" style="position:relative;width:100%;height:100%;background-color:plum;border-radius:inherit">
</div>
</div>
DEMO
*, *:after, *:before {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
<div id="wrapper-target" style="position:absolute;top:100px;left:100px;width:250px;height:250px;border-radius:50%;border:25px solid red;">
<div id="target" style="position:relative;width:100%;height:100%;background-color:plum;border-radius:inherit"></div>
</div>
Set value of border-radius in %, not in px, if you want to make a circle.
You inherit the border-radius with a fixed value while the child element has other dimensions. Calculate the border in percent. Use border-radius:40%; on your wrapper.
Maby this wil help. The css is now set in a external file.
The border-radius:inherit; checks the border-radius that is already there. so it sets to that border-radius.
*,
*:after,
*:before {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
#wrapper-target {
position: absolute;
top: 100px;
left: 100px;
width: 250px;
height: 250px;
border-radius: 50px;
border: 25px solid red;
background-color: plum;
}
#target {
position: relative;
width: 100%;
height: 100%;
background-color: plum;
border-radius: inherit;
}
<div id="wrapper-target">
<div id="target">
</div>
</div>

dividing the screen to 4 Quarters doesn't work

I am dividing my screen to 4 Quarters but it doesn't work with all screen resolutions.I need it to always be 4quarters even by changing the window size.
here is the code:
body{
height:800px;
}
div{
position:relative;
border:1px solid red;
width:49.7%;
height:49.7%;
}
#Q1,#Q3{
float:left;
}
#Q2,#Q4{
float:right;
}
</style>
</head>
<body>
<div id="Q1"> </div>
<div id="Q2"> </div>
<div id="Q3"> </div>
<div id="Q4"> </div>
</body>
Use this CSS to make the height 100% and quarter it:
body{
height:100%;
}
html {
height: 100%;
}
div{
position:relative;
border:1px solid red;
width: 50%;
height: 50%;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
#Q1,#Q3{
float:left;
}
#Q2,#Q4{
float:right;
}
The computed width of the boxes exceeds the total available space in lower screens. This is because the border of 1px around the elements.
You could give the div elements a box-sizing: border-box; declaration so that their width would be calculated including padding and borders.
Example Here
div {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
In addition, if you want to resize the height of the boxes with the respect to the height of the body, note to set height: 100% on body and html as well.
You have to specify the height of html to get height: 100% to work for the <body>. This because a percentage value of height property is relative to the height of box's containing block.
Updated Example Here
html, body {
height: 100%;
padding : 0;
margin : 0; /* Remove the default 8px margin around the body */
}
Also note that UAs apply a default margin to the <body> by default. Make sure you have reset the user agent stylesheet.

How to make table take up 100% of remaining space?

fiddle
HTML
<div id="a">
<table id="b">
<tr><td></td></tr>
</table>
</div>
CSS
* {
margin: 0;
padding: 0;
}
#a {
background-color: yellow;
}
#b {
background-color:red;
height: 50px;
margin-left: 50px;
width: 100%;
}
I want the table #b to span from 50px from the left of its container (#a) all the way to the right edge of of #a.
By setting the width to 100% it goes off the page because it tries matching the width of #a. If I omit the width then the table is too small.
Setting the table to display:block and removing the width seems to give the desired behaviour. Is this reliable, or is there another method of achieving the same thing? I'd prefer not to resort to absolute positioning.
You can use the calc() function in CSS like this:
#b {
width: calc(100% - 50px);
/* your other stuff */
}
Most browsers support this nowadays. Have a look at caniuse.com to see, which browsers don't.
Example Fiddle
One way to do is is to use box-sizing: border-box
* { -moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
...
#a {
...
padding-left: 50px;
}
Unlike Sirko's answer, this is will work in IE8+ and not IE9+
Another way is to use conflicting absolute positioning

Nested DIV in a responsive design - margin, padding, etc

It's been a while since I really dealt with percentages in web design. I have a nested DIV which sits inside a container but the padding of the container pushes it beyond the 100% width. Without wishing to embark on a process of trial and error to see what makes it as close to 100% of the width as possible, how do I go about achieving a snug fit? I also noticed that when I resized the window and made the space smaller, the right hand padding simply got smaller.
<div id="block">
<div class="inside">ssdfsdfdfsfdf</div>
</div>
#block {
width: 100%;
background-color: #CCC;
padding: 20px;
}
.inside {
height: 200px;
background-color: #333;
}
http://jsfiddle.net/AndyMP/cs2U9/4/
Use box-sizing css property for #block element.
#block {
width: 100%;
background-color: #CCC;
padding: 20px;
-o-box-sizing: border-box; /* Opera */
-ms-box-sizing: border-box; /* IE */
-moz-box-sizing: border-box; /* Mozilla */
-webkit-box-sizing: border-box; /* Chrome, Safari */
box-sizing: border-box;
}
About CSS box-sizing property: http://www.w3schools.com/cssref/css3_pr_box-sizing.asp
I needed an
overflow: hidden
on the container DIV in order to get it to sit perfectly.
http://jsfiddle.net/AndyMP/cs2U9/6/