Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Padding adds to div width / height? [duplicate]

Tags:

css

<html>
<head>
<style>
    * { margin:0; border:0; padding:0; }
    div { height:500px; }
    #container { width:1000px; background-color:#000; }
    #column-one { float:left; padding:10px; width:500px; background-color:#234; }
    #column-two { float:left; padding:10px; width:500px; background-color:#345; }
</style>
</head>
<body>
<div id="container">
    <div id="column-one">
    </div>
    <div id="column-two">
    </div>
</div>
</body>
</html>

Counterintuitive indeed.

like image 213
Sid Bayer Avatar asked Feb 02 '11 20:02

Sid Bayer


3 Answers

The content-box model states that padding and borders don't count in the width that you set for a box. So they add on to its width.

Modern browsers support CSS3's box-sizing: border-box to cause width to represent the total width of content, padding and borders (the default is, of course, content-box, triggering the above behavior).

like image 59
BoltClock Avatar answered Nov 13 '22 02:11

BoltClock


The width is the width your content can fill, not the width of the box delimited by your border.

like image 22
greg0ire Avatar answered Nov 13 '22 04:11

greg0ire


Maybe I misunderstood but I think you can solve your problem by using width: auto for the div instead of width: 100%; PS: I know the post is old but maybe still can be useful...

like image 1
daniele piscaglia Avatar answered Nov 13 '22 04:11

daniele piscaglia