Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set div outerwidth using css

Tags:

css

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

like image 523
Omid Avatar asked Jun 06 '12 15:06

Omid


People also ask

What is the difference between width () and outerWidth ()? *?

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.

What is outer width in CSS?

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.

How do I get to the outerWidth element?

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).

What is outerWidth in javascript?

The outerWidth property returns the outer width of the browser window, including all interface elements (like toolbars/scrollbars).


2 Answers

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

like image 154
thirtydot Avatar answered Sep 20 '22 14:09

thirtydot


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).

like image 40
Mr. TA Avatar answered Sep 19 '22 14:09

Mr. TA