html - "float"和 "width:auto"属性冲突

标签 html css

我有这个例子:http://jsfiddle.net/DRW2S/

#con{
    width:auto;
    background-color:red;
}
#inp{
    width:auto;
    max-width:525px;
    height:26px;
    float:right;
}
#btn{
    float:right;
    width:30px;
    background-color:yellow;
}
<div id="con">
    <input type="text" id="inp" />
    <div id="btn"> hi </div>
    <div style="clear:both;"></div>
</div>
​

如您所见,我已经使用 width:auto 属性设置了“inp”,但感觉根本不起作用。我注意到 float 属性在这种情况下有所作为。为什么它不起作用,我该如何解决?

最佳答案

您应该在 float 元素上设置宽度。看这个:

You should always set a width on floated items (except if applied directly to an image - which has implicit width). W3C's Cascading Style Sheets, level 2, CSS2 Specifications states:

"A floated box must have an explicit width..."

http://css.maxdesign.com.au/floatutorial/introduction.htm

虽然点击规范链接,但我看不到那里引用的句子..

尝试在 float 上设置像素或百分比宽度。

关于html - "float"和 "width:auto"属性冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12877806/

相关文章:

html - 使用 CSS 在子菜单下拉菜单下创建子菜单

php - 通过我们导航的链接更改元素的类

javascript - 创建 'modal box'时无法设置时间

html - 如何为新的 Ionic "tabs"应用程序添加非选项卡式登录页面?

javascript - 如何在 HTML 中淡入/淡出背景音乐(无 Flash)?

javascript - 嵌入 iframe 的 Three.js 在 iOS 8/9 中不断增长

html - 如何让 SVG 多段线以百分比表示?

html - 需要将父文档的 css 获取到 iframe

html - 中心列表项

html - 为什么谷歌浏览器会在我的 H1 标签中添加神秘的边距?