【问题标题】:CSS: how to set the width of form control so they all have the same width?CSS:如何设置表单控件的宽度,使它们都具有相同的宽度?
【发布时间】:2010-05-22 10:03:22
【问题描述】:

考虑以下示例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <style type="text/css">
            div { width: 15em }
            input, textarea, select { width: 100%;
                -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box }
        </style>
    </head>
    <body>
        <form>
            <div>
                <input value="Input">
            </div>
            <div>
                <textarea>Text area</textarea>
            </div>
            <div>
                <select>
                    <option>One</option>
                    <option>Two</option>
                    <option>Three</option>
                </select>
            </div>
        </form>
    </body>
</html>

在支持 border-box 框大小的浏览器上,这是按照我的意愿呈现的:

Correct rendering http://img.skitch.com/20100522-c75mhdut2q32yc7u5r2tkft1n4.png

然而,在 IE 6/7 上,它呈现为:

IE 6/7 rendering http://img.skitch.com/20100522-f5pkgnwwceaak3t8fqq2w16gfm.png

如何在 IE 6/7 中获得与在其他浏览器中相同的渲染效果,而无需以像素为单位设置大小?

【问题讨论】:

  • 介意我建议将此问题交叉发布到doctype.com 吗?

标签: css internet-explorer border-box


【解决方案1】:

解决方案是使用 CSS 和 JavaScript 来替换浏览器供应商合谋对样式造成痛苦的表单控件。选择只是带有 onmouseup 事件的下拉菜单。 JS 驱动的文本编辑器(丰富而简单)可以替换在线文本区域。甚至有专门用于此目的的库。 (Example)

【讨论】:

    【解决方案2】:

    这在 CSS 中是不可能的。我做了一些研究,发现在here 之前提出了同样的问题。解决方案是使用此boxsizing.htc 文件并将以下行添加到您的 HTML 头部:

    <!--[if lt IE 8]><style>input, textarea { behavior: url("boxsizing.htc"); }</style><![endif]-->
    

    【讨论】:

      【解决方案3】:

      一个快速的解决方法是使用类似select { *width:102.5%; _width:102.5%; } 的东西来破解 IE6/7 浏览器,但如果太宽,它可能无法与像素对齐。

      【讨论】:

      • 问题在于,它会将您锁定在给定的字体大小上。假设我添加了一个select { *width:102.5% }。完美,现在表单域具有相同的宽度:img.skitch.com/20100522-ght27dws54jqp8r79bf5nf1hus.png。但现在添加一个body { font-size: 200% }。使用相同的select { *width:102.5% },您会得到:img.skitch.com/20100522-ksie3bm45e83g314869fdhby29.png。因此,您输入select { *width:102.5% } 的百分比将取决于字体大小。随着字体大小的变化,您需要更改该百分比。而不是这个,我更喜欢以像素为单位的宽度。
      【解决方案4】:

      也许这可以帮助你:CSS3 support for Internet Explorer 6, 7, and 8。从页面:

      IE-CSS3 是一个脚本提供 Internet Explorer 支持几个 流行的新款式 即将推出的 CSS3 标准。

      【讨论】:

      • 很遗憾,这个htc 不支持box-sizing
      猜你喜欢
      • 1970-01-01
      • 2021-07-02
      • 2013-09-18
      • 2013-08-24
      • 2011-01-07
      • 2011-12-30
      • 1970-01-01
      • 1970-01-01
      • 2015-11-13
      相关资源
      最近更新 更多