10

In my page, there are several input and select tags.

I use width:100%; to define their width, but the width in the browser is not 100%.

I used the debug tool in chrome, and found there is also useragent stylesheet styles applied.

How can I make the width 100%?

4
  • what have you found in the useragent stylesheet? Commented Mar 26, 2014 at 8:57
  • i found the input&select tags have different border value. Commented Mar 26, 2014 at 8:58
  • how can i reset the user agent stylesheet of input&select. Commented Mar 26, 2014 at 9:00
  • 2
    Refer to css-reset! Commented Mar 26, 2014 at 9:02

4 Answers 4

26

Demo Fiddle

You should ideally separate style from content, so in your CSS include:

input, select{
  width:100%;
  box-sizing:border-box;
}

nb. demo fiddle without box-sizing set

And you need to use box-sizing:border-box in order for sizing to take into account any browser specific or set margin/padding/borders. Here's a handy read on the subject.

Box-Sizing on MDN:

The box-sizing CSS property is used to alter the default CSS box model used to calculate widths and heights of elements. It is possible to use this property to emulate the behavior of browsers that do not correctly support the CSS box model specification.

border-box

The width and height properties include the padding and border, but not the margin. This is the box model used by Internet Explorer when the document is in Quirks mode.

Sign up to request clarification or add additional context in comments.

1 Comment

box-sizing:border-box saved the day
0

To override the user agent stylesheet use reset.css in your code at the start.

   /* 
      Reset .css
   */

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}
body {
    line-height: 1;
}
ol, ul {
    list-style: none;
}
blockquote, q {
    quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
    content: '';
    content: none;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}

If you do not want to override the useragent then you can go for width value in 'px'

Comments

-1

or use the following css:

input[type=text], select { 
    width: 100% //or whatever you want)
}

1 Comment

i have try this.But input&select's width are different.
-2

Try to use the width CSS property of select tag as follows:

<Select name="foo" width="100" style="width: 100px">
 <option>One</option>
 <option>Two</option>
 <option>Three</option>
 <option>Four</option>
</select>

So the same for the input tag

<input type="text" name="faa" value="test" style="width: 100px;" />

You can use the style in css as well for input tag.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.