html - How to style CSS for input box's width as a percentage when it has border and padding? -
If the container is 500 pix width wide, and the label is 30% wide, and the input box is 70% wide, then it is considered Everything should be fine, but the catch is that the border in the input box is left and right for 4px, left and right padding for 2px, it is composed of 6px wide.
We can hack it by making the width is not 70%, but 68% or something else, but it is somewhat hackneyed. Is there any way to handle it? (We can not reset the CSS, because the input box usually requires a border, assuming that we do not hack it using the outline, which is not in any place).
CSS:
# my-container {width: 500px} # First-name-label {display: inline-block; Width: 30%} # first-name {display: inline-block; The following are a few examples: - Outwidth: 356 (for input box, as shown in Debug Console)
- External width 34 9
- External width 349
- Do not wrap, but the input box will move from the container width
This is the reason that the width is not accurate because they are set to the box -Size: content-box; , which is the default setting for box-size . When width and height are calculated in css, then the range and padding are calculated outside of that It is width and height , even if it is width: 100%; Is set to . To oppose this, you simply limit the box-sizing: border-box; . It brings both the range and padding inside the calculation of width and height. Try this: # my-container {width: 500px; } #name-label {display: inline-block; Width: 30%; } # First-name {display: inline-block; Width: 70%; -WebKit-box-size: border-box; / * Safari, other webkit * / -moz-box-size: border-box; / * Firefox, Other Gecko * / Box Size: LimitBox; / * Opera / IE 8+ * /} Or if you want to be more specific for text input, try it out: input [type = text] {webkit-box-size: border-box; / * Safari, other webkit * / -moz-box-size: border-box; / * Firefox, Other Gecko * / Box Size: LimitBox; / * Opera / IE 8+ * / CSS syntax: box-size: content box | Border-box | Initial | the heir; Chrome (Any): Box Sizing Opera 8.5+: Box-Sizing Firefox (Any) : -Moz-Box-Sizing Safari 3: -WebKit-Box-Sizing (5.1 IE8 +: Box-sizing If you want to know more about this, you can find a very informative article on Box Sizing here: < P>
Comments
Post a Comment