代码之家  ›  专栏  ›  技术社区  ›  SpliFF

带显示器的输入:块不是块,为什么不是?

  •  163
  • SpliFF  · 技术社区  · 17 年前

    为什么 display:block;width:auto; 在我的文本输入上不表现得像一个div并填充容器宽度?

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

    <!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;

    好吧,最后一个似乎是Firefox 3中的一个bug。InternetExplorer8和Safari4将最大宽度限制为100%+填充+边框,这是规范所说的。最后,InternetExplorer做对了。

    Dean Edwards Erik Arvidsson

    7 回复  |  直到 17 年前
        1
  •  134
  •   Peter Mortensen Pieter Jan Bonestroo    8 年前

    看看我想出了什么,一个使用相对未知的解决方案 box-sizing:border-box CSS3风格。这允许在任何元素上都有“真实”的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编写的行为支持InternetExplorer6和InternetExplorer7,并由Dean Edwards进行了一些调整,以支持百分比和其他非像素宽度。

    Working example
    Behaviour (boxsizing.htc)

        2
  •  10
  •   Community Mohan Dere    9 年前

    发生这种情况的原因是文本输入的大小由其大小属性决定。在<内添加size=“50”;输入>标签。然后把它改成size=“100”——明白我的意思了吗?

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

    Hidden features of HTML

        3
  •  10
  •   Peter Mortensen Pieter Jan Bonestroo    8 年前

    你最好的选择是将输入包裹在一个带有边框、边距等的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>
    
        4
  •  2
  •   Julian    6 年前

    将此添加到您的样式中:

    box-sizing: border-box;
    
        5
  •  1
  •   Jacob    17 年前

    你可以伪造它,就像这样:

    <!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>
    
        6
  •  1
  •   Mike    17 年前

    试试这个:

    form { padding-right: 12px; overflow: visible; }
    input { display: block; width: 100%; }
    
        7
  •  1
  •   Peter Mortensen Pieter Jan Bonestroo    8 年前

    你也可以伪造它,就像这样:

    <!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>