嵌套浮动 div 导致外部 div 不增长

发布于 2024-10-01 17:26:47 字数 2124 浏览 1 评论 0原文

如果有人可以建议一个比 stackoverflow 更好的地方来解决 css 问题,请告诉我。

我有一个带有背景和边框的外部 div,然后我需要在彩色框中有两列。出于某种原因,当我将浮动 div 放置在外部 div 内时,外部 div 不会增长。

这是我的 HTML:

<div class="tip_box">
    <h3>Send</h3>
    <hr />
    <form id="email_form">

        <div class="three-columns">
            <div class="contact_form_input">
                <h6>Your Name</h6>
                <input type="text" name="name_text_box" class="form_input" id="name_text_box" />
            </div>
            <div class="contact_form_input">
              <h6>Your Email</h6>
              <input type="text" name="email_text_box" class="form_input" id="email_text_box" />
            </div>
        </div>
        <div class="three-columns">
            <div class="contact_form_input">
                <h6>Recipient Name</h6>
                <input type="text" name="name_text_box" class="form_input" id="Text1" />
            </div>
            <div class="contact_form_input">
              <h6>Recipient Email</h6>
              <input type="text" name="email_text_box" class="form_input" id="Text2" />
            </div>
        </div>

    </form>
</div>

<p>This is where your message will go. Anything you want, as long as you want. Make it personal; make the recipient know you care.</p>

这是我的 CSS:

.three-columns {
    width: 290px;
    float: left;
    margin-right: 45px;
}
.tip_box {
    padding: 20px;
    margin: 20px 0px;
    -moz-border-radius: 7px;
    -webkit-border-radius: 7px;
    -khtml-border-radius: 10px;
    border-radius: 7px;
    padding-left: 55px;
    background: #eee;
    font-style:italic;
    background: #eff7d9 url(../images/icons/tip.png) no-repeat scroll 10px 15px;
    border: 1px solid #b7db58;
    color: #5d791b;
}

屏幕截图:

http://dl.dropbox.com/u/2127038/ cssissue.png

If anyone can suggest a better place than stackoverflow for css questions please let me know.

I have an outer div with background and border and then I need to have two columns within the colored box. Some reason when I place the floating divs inside the outer div, the outer div does not grow.

Here is my HTML:

<div class="tip_box">
    <h3>Send</h3>
    <hr />
    <form id="email_form">

        <div class="three-columns">
            <div class="contact_form_input">
                <h6>Your Name</h6>
                <input type="text" name="name_text_box" class="form_input" id="name_text_box" />
            </div>
            <div class="contact_form_input">
              <h6>Your Email</h6>
              <input type="text" name="email_text_box" class="form_input" id="email_text_box" />
            </div>
        </div>
        <div class="three-columns">
            <div class="contact_form_input">
                <h6>Recipient Name</h6>
                <input type="text" name="name_text_box" class="form_input" id="Text1" />
            </div>
            <div class="contact_form_input">
              <h6>Recipient Email</h6>
              <input type="text" name="email_text_box" class="form_input" id="Text2" />
            </div>
        </div>

    </form>
</div>

<p>This is where your message will go. Anything you want, as long as you want. Make it personal; make the recipient know you care.</p>

Here is my CSS:

.three-columns {
    width: 290px;
    float: left;
    margin-right: 45px;
}
.tip_box {
    padding: 20px;
    margin: 20px 0px;
    -moz-border-radius: 7px;
    -webkit-border-radius: 7px;
    -khtml-border-radius: 10px;
    border-radius: 7px;
    padding-left: 55px;
    background: #eee;
    font-style:italic;
    background: #eff7d9 url(../images/icons/tip.png) no-repeat scroll 10px 15px;
    border: 1px solid #b7db58;
    color: #5d791b;
}

Screenshot:

http://dl.dropbox.com/u/2127038/cssissue.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

虐人心 2024-10-08 17:26:48

在这种情况下,我不会将 div 向左浮动,我会让它们显示:内联或内联块。

如果浏览器窗口缩小,您的 3 列将变成 2 列,然后变成 1 列。

In this case I wouldn't float the divs left, I would make them display: inline or inline-block.

Your 3 columns will turn into 2 columns, then 1 column if the browser window shrinks.

小草泠泠 2024-10-08 17:26:47

包含浮动块的非浮动块不会自动扩展,因为浮动块是在正常流之外(或者至少是在流之外)。纠正此问题的一种方法是将 overflow 属性指定为 hiddenauto

.tip-box { overflow: auto; }

有关更多信息,请参阅 quirksmode

Non-float blocks containing float blocks will not automatically expand, since float blocks are taken outside the normal flow (or at least specially outside the flow). One way to correct that is to specify the overflow property to hidden or auto.

.tip-box { overflow: auto; }

See quirksmode for more.

撧情箌佬 2024-10-08 17:26:47

之后添加以下 HTML:

<div class="clear"></div>

这是 CSS:

.clear{
clear:both;
}

它肯定会起作用。

Add following HTML after <div class="tip_box"></div>:

<div class="clear"></div>

Here is the CSS:

.clear{
clear:both;
}

It will surely work.

鹿! 2024-10-08 17:26:47
.tip_box { overflow:hidden; zoom:1; }

这会在 ie7+ 和其他浏览器中建立新的块格式化上下文,触发 ie6 中的 haslayout 以包含浮动

.tip_box { overflow:hidden; zoom:1; }

this establishes new block formatting context in ie7+ and other browsers, triggers haslayout in ie6 to contain floats

不必了 2024-10-08 17:26:47

您将需要通常所说的清除修复。在这种情况下,包含元素上的 overflow: hide 即可 - 请参阅: http://www.jsfiddle.net/yijian/zuNwH/2

.tip_box {
    overflow: hidden;
}

顺便说一句,您可能还想使用 label 元素而不是 h6 进行标记表单元素的标签,并使用列表而不是单独的 div 来包含每个 label - input 对,并减少 class 的数量您通过依赖 CSS 文件的更复杂的选择器来使用属性。

<li>
    <label for="recipient_email">Recipient Email</label>
    <input type="text" name="email_text_box" id="recipient_email" />
</li>

You're going to need what is commonly known as a clearfix. In this case a overflow: hidden on the containing element will do - see: http://www.jsfiddle.net/yijiang/zuNwH/2

.tip_box {
    overflow: hidden;
}

As an aside, you might also want to use label elements instead of h6 to markup labels for your form elements, and use a list instead of individual divs for containing each label - input pair, and reduce the amount of class attribute you use by relying on more complex selectors for your CSS file.

<li>
    <label for="recipient_email">Recipient Email</label>
    <input type="text" name="email_text_box" id="recipient_email" />
</li>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文