【问题标题】:Horizontally Aligning DIVs Within DIVs - IE Not Working在 DIV 中水平对齐 DIV - IE 不工作
【发布时间】:2013-10-16 08:23:12
【问题描述】:

我在尝试水平对齐一个 DIV 中的 3 个 DIV 时遇到了一些麻烦(这不是我所知道的最复杂的任务)。

我已经设法让这个在 FireFox 中工作,但由于某种原因,在 IE 中,每个 DIV 似乎都显示在主 DIV 中的新行上。

FF 中的示例输出(按预期/要求):

DIV1 DIV2 DIV3

IE 中的示例输出:

DIV1

DIV2

DIV3

我已将我的小提琴附在此处:http://jsfiddle.net/oampz/u6bWR/1/

奇怪的是,在我的 jsFiddle 上它在 IE 和 FireFox 中看起来很好,但在我的应用程序中,使用完全相同的代码,我遇到了上述问题。

HTML:

<div class="ctr-full">
<div class="wrap">
    <div class="grid-wrap">
        <div class="grid one-whole">
            <ul class="list">
                <li>
                    <div class="toggle_head toggleInputbackground" style="position: relative;">
                        <div class="one-fiftieths" style="display: inline-block; vertical-align: middle; padding-top:10px;">
                            <img src="/images/expand.png" border="0" alt="Expand" class="expand"></img>
                            <img src="/images/collapse.png" border="0" alt="Collapse" class="collapse"></img>
                        </div>
                        <div class="nineteen-twentieths" style="display: inline-block; vertical-align: middle; padding-top:10px;">
                            <label>Step 1 > Enter Details</label>
                        </div>
                        <div style="display: inline-block; vertical-align: middle; padding-top:10px;">
                            <label>?</label>
                        </div>
                    </div>
                    <div class="toggle_body">
                        <ul>
                            <li>
                                <label>text text text.</label>
                            </li>
                        </ul>
                    </div>
                </li>
            </ul>
        </div>
    </div>
</div>
</div>

CSS:

.toggleInputbackground {
    background: #B8B8B8;
    background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #B8B8B8), color-stop(100%, #B8B8B8));
    background-image: -webkit-linear-gradient(#B8B8B8, #B8B8B8);
    background-image: -moz-linear-gradient(#B8B8B8, #B8B8B8);
    background-image: -o-linear-gradient(#B8B8B8, #B8B8B8);
    background-image: linear-gradient(#B8B8B8, #B8B8B8);
    color: #5C5C5C;
    height: 45px;
}
.nineteen-twentieths {
    width: 95%;
}
.one-fiftieths {
    width: 2%;
}
.one-whole {
    width: 100%;
}
.ctr-full {
    background: whitesmoke;
    padding: 12px 0;
    *zoom: 1;
}
.ctr-full:after {
    content:"";
    display: table;
    clear: both;
}
.wrap {
    width: 90%;
    margin: 0 auto;
    padding: 0 20px;
    max-width: 1440px;
    *zoom: 1;
}
.wrap:after {
    content:"";
    display: table;
    clear: both;
}
.grid-wrap {
    margin-left: -24px;
    *zoom: 1;
}
.grid-wrap:after {
    content:"";
    display: table;
    clear: both;
}
.grid {
    float: left;
    padding-left: 24px;
}
.list li, .list dt, .list dd {
    padding: 4px 0;
}

jQuery:

//hide the all of the element with class msg_body
$(".toggle_body").hide();
$(".collapse").hide();

//OnClick of a header componenet, toggle the visibility of all images which are direct children of .toggle_head
$(".toggle_head").click(function () {
    //alert("You clicked");
    var $this = $(this);
    $this.next(".toggle_body").slideToggle("slow", function () {
        $this.children('img').toggle();
    });
});

我希望它是我错过的一些简单的东西。

谢谢

更新(硬编码宽度:)

<div class="toggle_head toggleReportInputbackground one-whole" style="display: inline-block;">
<div style="display: inline-block; padding-top:10px; width:5%;">
    <img src="/images/expand.png" border="0" alt="Expand" class="expand"></img>
    <img src="/images/collapse.png" border="0" alt="Collapse" class="collapse"></img>
</div>
<div style="display: inline-block; padding-top:10px; width:80%;">
    <label class="boldText">Enter Details</label>   
</div>
<div style="display: inline-block; padding-top:10px; width:5%;">
    <label>?</label>
</div>
</div>

如您所见,我使用了 5%、80% 和 5% 的宽度,留出 10% 备用。我还从 3 个内容中删除了 vertical-align: middle; >divs 和 position: relative; 来自主/容器 div

我在这里约会了我的小提琴:http://jsfiddle.net/oampz/u6bWR/2/

再一次,它在 FF 中看起来很好。IE 仍然显示在新行上。

【问题讨论】:

    标签: html css


    【解决方案1】:

    尝试将float:left 分配给所有三个DIV。这可能会解决您的问题。

    【讨论】:

    • 不会的。它将重置中间垂直对齐方式。
    • 谢谢,我已经删除了 vertical-align: middle;因为这并没有真正添加任何东西。
    • float:left 工作,提供中间垂直对齐被移除。
    【解决方案2】:

    您为前两个 div(分别为 2% 和 95%)定义了 width,但没有为第三个定义。这意味着第三个 div 只剩下 3% 了。如果它的内容 (?) 比其父元素的 3% 宽,那么 div 将换行。您还应该考虑在inline-block 布局中成为问题的空格。我建议将width: 3% 添加到第三个 div 并重写您的 HTML 以删除空格:

    <div class="toggle_head toggleInputbackground" style="position: relative;">
        <div class="one-fiftieths" style="display: inline-block; vertical-align: middle; padding-top:10px;">
            <img src="/images/expand.png" border="0" alt="Expand" class="expand"></img>
            <img src="/images/collapse.png" border="0" alt="Collapse" class="collapse"></img>
        </div><div class="nineteen-twentieths" style="display: inline-block; vertical-align: middle; padding-top:10px;">
            <label>Step 1 > Enter Details</label>
        </div><div style="display: inline-block; vertical-align: middle; padding-top:10px; width: 3%;">
            <label>?</label>
        </div>
    </div>
    

    这是完整的小提琴:http://jsfiddle.net/CjXQZ/1/

    【讨论】:

    • 谢谢,我仍然面临一个问题。为我的主要问题添加了一个更新,标题为“更新(硬编码宽度:)”
    • 你是对的。我现在更新了。根据您问题中的编辑-您没有使用我的HTML。您可能没有注意到这里最重要的事情 - 您必须在结束标记 (
      ) 和开始标记 (没有空格。
  • 我在尝试访问新小提琴时收到错误消息。我认为这是因为您试图在我的帐户中/下保存新版本..
  • 出于兴趣,我在结束标签 (
    ) 和开始标签 (
  • 糟糕!你是对的。现在应该没问题了。根据空格-您将开始标签放在新行中,这也是 white-space.
  • 猜你喜欢
    • 2013-04-16
    • 2017-11-05
    • 1970-01-01
    • 2014-05-15
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 2010-09-07
    • 2019-08-25
    相关资源
    最近更新 更多