Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why padding is included in height sometimes?

Tags:

css

padding

I have a text area and I added some CSS to it

<textarea watermark="Have a question? ..." class="test-input" rows="4" cols="15" name="">Have a question? ...</textarea> 

and CSS applied to it -

.test-input {     border: 1px solid #D0D0D0;     border-radius: 3px 3px 3px 3px;     color: #646464;     float: left;     font-family: Arial;     font-size: 12px;     height: 20px;     margin: 0 0 0 15px;     padding: 5px;     resize: none;     width: 264px; } 

And I get the text area with some padding inside it. In the cases when it works absolutely fine, text area height comes to be 20px with 5px padding not included in height. But, in few cases height of the text area includes padding and the height gets reduced to 8px. I have looked for any css if its overriding it but I didn't find. And I compared the result in both cases. Left is the reduced height and right is the expected height.

enter image description here I can fix this issue, in other case managing height specifically, adding !important or with help of some JavaScript. But, I am wondering what's the cause here that's making such effect. Why and in which cases paddings are getting included with height or width?

like image 926
Ashmah Avatar asked Mar 11 '13 10:03

Ashmah


People also ask

Is padding included in height?

The width and height properties include the content, but does not include the padding, border, or margin.

Why padding is added to width?

Padding is used to create space around an element's content, inside of any defined borders. This element has a padding of 70px.

Is padding included in max-width?

This maximum width does not include padding, borders, or margins. An element to which a max-width is applied will never be wider than the value specified even if the width property is set to be wider.

How do I prevent the padding property from changing width or height in CSS?

to fix this, you simply need to update the box-sizing parameter and set this to border-box in your css. Or you can do this for all elements by simply adding the following. This tells the browser to account for any border and padding in the values you specify for an element's width and height.


2 Answers

That depends on what box-sizing attribute you have used:

  • border-box means that the height and width of the box, defined/calculated in CSS, will also include the padding(s) and border width(s) applied to it
  • content-box is the default behavior, where padding(s) and border width(s) are added onto the defined/calculated height and width of the box.

By setting box-sizing: border-box as seen in your left example, you have defined the height of the element at 20px. This means that the actual content box will only be 8px tall, because the browser will subtract the border (1px top, 1px bottom) and padding (5px top, 5px bottom) form the defined height, leaving only 8px left, which is a tad bit too short to contain height of the entire line (therefore the word appears to be cut off).

like image 81
Terry Avatar answered Sep 22 '22 12:09

Terry


jQuery seems consistent with these definitions:

  • .height() does not include padding (even where box-sizing: border-box)
  • .innerHeight() includes padding but not border
  • .outerHeight() includes padding and border
  • .outerHeight(true) includes padding and border and margin

Each can set or get, e.g. $element.outerHeight(desired_height_including_margins, true);

enter image description here

enter image description here

enter image description here

(Images excerpted from linked pages.)

like image 35
Bob Stein Avatar answered Sep 23 '22 12:09

Bob Stein