Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keep padding from making the element bigger?

I have an element with a 70% width, and it is floating beside an element with 30% width, but when I add 25px of padding it expands the element and breaks the format, is there any way to make it just increase the contents distance from the edge as opposed to just making it bigger?

like image 222
nkcmr Avatar asked Mar 03 '11 00:03

nkcmr


People also ask

How do you stop padding from increasing width?

to fix this, you simply need to update the box-sizing parameter and set this to border-box in your css. Or you can do this for all elements by simply adding the following. This tells the browser to account for any border and padding in the values you specify for an element's width and height.

Does padding increase the width of an element?

Normally, when an element's size is set, the width and height properties determine the width and height of the element's content box. Any padding added to the element will increase the total computed width and/or height of the element—this is how the default box model works in regards to sizing the element.

Why does padding increase div size?

So why does this happen? This is because browsers by default add all the padding , margins , etc to the overall height and width of the element, in our case it is the div HTML element. By default, the box-sizing CSS property of the element will be set to content-box which causes this unpredictable behavior.

Does padding add to the height of an element?

The box model of an element in CSS—includes the content, padding, border, and margin areas. Any padding added to the element will increase the total computed height of the element, so you may not always end up with the expected height using just the height property if you also add padding to your element.


1 Answers

When you use the border-box model, the padding is included in the box size. See here for details.

<!DOCTYPE html> <html>     <head>         <title>padding example</title>         <style type="text/css">         .seventy {             display: block;             float: left;             width: 70%;             background-color: red;         }         .thirty {             display: block;             float: left;             width: 30%;             padding: 25px;             box-sizing: border-box;             -webkit-box-sizing: border-box;             -moz-box-sizing: border-box;             background-color: green;         }         </style>     </head>     <body>         <div class="seventy">Stuff</div>         <div class="thirty">More Stuff</div>     </body> </html> 
like image 157
codelark Avatar answered Oct 21 '22 14:10

codelark