将 2 个不同大小的 div 置于主/父 div 中居中
我有 2 个菜单需要位于菜单栏的中心。需要在IE7、IE8、IE9、Chrome、FF中良好运行。出于示例目的,我根据镀铬测量添加了两者的宽度/高度。
大菜单:
<div id="outer-menu-bar" style="width:800px;height:32px;">
<div id="inner-menu" style="width:578px;height:32px;">Foo foo</div>
</div>
小菜单:
<div id="outer-menu-bar" style="width:800px;height:32px;">
<div id="inner-menu" style="width:285px;height:32px;">Foo foo</div>
</div>
我尝试将样式设置为 #inner { width: 50%;保证金:自动; } 如 如何水平居中
想法?
I have 2 menus that need to be centered on a menubar. Needs to run well in IE7,IE8,IE9, Chrome, FF. For example purposes, I've added the width/height for both based on chrome measurements.
big menu:
<div id="outer-menu-bar" style="width:800px;height:32px;">
<div id="inner-menu" style="width:578px;height:32px;">Foo foo</div>
</div>
small menu:
<div id="outer-menu-bar" style="width:800px;height:32px;">
<div id="inner-menu" style="width:285px;height:32px;">Foo foo</div>
</div>
I tried setting the style to #inner { width: 50%; margin: auto; } as described in How to horizontally center a <div> in another <div>? but one menu is bigger than 50% so it gets clipped. The small one hangs to the left 20 pixels. During prototype we hacked it with jQuery but this isn't desirable because the menu jumps after load.
Thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能的解决方案
彩色边框进行解释。
http://jsfiddle.net/jnG4q/
862px > 800px,所以它们不适合排成一排。
用于查看示例的较小菜单:
http://jsfiddle.net/jnG4q/1
需要 HTML
附加 CSS
Possible solution
Colored borders for explanation.
http://jsfiddle.net/jnG4q/
862px > 800px, so they don't fit in one row.
Smaller menus to view the sample:
http://jsfiddle.net/jnG4q/1
Wanted HTML
Additional CSS
我认为 Smamatti 有一个很好的解决方案。我想出了或多或少相同的东西,唯一的区别是我删除了所有内联样式。
http://jsfiddle.net/S1l3/YVdFq/4/
HTML:
CSS:
I think Smamatti has got a nice solution. I came up with more or less the same thing, the only difference is I removed all the inline styling.
http://jsfiddle.net/S1l3/YVdFq/4/
The HTML:
The CSS: