简体   繁体   English

在主/父div中居中2个不同大小的div

[英]Centering 2 different sized divs within a master/parent div

I have 2 menus that need to be centered on a menubar. 我有2个菜单需要以菜单栏为中心。 Needs to run well in IE7,IE8,IE9, Chrome, FF. 需要在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%; 我尝试将样式设置为#inner {width:50%; margin: auto; 保证金:自动; } as described in How to horizontally center a <div> in another <div>? 如何在另一个<div>中水平居中<div>中所述? but one menu is bigger than 50% so it gets clipped. 但是一个菜单大于50%,因此会被剪裁。 The small one hangs to the left 20 pixels. 小的一个挂在左边20像素。 During prototype we hacked it with jQuery but this isn't desirable because the menu jumps after load. 在原型期间,我们用jQuery攻击它,但这是不可取的,因为菜单在加载后跳转。

Thoughts? 思考?

Possible solution 可能解决方案

Colored borders for explanation. 彩色边框解释。
http://jsfiddle.net/jnG4q/ http://jsfiddle.net/jnG4q/

862px > 800px, so they don't fit in one row. 862px> 800px,因此它们不适合一行。 Smaller menus to view the sample: 查看样本的较小菜单:
http://jsfiddle.net/jnG4q/1 http://jsfiddle.net/jnG4q/1

Wanted HTML 想要HTML

<div id="outer-menu-bar" style="width:800px;height:32px;">  
    <div class="inner-menu" style="width:578px;height:32px;">Foo foo</div>
    <div class="inner-menu" style="width:285px;height:32px;">Foo foo</div>
</div>

Additional CSS 额外的CSS

#outer-menu-bar {
    text-align: center;
}

.inner-menu {
    display: inline-block;
}

I think Smamatti has got a nice solution. 我认为Smamatti有一个很好的解决方案。 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/ http://jsfiddle.net/S1l3/YVdFq/4/

The HTML: HTML:

<div id="outer-menu-bar">  
     <div class="inner-menu" >Foo fooFoo fooFoo fooFoo fooFoo fooFoo fooFoo </div>
    <div class="inner-menu" >Bar barBar bar Barbar </div>
</div>

The CSS: CSS:

#outer-menu-bar
{
    text-align:center;
    background:#900;
}

.inner-menu
{
    display:inline-block;
    background:#ccc;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM