Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS padding added to height/width for <input type='submit'>

Tags:

html

css

If this question has been asked, I apologize, but I couldn't find the exact same question. As W3schools says, The height property does not include padding, borders, or margins! (link). So here's a simple style declaration:

input {
  width:180px;
  height:18px;
  padding:7px;
  border:1px solid black;
}

and here's html:

<input type='text' name='text' value='194px by 32px' /><br  />
<input type='submit' name='button' id='button' value='180px by 18px' />

The text field is 194px by 32px as expected, but the submit button is 180px by 18px. Obviously, I can just increase the height & width of the button by twice the padding, but I want to understand why there's a discrepancy. I made a jsfiddle here: http://jsfiddle.net/RRf5A/. Thanks for your help. FYI, I already tried <button> instead of input submit, and display:inline-block.

like image 563
yitwail Avatar asked May 21 '11 03:05

yitwail


People also ask

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.

Does padding add to height?

The box model of an element in CSS—includes the content, padding, border, and margin areas. Any padding added to the element will increase the total computed height of the element, so you may not always end up with the expected height using just the height property if you also add padding to your element.

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.

How do I make input width fit content?

Use the span. text to fit width of text, and let the input have same size with it by position: absolute to the container.


1 Answers

You can get around this by adding box-sizing: content-box; to input elements of the type submit. Rather than go into detail myself I'll just send you along to http://www.quirksmode.org/css/box.html. However, it's not currently supported all across the board so you might find it better for cross-browser compatibility to specifically declare different heights and paddings for submission buttons.

like image 126
iamnotoriginal Avatar answered Oct 20 '22 19:10

iamnotoriginal