Pixel Border and Percentage width in Proportion
Asked Answered
D

7

10

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!

Dues answered 16/11, 2011 at 13:57 Comment(0)
H
5

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 outside div would be 57% (in your example) with a background that is the color of your desired border. Then, the inner div would have a width of 96% or so (play with the exact number to find a border that is appropriate for your design).

Hurless answered 16/11, 2011 at 14:21 Comment(2)
Thanks for this - I really wanted to avoid doing that but at least there's a fix if I run into it. The other slightly hacky way would be multiple background images for the border but that's not very cross browser at the moment.Dues
Yea, multiple backgrounds wouldn't work in older browsers; however, it wouldn't break anything like using border-box would. So maybe it's a good gracefully-degrading option.Hurless
A
12

You could use CSS3 calc() function,

.selector{
  border: 5px solid black;
  width: -moz-calc(50% - 10px);
  width: -webkit-calc(50% - 10px);
  width: calc(50% - 10px);
}

SASS mixin

@mixin calc($property, $expression) {
  #{$property}: -moz-calc(#{$expression});
  #{$property}: -webkit-calc(#{$expression});
  #{$property}: calc(#{$expression});
}
article {
  border: 1px solid red;
  @include calc( width, '100% - 2px')
}
Austronesia answered 10/4, 2013 at 19:39 Comment(0)
O
8

You could use an inset box-shadow instead of a border:

box-shadow: 0px 0px 0px 10px red inset;

Just pad the inside of the container to compensate.

Edit: I write "pad" but of course if you use padding it'll throw off the box dimensions. Margin the content inside instead.

Orientate answered 23/11, 2012 at 15:52 Comment(1)
box-shadow has a performance overhead and should be used sparingly - especially on low end and mobile devicesStavros
S
7

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:

.column {
    width: 16%;
    float: left;
    margin: 0 2% 0 2%;
    background: #03a8d2;
    border: 2px solid black;
    padding: 15px;
    font-size: 13px;

    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

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:

.column {
    width: 16%;
    float: left;
    margin: 0 2% 0 2%;
    background: #03a8d2;

    outline: 2px solid black;
}

Full explanation: http://designshack.net/articles/css/beating-borders-the-bane-of-responsive-layout/

Saraband answered 23/4, 2014 at 17:18 Comment(1)
This works perfectly for what I am doing - working on percentage based containers for a responsive design on a mobile viewport. While I normally don't use box-sizing as it isn't the most supported property - it works well enough for the fluid containers on the platforms that I need this to work on. Perfect!Arak
H
5

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 outside div would be 57% (in your example) with a background that is the color of your desired border. Then, the inner div would have a width of 96% or so (play with the exact number to find a border that is appropriate for your design).

Hurless answered 16/11, 2011 at 14:21 Comment(2)
Thanks for this - I really wanted to avoid doing that but at least there's a fix if I run into it. The other slightly hacky way would be multiple background images for the border but that's not very cross browser at the moment.Dues
Yea, multiple backgrounds wouldn't work in older browsers; however, it wouldn't break anything like using border-box would. So maybe it's a good gracefully-degrading option.Hurless
D
4

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?

Dustindustman answered 15/2, 2012 at 5:43 Comment(0)
L
2

In CSS3 you can also use the new box-sizing property to include the pixel and padding count into the width of the element:

box-sizing: border-box;
Lifesaving answered 10/7, 2014 at 16:43 Comment(0)
A
0

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.

Amputate answered 15/9, 2013 at 19:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.