用显示输入:块不是一个块,为什么不呢?

时间:2009-06-23 05:46:03

标签: html css

为什么我的文字输入上的display:block;width:auto;行为不像div并填充容器宽度?

我的印象是div只是一个具有自动宽度的块元素。在下面的代码中,div和输入不应该具有相同的尺寸吗?

如何获取填充宽度的输入? 100%宽度不起作用,因为输入具有填充和边框(导致最终宽度为1像素+5像素+ 100%+ 5像素+ 1像素)。固定宽度不是一个选项,我正在寻找更灵活的东西。

我更愿意直接回答变通方法。这似乎是一个CSS怪癖,理解它可能会在以后有用。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <title>width:auto</title>

        <style>
        div, input {
            border: 1px solid red;
            height: 5px;
            padding: 5px;
        }
        input, form {
            display: block;
            width: auto;
        }
        </style>
    </head>

    <body>
        <div></div>
        <form>
            <input type="text" name="foo" />
        </form>
    </body>
</html>

我应该指出我已经可以使用包装器解决方法做到这一点。除了对页面语义和CSS选择器关系的这种搞砸之外,我试图理解问题的本质以及是否可以通过改变INPUT本身的性质来克服它。

好的,这很奇怪!我发现解决方案只是将max-width:100%添加到width:100%;padding:5px;的输入中。然而,这引发了更多问题(我将在另一个问题中提出),但似乎宽度使用普通的CSS框模型,max-width使用Internet Explorer边框模型。多奇怪啊。

好的,最后一个似乎是Firefox 3中的一个错误.Internet Explorer 8和Safari 4将最大宽度限制为100%+填充+边框,这是规范所要做的。最后,Internet Explorer做对了。

天哪,这真棒!在玩这个游戏的过程中,并且在古老的大师Dean EdwardsErik Arvidsson的帮助下,我设法拼凑出三个独立的解决方案,在任意元素上制作真正的跨浏览器100%宽度填充和边框。见下面的答案。此解决方案不需要任何额外的HTML标记,只需要一个类(或选择器)和旧版Internet Explorer的可选行为。

7 个答案:

答案 0 :(得分:128)

查看我提出的内容,使用CSS 3中相对未知的box-sizing:border-box样式的解决方案。这允许任何元素上的'真''100%宽度,而不管元素的填充和/或边框。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">

        <title>Cross-browser CSS box-sizing:border-box</title>

        <style type="text/css">
            form {display:block; margin:0; padding:0; width:50%; border:1px solid green; overflow:visible}
            div, input {display:block; border:1px solid red; padding:5px; width:100%; font:normal 12px Arial}

            /* The voodoo starts here */
            .bb {
                box-sizing: border-box; /* CSS 3 rec */
                -moz-box-sizing: border-box; /* Firefox 2 */
                -ms-box-sizing: border-box; /* Internet Explorer 8 */
                -webkit-box-sizing: border-box; /* Safari 3 */
                -khtml-box-sizing: border-box; /* Konqueror */
            }
        </style>

        <!-- The voodoo gets scary. Force Internet Explorer 6 and Internet Explorer 7 to support Internet Explorer 5's box model -->
        <!--[if lt IE 8]><style>.bb {behavior:url("boxsizing.htc");}</style><![endif]-->
    </head>

    <body>
        <form name="foo" action="#">
            <div class="bb">div</div>
            <input class="bb" size="20" name="bar" value="field">
        </form>
    </body>
</html>

此解决方案通过Erik Arvidsson编写的行为支持Internet Explorer 6和Internet Explorer 7,并对Dean Edwards进行了一些调整,以支持百分比和其他非像素宽度。

Working example
Behaviour (boxsizing.htc)

答案 1 :(得分:8)

发生这种情况的原因是文本输入的大小由其大小属性决定。在&lt; input&gt;内添加size =“50”标签。然后将其更改为size =“100” - 看看我的意思?

我怀疑有一个更好的解决方案,但我想到的唯一一个问题就是我在SO的“隐藏的HTML功能”问题上找到的东西:使用内容可编辑的div而不是输入。将用户输入传递给封闭表单(如果这是你需要的)可能有点棘手。

Hidden features of HTML

答案 2 :(得分:6)

你最好的选择是将输入包装在带有边框,边距等的div中,输入内部的宽度为100%,没有边框,没有边距等。

例如,

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <title>width:auto</title>

        <style>
            div {
                border: 1px solid red;
                padding: 5px;
            }
            input, form {
                display: block;
                width: 100%;
            }
        </style>
    </head>

    <body>
        <form>
            <div><input type="text" name="foo" /></div>
        </form>
    </body>
</html>

答案 3 :(得分:1)

你可以假装它,就像这样:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <title>width:auto</title>

        <style>
        div, #inputWrapper {
            border: 1px solid red;
        }
        #topDiv {
            padding: 5px;
            height: 5px;
        }
        form {
            display: block;
        }
        #inputWrapper {
            overflow: hidden;
            height: 15px;
        }
        input {
            display: block;
            width: 100%;
            border-style: none;
            padding-left: 5px;
            padding-right: 5px;
            height: 15px;
        }
        </style>
    </head>

    <body>
        <div id="topDiv"></div>
        <form>
          <div id="inputWrapper">
            <input type="text" name="foo" />
          </div>
        </form>
    </body>
</html>

答案 4 :(得分:1)

你也可以假装它,就像这样:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <title>width:auto</title>

        <style>

            .container {
                width:90%;
            }

            .container div{
                border: 1px solid red;
                padding: 5px;
                font-size:12px;
                width:100%;
            }

            input{
                  width:100%;
                border: 1px solid red;
                font-size:12px;
                padding: 5px;
            }

            form {

                margin:0px;
                padding:0px;
            }

        </style>
    </head>

    <body>
        <div class="container">
            <div>
                asdasd
            </div>
            <form action="">
                <input type="text" name="foo" />
            </form>
        </div>
    </body>
</html>

答案 5 :(得分:1)

试试这个:

form { padding-right: 12px; overflow: visible; }
input { display: block; width: 100%; }

答案 6 :(得分:0)

将此添加到您的样式中:

// too bizz bazz buzz bizz