使浮动 div 收缩而不是下降到新行
有什么方法可以强制浮动 div 缩小而不是换行吗?
我知道我可以在 div 上设置隐式宽度,但它位于菜单上,其中可能包含可变数量的项目。我正在尝试做到这一点,同时尽可能保持网站布局流畅。
#left {
float: left;
border: 1px solid #000;
}
#right {
float: right;
border: 1px solid #000;
}
<div id="left">
<p>This div represents the logo</p>
</div>
<div id="right">
<p>When the window's width is reduced and these divs touch I want this div to shrink instead of falling to the next line.
</p>
</div>
基本上,我希望 #right 在浏览器窗口缩小时开始缩小,而不是先放下一行,然后在进一步调整窗口大小时缩小。
Is there any way I can force a floated div to shrink instead of going to a new line?
I know I can set implicit widths on the divs but it's on a menu which might have variable amounts of items in it. I'm trying to do this while keeping the site's layout fluid if possible.
#left {
float: left;
border: 1px solid #000;
}
#right {
float: right;
border: 1px solid #000;
}
<div id="left">
<p>This div represents the logo</p>
</div>
<div id="right">
<p>When the window's width is reduced and these divs touch I want this div to shrink instead of falling to the next line.
</p>
</div>
Basically, I want #right to begin shrinking when the browser window is shrunk rather than having it drop a line first, then shrink when the window is further resized.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过赋予这两个 div 相对(例如百分比)宽度?
当您浮动而没有明确声明宽度(固定或相对)时,尺寸将默认为“自动”。 Auto 将强制 div 为其内容的宽度。当浏览器缩小时,内容仍然会强制这些框达到该宽度,直到通过触摸另一个元素而强制折叠。
使用自动宽度并不是实现布局流畅性的最佳方法。您需要在某处指定某种相对尺寸,否则这个问题将完全不可避免。
有很多资源可以帮助您实现更流畅的布局(www.alistapart.com 上的很多文章对此进行了相当深入的讨论)。
Have you tried experimenting with giving these two divs relative (such as a percentage) widths?
When you float without explicitly declaring a width, either fixed or relative, the dimensions will default to 'auto'. Auto will force the div to be the width of it's content. When the browser shrinks, the content will still force these boxes to that width, until it is forced to collapse by touching another element.
Using auto widths is not the best way to achieve fluidity in your layout. You'll need to specify some kind of relative dimension somewhere, otherwise this problem will be entirely unavoidable.
There are lots of resources out there which can help you achieve a more fluid layout (a lot of articles on www.alistapart.com discuss this in quite some depth).
CSS 的
display
属性,将其设置为inline< /code> 和
div
的行为类似于span
。CSS's
display
property, set it toinline
and thediv
will behave like aspan
.