你能在div中制作浮动元素吗?

时间:2010-04-01 15:47:55

标签: html css

(目标浏览器是IE8)

我有一个包含左侧浮动元素列表的div。元素宽度可以在运行时更改。我想这样做,如果它们更适合div,它就会被切断而不会换成新的线。

如果项目本身就在一条线上似乎有用:

此页面演示了此问题: (文本输入的宽度需要在运行时更改)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title></title>
    <style type="text/css">
        div.aclb {background:#EEF3FA; color:#666; cursor:text; padding:1px; overflow-y:auto; border:#BBC8D9 1px solid; }
        div.aclb:hover {border:#3399FF 1px solid;}
        div.aclb.focus {background:#FFF; border:#3399FF 1px solid;}
        div.aclb ul {padding:0; margin:0; list-style:none; display:table; vertical-align:middle; }
        div.aclb li {float:left; cursor:default; font-family:Arial; padding:0; margin:0; height:18px;} /*Setting Height here is important. Seems li can have a height>18px on some browsers*/
        div.aclb li.block {padding:0px 2px; height:16px; white-space:nowrap; border:solid 1px #BBD8FB; background:#f3f7fd; font-size:11px; line-height:16px;}
        div.aclb li.block:hover {border:solid 1px #5F8BD3; background:#E4ECF8; color:#000;}

        div.aclb input {margin:0; padding:0; height:18px; background:transparent; border:none; color:#666; overflow:hidden; resize:none; font-family:Arial; font-size:13px; outline:none}
        div.aclb input:focus {margin:0; padding:0; height:18px; background:transparent; border:none; color:#22F; overflow:hidden; resize:none; font-family:Arial; font-size:13px; outline:none;}

        div.aclb a.d {cursor:pointer; display:block; color:#6B6B6B; width:13px; height:12px;float:right; margin:1px 0 1px 4px; border:solid 1px transparent; font-family:Verdana; font-size:11px; text-align:center;  line-height:10px;}
        div.aclb a.d:hover { border:solid 1px #7DA2CE; background:#F7FAFD; color:#AD0B0B;}
        div.aclb a.d:active {border:solid 1px #497CBB; background:#BAD8E8; color:#A90909;}
    </style>
</head>
<body>

<div style="width:250px" class="aclb">
    <ul>
        <li class="block">
            <a class="d">x</a><span>Item 1</span>
        </li>
        <li class="input">
            <input type="text" style="width:300px" maxlength="30"/>
        </li>
    </ul>
</div>

</body>

</html>

4 个答案:

答案 0 :(得分:3)

您可以通过在浮动的块周围添加另一个块元素来轻松实现,高宽度和溢出设置为隐藏。此外,您需要将overflow:hidden设置为原始父容器。

请注意,如果浮动元素的宽度超过上面提到的新块元素的宽度,它们将再次换行。所以我建议给它一个很高的价值。

编辑:我看到你有一个带有ul和一些里面的div。所以你不必添加一个新元素,因为ul也是一个块元素,它已经存在了。

以下是您的代码,其中包含一些更正:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title></title>
    <style type="text/css">
        div.aclb {background:#EEF3FA; color:#666; cursor:text; padding:1px; overflow-y:auto; border:#BBC8D9 1px solid; }
        div.aclb:hover {border:#3399FF 1px solid;}
        div.aclb.focus {background:#FFF; border:#3399FF 1px solid;}
        div.aclb ul {padding:0; margin:0; list-style:none; display:table; vertical-align:middle; }
        div.aclb li {float:left; cursor:default; font-family:Arial; padding:0; margin:0; height:18px;} /*Setting Height here is important. Seems li can have a height>18px on some browsers*/
        div.aclb li.block {padding:0px 2px; height:16px; white-space:nowrap; border:solid 1px #BBD8FB; background:#f3f7fd; font-size:11px; line-height:16px;}
        div.aclb li.block:hover {border:solid 1px #5F8BD3; background:#E4ECF8; color:#000;}

        div.aclb input {margin:0; padding:0; height:18px; background:transparent; border:none; color:#666; overflow:hidden; resize:none; font-family:Arial; font-size:13px; outline:none}
        div.aclb input:focus {margin:0; padding:0; height:18px; background:transparent; border:none; color:#22F; overflow:hidden; resize:none; font-family:Arial; font-size:13px; outline:none;}

        div.aclb a.d {cursor:pointer; display:block; color:#6B6B6B; width:13px; height:12px;float:right; margin:1px 0 1px 4px; border:solid 1px transparent; font-family:Verdana; font-size:11px; text-align:center;  line-height:10px;}
        div.aclb a.d:hover { border:solid 1px #7DA2CE; background:#F7FAFD; color:#AD0B0B;}
        div.aclb a.d:active {border:solid 1px #497CBB; background:#BAD8E8; color:#A90909;}
    </style>
</head>
<body>

<div style="width:250px; overflow: hidden;" class="aclb">
    <ul style="width: 1000px; overflow: hidden;">
        <li class="block">
            <a class="d">x</a><span>Item 1</span>
        </li>
        <li class="input">
            <input type="text" style="width:300px" maxlength="30"/>
        </li>
    </ul>
</div>

</body>

</html>

我希望它对你有用。 :)

答案 1 :(得分:1)

溢出:隐藏在父容器上应该可以解决问题。

答案 2 :(得分:1)

你能用“nowrap”吗?可能只是为了文字提醒你

答案 3 :(得分:1)

好像你正在寻找这种行为,不是吗?

div.aclb {
    overflow: hidden;
}
div.aclb ul {
    white-space: nowrap;
}
div.aclb li {
    display: inline;
}