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>
Related
Whenever I add margin to any element I get overflow, I tried adding box-sizing, position:relative. but nothing works
searched on google but nothing seems to help me
can anyone know why is this happening?
Sample Image
The margin is outside the element. One way to deal with it is to use calc on width as in the following snippet.
And note that margin is diferent from padding: paddingis inside the border (so it is included in the area covered by the background color), margin is outside:
.x {
box-sizing: border-box;
margin: 30px;
width: calc(100% - 60px);
background: yellow;
border: 5px solid red;
}
<div class="x">margin....</div>
With padding instead of margin, this would be:
.x {
box-sizing: border-box;
padding: 30px;
width: 100%;
background: yellow;
border: 5px solid red;
}
<div class="x">Padding....</div>
You can't add margin to a div that is a sibling of your container or else it'll create an overflow. Use padding instead. See how the text in the margin example shifts the text.
.parent {
width: 100px;
height: 100px;
background: red;
}
.padding-example {
padding: 10px;
}
.margin-example {
margin: 10px;
}
<div class="parent">
<div class="padding-example">Correct</div>
</div>
<hr>
<div class="parent">
<div class="margin-example">Wrong</div>
</div>
I'm trying to figure out what are ways to stop borders from overflowing from it's parent container.
The only solution that I can come up is to set childs width and height by using calc() to calculate and subtract width and height of child's border.
Is there any better ways with dealing with this?
What solution would be suitable for pre-IE8?
Here's jsFiddle example.
CSS
.container {
width: 100px;
height: 100px;
margin: 0;
padding: 0;
clear: both;
background-color: purple;
}
.child {
width: 100%;
height: 100%;
border: 5px solid red;
}
HTML
<div class="container">
<div class="child">
text
</div>
</div>
you have set the width and height of child element to 100 percent so obviously it will be equal to the container one. Now setting a border,it will take extra width and height that will overflow.
so the first solution is the changing dimensions.
.child {
width: 90%;
height: 90%;
border: 5px solid red;
}
Note-90% is only in this case.
and the other solution is
.child {
width: 100%;
height: 100%;
border: 5px solid red;
box-sizing:border-box;}
As #dlev said:
One option is to set box-sizing: border-box; on the child, which forces the border to be considered part of the child element's "box", rather than just the content.
But even if you one to use calc css method then just deduct the border size:
.child {
width: calc(100% - 5px);
height: calc(100% - 5px);
border: 5px solid red;
}
demo
So basically what I want to do is have a div or two on a page that is larger than its parent div. Normally I would restructure the whole website however that would be a large task.
The reason I don't want them to be position absolute is that the container heights will then be screwed up and it will cause the position absolutes to overlap some divs.
The reason for the two divs being larger than their parent divs is they must be the width of the browser when the container divs can be no larger than 1200px.
Yes!
Not only that, we can do one better by using vw and calc.
Simply set the width of the child elements to be 100% of the viewport width by using vw (percentage viewport units), and then set their left margin to a negative calculated value based on this, minus the width of the wrapper. Other than the optional max-width of the parent, everything else is calculated automatically. You can dynamically change the width of the parent container, and the children will automatically resize and align as needed, without being positioned.
body,
html,
.parent {
height: 100%;
width: 100%;
text-align: center;
padding: 0;
margin: 0;
}
.parent {
width: 50%;
max-width: 800px;
background: grey;
margin: 0 auto;
position: relative;
}
.child {
width: 100vw;/* <-- children as wide as the browser window (viewport) */
margin-left: calc(-1 * ((100vw - 100%) / 2));/* align left edge to the left edge of the viewport */
/* The above is basically saying to set the left margin to minus the width of the viewport MINUS the width of the parent, divided by two, so the left edge of the viewport */
height: 50px;
background: yellow;
}
<div class='parent'>
parent element
<div class='child'>child element</div>
</div>
You can also use margins to achieve this: http://jsfiddle.net/MEc7p/1/
div{
outline: 2px solid red;
}
#outer{
width: 200px;
height: 400px;
}
#inner{
width: 600px;
height: 200px;
margin: 0 -20px;
outline: 1px solid green;
}
Try this fiddle
http://jsfiddle.net/stanze/g2SLk/
.wrapper {
width: 400px;
border: 1px solid #f00;
min-height: 153px;
}
.wrapper-child-1 {
float: left;
border: 1px solid #ccc;
width: 195%;
min-height: 262px;
}
<div class="wrapper">
<div class="wrapper-child-1"> </div>
</div>
I am getting a little gap between child-div and its parent-div. Is it possible for child-div to on its parent-div height? or (the way around)possible if the parent-div can scope the height of its child-div for not to overlap or get some extra spaces.
I have a DOM like this:
<div class="parent-div">
<div class="parent-image">
</div>
<div class="child-div">
</div>
</div>
here is my CSS:
.parent-image:{
height:60px;
}
.parent-div{
border: 1px solid #E3E3E3;
border-radius: 4px 4px 4px 4px;
margin-bottom: 20px;
width: 100%;
}
.child-div{
????
}
If you specify height: 100%; it will take the height of the parent.
If your child has padding, you need to change its box-sizing.
.child {
padding: 10px;
box-sizing: border-box;
}
If your child has more content than the parent, you either need to tell it to scroll, or hide. Note that on some browsers the scroll-bar will be inside the div, and on other browsers, it'll be on the outside.
.parent.c .child {
overflow: auto;
}
or
.parent.d .child {
overflow: hidden;
}
Demo of All
In your CSS, you can set your child-div to:
.child-div{
height:100%;
}
Here is a demonstration: http://jsfiddle.net/Xq7zQ/
I have four elements each of whom's width is set to 25%. They fill the width of the page perfectly.
I want to put a 1px gap between each one. If I do this (margin-right: 1px; for example), the last element overflows onto the next line. How can I reduce the width of each element by 1px without calculating the width in pixels in the first place?
I have just found a solution myself, with the help of #Lubnah in the comments.
.tab-list li {
margin-right: -1px;
border-left: 1px solid #fff;
}
.tab-list li:first-of-type {
border-left: none;
margin-right: 0px;
}
You can use CSS calc but its browser support is sketchy:
width: calc( 25% - 1px );
width: -moz-calc( 25% - 1px );
width: -webkit-calc( 25% - 1px );
Width is calculated inside the container. Any padding or margins you set will be added to the width.
Set your width to 23% and your margin to 1%
Left margin (1) plus width (23) plus right margin (1) = 25. That, placed four times on the page will add up to 100.
You could cheat slightly by having an inner div inside each element with width auto and margin-right:1px
See this fiddle: http://jsfiddle.net/7R6zZ/
<div class="outer">
<div class="inner">1</div>
</div>
<div class="outer">
<div class="inner">2</div>
</div>
<div class="outer">
<div class="inner">3</div>
</div>
<div class="outer">
<div class="inner">4</div>
</div>
.outer {
width:25%;
float:left;
}
.outer .inner {
width:auto;
margin-right:1px;
background:#999;
min-height:300px;
}
Use box-sizing: border-box; and borders.
* { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; }
div {
width: 25%;
border-right: 1px solid right;
}
use box sizing as box-sizing:border-box
and use 1px right border with same color as background
.box{
width:25%;
box-sizing:border-box;
border-right: 1px solid "same color as your background"
}