I think I might already know the answer to this one but I need a sanity check!
Say I have
#gridtest{
width:590px;
}
I could change the width to a percentage by using RESULT=TARGET/CONTEXT. In this case the context is a container with a max-width set to 1000px so I can do this:
#gridtestpercent{
width:59%; /*590/1000*/
}
If I were to shrink the window down the div would always be in the proportion to the its container. But what if I wanted to do
#gridtest{
width:570px;
border:10px solid red;
}
I can work the width out based on the target now being 570 but as the window is shrunk the proportions all go out of sync.
#gridtestpercentnoborder{
width:57%; /*570/1000*/
border:10px solid red;
}
I can't use percentage border. I don't want to use JS to keep checking the context and I can't use the CSS3 box-border declaration yet.
If I wanted to use the technique described in responsive web design by Ethan Marcotte where everything shrinks in relation to each other would I be out of luck if using a border?
Cheers!
You could use CSS3 calc() function,
SASS mixin
The accepted answer is not correct. You actually have 2 options:
Use the
box-sizing
property, so all the paddings and borders are considered part of the size:Or, use the
outline
property instead of the border property. You will still have problems with the paddings, but it's easier to add. Example:Full explanation: http://designshack.net/articles/css/beating-borders-the-bane-of-responsive-layout/
In CSS3 you can also use the new
box-sizing
property to include the pixel and padding count into thewidth
of the element:If you want to stay semantic you can use
div { box-sizing:border-box; }
or some absolutely positioned:after
elements. See the post How do I add 1px border to a div whose width is a percentage?If possible, depending on your design, what I like to do is put the border as an absolute div with a width of 3px ( for example ) and a height higher than its parent div. I then set overflow hidden on the parent div.
Unfortunately, yes, you're out of luck. One hacky way to get around this problem is with a wrapper
div
that you use to create your border. So the outsidediv
would be 57% (in your example) with a background that is the color of your desired border. Then, the innerdiv
would have a width of 96% or so (play with the exact number to find a border that is appropriate for your design).