button , like most form elements, is a replaced element. Substituted elements behave differently than ordinary irreplaceable elements (such as div ) when they are absolutely positioned. The following two points from section 10.3.8 of CSS2.1 apply:
- The used "width" value is defined as for inline replaced elements. If "margin-left" or "margin-right" is specified as "auto", its used value is determined by the rules below.
...
- If at this point the values ββare too limited, ignore the value for "left" (in case the "direction" property of the containing block is "rtl") or "right" (in case "direction" is 'ltr') and decide for of this value.
The width of the button is determined not on the basis of the indicated offsets, in contrast to unsigned elements, but on the contents of the button itself. Since the width value used is not automatic, and the left and right values ββare not automatic, the values ββare overloaded and the browser is forced to ignore right to respect width .
If you want the button to fill the entire height and width of the wrapper, do not use absolute positioning. Instead, specify 100% height and width on the button and use the registration on the wrapper to offset the button:
.wrapper { height: 300px; width: 300px; background-color: #f33; padding: 3px; box-sizing: border-box; } .wrapper button { height: 100%; width: 100%; }
<div class="wrapper"> <button>Hello world</button> </div>
(If you cannot use box-sizing , subtract the padding from the dimensions of the wrapper.)
The vertical alignment of the text is probably due to the way the browser controls the control buttons by default, which is usually based on the control buttons on the system.
source share