How to set border's thickness in percentages?
Border doesn't support percentage... but it's still possible...
As others have pointed to CSS specification, percentages aren't supported on borders:
'border-top-width',
'border-right-width',
'border-bottom-width',
'border-left-width'
Value: <border-width> | inherit
Initial: medium
Applies to: all elements
Inherited: no
Percentages: N/A
Media: visual
Computed value: absolute length; '0' if the border style is 'none' or 'hidden'
As you can see it says Percentages: N/A.
Non-scripted solution
You can simulate your percentage borders with a wrapper element where you would:
- set wrapper element's
background-color
to your desired border colour - set wrapper element's
padding
in percentages (because they're supported) - set your elements
background-color
to white (or whatever it needs to be)
This would somehow simulate your percentage borders. Here's an example of an element with 25% width side borders that uses this technique.
HTML used in the example
.faux-borders {
background-color: #f00;
padding: 1px 25%; /* set padding to simulate border */
}
.content {
background-color: #fff;
}
<div class="faux-borders">
<div class="content">
This is the element to have percentage borders.
</div>
</div>
Issue: You have to be aware that this will be much more complicated when your element has some complex background applied to it... Especially if that background is inherited from ancestor DOM hierarchy. But if your UI is simple enough, you can do it this way.
Scripted solution
@BoltClock mentioned scripted solution where you can programmaticaly calculate border width according to element size.
This is such an example with extremely simple script using jQuery.
var el = $(".content");
var w = el.width() / 4 | 0; // calculate & trim decimals
el.css("border-width", "1px " + w + "px");
.content { border: 1px solid #f00; }
<div class="content">
This is the element to have percentage borders.
</div>
But you have to be aware that you will have to adjust border width every time your container size changes (i.e. browser window resize). My first workaround with wrapper element seems much simpler because it will automatically adjust width in these situations.
The positive side of scripted solution is that it doesn't suffer from background problems mentioned in my previous non-scripted solution.
You can also use
border-left: 9vw solid #F5E5D6;
border-right: 9vw solid #F5E5D6;
OR
border: 9vw solid #F5E5D6;
So this is an older question, but for those still looking for an answer, the CSS property Box-Sizing is priceless here:
-webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
-moz-box-sizing: border-box; /* Firefox, other Gecko */
box-sizing: border-box;
It means that you can set the width of the Div to a percentage, and any border you add to the div will be included within that percentage. So, for example, the following would add the 1px border to the inside of the width of the div:
div { box-sizing:border-box; width:50%; border-right:1px solid #000; }
If you'd like more info: http://css-tricks.com/box-sizing/
You can use em for percentage instead of pixels,
Example:
border:10PX dotted #c1a9ff; /* In Pixels */
border:0.75em dotted #c1a9ff; /* Exact same as above in Percentage */
Percentage values are not applicable to border-width
in CSS. This is listed in the spec.
You will need to use JavaScript to calculate the percentage of the element's width or whatever length quantity you need, and apply the result in px
or similar to the element's borders.