【问题标题】:How to make input and select to be same width in css如何在css中输入和选择相同的宽度
【发布时间】:2014-05-04 13:08:36
【问题描述】:

在我的页面中,有几个inputselect 标签。

我用width:100%;定义它们的宽度,但是浏览器中的宽度不是100%

我在chrome中使用了调试工具,发现还应用了useragent样式表样式。

如何使宽度为 100%?

【问题讨论】:

  • 您在用户代理样式表中发现了什么?
  • 我发现输入和选择标签的边框值不同。
  • 如何重置输入和选择的用户代理样式表。
  • 参考css-reset!

标签: html css select input tags


【解决方案1】:

尝试使用select标签的width CSS属性如下:

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

输入标签也一样

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

您也可以在 css 中使用样式作为输入标签。

【讨论】:

    【解决方案2】:

    要覆盖用户代理样式表,请在开始时在您的代码中使用 reset.css

       /* 
          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;
    }
    

    如果您不想覆盖用户代理,那么您可以在 'px' 中获取宽度值

    【讨论】:

      【解决方案3】:

      Demo Fiddle

      理想情况下,您应该将样式与内容分开,因此在您的 CSS 中包括:

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

      注意。 demo fiddle without box-sizing set

      并且您需要使用box-sizing:border-box 来调整大小以考虑任何特定于浏览器的或设置边距/填充/边框。 Here's a handy read on the subject.

      Box-Sizing on MDN:

      box-sizing CSS 属性用于改变默认的 CSS 盒子模型 用于计算元素的宽度和高度。有可能 使用此属性来模拟不支持的浏览器的行为 正确支持 CSS 盒子模型规范。

      边框

      宽度和高度属性包括内边距和边框,但是 不是边距。这是 Internet Explorer 使用的盒子模型 文档处于 Quirks 模式。

      【讨论】:

      • box-sizing:border-box 拯救了这一天
      【解决方案4】:

      或者使用下面的css:

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

      【讨论】:

      • 我试过这个。但是输入和选择的宽度不同。
      猜你喜欢
      • 1970-01-01
      • 2013-10-27
      • 2018-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多