views:

160

answers:

4

Specifically, I am referring to a situation where you need to have a total width of say 100% on a DIV, but a 10 pixel padding and 1 pixel border. (And don't rely on the browser automatically setting it to that width — say it's floated left for instance.)

Is there any simple way to accomplish this without using JavaScript?

+2  A: 

No, there's no way to set this on one element that works with the currently major browsers.

You could use 2 nested divs. Set the 100% width on the outher div, and set the padding and border on the inner div.

molf
Yeah, I figured it wasn't possible with just one, but I wanted to make sure. I'll stick with nested DIVs. Thanks!
James Skidmore
A: 

That's only possible with CSS 3.

Georg
I presume you mean "That's *only* possible with CSS3": http://www.w3.org/TR/css3-values/#calc
mercator
A: 

What about the following solution?

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt;   
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">     
    <head>  
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
     <title>Content with Menu</title>   
    <style type="text/css">
      .content .outer{
        width:100%;
        border:1px solid black;
        background-color:green;
      }
      .content .inner{
        margin-left:10px;
        margin-right:10px;
        background-color:red;
      }
    </style>       
    </head>
    <body>   
    <div class="content">
      <div class="outer">
        <div class="inner">  
          <p>Hi!</p>
        </div>
      </div>
    </div>
    </body>
</html>

Update OK, doesn't accomplish what you are talking about with just one element.

merkuro
+2  A: 

If you use box-sizing: border-box you can set width: 100%; border: 1px solid black; padding: 10px; and the total of the width, border, margin, and padding will be what is specified for the width. Source

EDIT: True, browser support is a bit limited. FF 3.5 and Safari 4 support it, not sure about IE8 or Chrome.

Kerrick
Only works in CSS3, though.
Bob Somers
Chrome and Safari are both Webkit--so generally speaking (CSS), they are synonymous.
rpflo