html - CSS:根据 parent 的宽度调整div宽度

标签 html css css-float fluid-layout

好的,我基本上是在构建一个流畅的布局。 我的 HTML 是这样的:

<div id="container">
    <div class="box" id="left">Left</div>
    <div class="box" id="center">This text is long and can get longer</div>
    <div class="box" id="right">Right</div>
    <div class="clear"></div>
</div>

这是CSS:

#container{
    width: 100%;
}
.box{
    float: left;
}
#left, #right{
    width: 100px;
}
#center{
    width: auto; /* ? */
    overflow: hidden;
}
.clear{
    clear:both;
}

我需要知道的是,当 #container 重新调整大小时,如何让 #center 重新调整大小,而不让元素在彼此下方移动。

最佳答案

尝试这些更正(只是简单的 float 元素,不需要设置绝对元素或填充)

just added a new fiddle

<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="utf-8">
<title>fluid layout</title>
<style>
    /*class to set the width of the columns */
    .floatbox{
        width:100px;
    }

    #container{
        width: 100%;
        float:left;
    }
    #left{
        float:left;
    }
    #right{
        float:right;
    }
    #center{
        overflow: hidden;
    }
    .clear{
        clear:both;
    }
</style>
</head>
<body>
    <div id="container">
        <!-- floating column to the right, it must be placed BEFORE the left one -->
        <div class="floatbox" id="right">Right</div>
        <div class="floatbox" id="left">Left</div>

        <!-- central column, it takes automatically the remaining width, no need to declare further css rules -->
        <div id="center">This text is long and can get longer</div>

        <!-- footer, beneath everything, css is ok -->
        <div class="clear"></div>
    </div>
</body>
</html>

关于html - CSS:根据 parent 的宽度调整div宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4439710/

相关文章:

html - 无序列表 Clearfix

javascript - 使用第三方 IME 时,JS keydown keyup keypress 事件在 IOS8 上不起作用

html - 如何让背景图片保持纵横比?

c# - 如何使用控制代码 (ASP.NET) 保留特定于控制的 css

javascript - jQuery 错误 - $(el).css ('float' ) 绝对或固定时 - 不同的浏览器返回不同的结果

css - 水平居中包含 float li 元素的 <ul> 导航栏

javascript - 如何在javascript函数中弹出php(与html标签混合)文件

Tomcat 中的 HTML Href

javascript - 避免向页面提交太多

jquery - 如何停止使用 jquery 重复?