将 2 个不同大小的 div 置于主/父 div 中居中

发布于 2024-12-15 01:52:24 字数 920 浏览 3 评论 0原文

我有 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%;保证金:自动; } 如 如何水平居中

中所述在另一个
? 中,但一个菜单大于 50%,因此会被裁剪。小的悬挂在左侧 20 像素处。在原型期间,我们使用 jQuery 对其进行了破解,但这并不理想,因为菜单在加载后会跳转。

想法?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹏半生如梦愿梦如真 2024-12-22 01:52:24

可能的解决方案

彩色边框进行解释。
http://jsfiddle.net/jnG4q/

862px > 800px,所以它们不适合排成一排。
用于查看示例的较小菜单:
http://jsfiddle.net/jnG4q/1

需要 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>

附加 CSS

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

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

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

<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

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

.inner-menu {
    display: inline-block;
}
待天淡蓝洁白时 2024-12-22 01:52:24

我认为 Smamatti 有一个很好的解决方案。我想出了或多或少相同的东西,唯一的区别是我删除了所有内联样式。

http://jsfiddle.net/S1l3/YVdFq/4/

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>

CSS:

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

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

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:

<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:

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

.inner-menu
{
    display:inline-block;
    background:#ccc;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文