Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does display:block not stretch buttons or input elements

Tags:

I'm trying to understand the reason behind this problem:

What's the underlying reason behind <button> or <input> elements not behaving like other elements when set to display:block!

I'm not looking for workarounds to fix this problem, so please don't point me to this answer because it doesn't answer the question.

Here's a js-fiddle that illustrates the problem

Update 1: @Pete is correct, the default size attribute of an element is what sets the size even on block, as you can in this fiddle the size and cols attribute of <input> and <textarea> changes their width. That solves part of my question.

With that in mind, my question is now, why is the <button> element not behaving like other block elements? It's a mystery to me!

like image 306
Julian Krispel-Samsel Avatar asked May 16 '13 10:05

Julian Krispel-Samsel


People also ask

What happens with display block?

display: block An element that has the display property set to block starts on a new line and takes up the available screen width. You can specify the width and height properties for such elements. Examples of elements that are at block-level by default are <div> , <section> , <p> , and lots more.

What is the function of display block?

display: block means that the element is displayed as a block, as paragraphs and headers have always been. A block has some whitespace above and below it and tolerates no HTML elements next to it, except when ordered otherwise (by adding a float declaration to another element, for instance).

What does the display block property do to your navigation bar?

Vertical Navigation Bar display: block; - Displaying the links as block elements makes the whole link area clickable (not just the text), and it allows us to specify the width (and padding, margin, height, etc. if you want) width: 60px; - Block elements take up the full width available by default.

What is the difference between display block and display none?

display:none hides an element, and it will not take up any space. The element will be hidden, and the page will be displayed as if the element is not there display:block display property of an element only changes how the element is displayed, NOT what kind of element it is.


1 Answers

Button, Input and other form elements are actually replaced elements - see this answer: HTML5: Non-replaced vs. replaced element?

Additionally, button and input are inline elements. Thus, reading the MDN docs regarding visual formatting here: https://developer.mozilla.org/en-US/docs/Web/CSS/Visual_formatting_model, as well as the w3c docs here: https://www.w3.org/TR/CSS21/visudet.html#inline-replaced-width, you can conclude that for replaced inline elements:

If 'height' and 'width' both have computed values of 'auto' and the element also has an intrinsic width, then that intrinsic width is the used value of 'width'.

Therefore, button and input have an intrinsic width set to their content (or the size attribute on input, if used). That's why just specifying display: block doesn't do anything to the size of a button or input. You also have to override the intrinsic width of the elements.


Update: While researching more after answering this question, I found a much older answer which goes into much more detail about this same issue. You can find it here: https://stackoverflow.com/a/27605483/630170.

like image 189
kumarharsh Avatar answered Oct 14 '22 03:10

kumarharsh