I wonder if there is way to set outerwidth
of a div
using css
to ignore padding and borders.
When I set a div
to be 50%
width, padding and border will be added to the width. How can I solve that without javascript
or jQuery.outerWidth()
?
Don't want to use an extra element
The width() method excludes padding, margin or border of the element. Whereas, the outerWidth() method (which too measures the element horizontally), includes padding, borders and (optionally) margins.
innerWidth is the width, in CSS pixels, of the browser window viewport including, if rendered, the vertical scrollbar. The Window. outerWidth is the width of the outside of the browser window including all the window chrome.
The outerWidth() method returns the outer width of the FIRST matched element. As the image below illustrates, this method includes padding and border. Tip: To include the margin, use outerWidth(true).
The outerWidth property returns the outer width of the browser window, including all interface elements (like toolbars/scrollbars).
I'm wonder if there is way to set outerwidth of a div using css to ignore padding and borders.
You can use box-sizing: border-box
to make padding
and border
be counted inside width
:
div {
box-sizing: border-box;
}
See: http://jsfiddle.net/thirtydot/6xx3h/
Browser support: http://caniuse.com/css3-boxsizing
The spec: http://www.w3.org/TR/css3-ui/#box-sizing
Nest another div
inside yours, and apply the paddings/borders to the nested one:
<div style="width:50%;"> <div style="padding:5px;"> .... </div> </div>
Unfortunately, there is no purely CSS way to achieve that (or at least I'm not aware of one).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With