开发者

Keeping boxes in place when text is more than expected

In the following HTML page (also provided at: http://pastehtml.com/view/axetf58is.html) one of the lightblue boxes in the first half of the page above the pink boxes is running out and leaving a lot of space because of some extra text in the first box. The actual and expected page layout is at: http://pastehtml.com/view/axetwro10.html How do I make sure that all boxes lie properly in the grid.

Here is the HTML markup from the first link which is giving me problems.

<html>
    <head>
        <title>Page 7</title>

        <style type="text/css">
            body {
                width: 1000px;
                margin-left: auto;
                margin-right: auto;
            }

            #header {
                height: 200px;
                margin: 2px;
                margin-bottom: 10px;
                font-family: Arial,Helvetica,sans-serif;
                font-size: XX-large;
            }

            #first_half {
                min-width: 500px;
                min-height: 500px;
                margin-bottom: 20px;
                overflow: hidden;
            }

            #first_half div.summaries_box {
                float: left;
                margin-right: 10px
            }

            #para_right {
                float: right;
                width: 350px;
                background-color: gray;
            }

            #second_half {
                clear: both;
            }

            #second_half div.summaries_box {
                float: left;
                margin-left: auto;
                margin-right: auto;
                margin-top: 20px;
            }

            div.summary {
                width: 260px;
                margin: 5px;
                float: left;
                background: lightblue;
            }

            h3.summary_header {
                font-weight: bold;
                font-variant: small-caps;
            }

            #snippets_horizontal {
                width: 90%;
                margin-left: auto;
                margin-right: auto;
                overflow: hidden;
            }

            #snippets_horizontal .snippet {
                float: left;
                width: 45%;
                margin-right: 5%;
                font-size: small;
                background-color: lightpink;
            }
        </style>
    </head>

    <body>
        <div id="header">
            <p>Lorem ipsum dolor sit amet</p>
        </div>

        <div id="first_half">
            <div class="summaries_box" style="width: 600px;">
                <div class="summary">
                    <h3 class="summary_header">Lorem ipsum dolor sit amet</h3>
                    <ul class="summary_list">
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet</p>&l开发者_StackOverflow中文版t;/li>
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                    </ul>
                </div>

                <div class="summary">
                    <h3 class="summary_header">Lorem ipsum dolor sit amet</h3>
                    <ul class="summary_list">
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                    </ul>
                </div>

                <div class="summary">
                    <h3 class="summary_header">Lorem ipsum dolor sit amet</h3>
                    <ul class="summary_list">
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                    </ul>
                </div>

                <div class="summary">
                    <h3 class="summary_header">Lorem ipsum dolor sit amet</h3>
                    <ul class="summary_list">
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                    </ul>
                </div>
            </div>

            <div id="para_right" class="para">
                <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
                incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
                exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
                irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
                pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
                deserunt mollit anim id est laborum.
                Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
                incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
                exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
                irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
                pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
                deserunt mollit anim id est laborum.</p>
            </div>
        </div>

        <div id="second_half">
            <div id="snippets_horizontal">
                <div class ="snippet">
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
                        incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud</p>
                </div>

                <div class ="snippet">
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
                        incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud</p>
                </div>
            </div>

            <div class="summaries_box" style="clear: both;">
                <div class="summary">
                    <h3 class="summary_header">Lorem ipsum dolor sit amet</h3>
                    <ul class="summary_list">
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                    </ul>
                </div>

                <div class="summary">
                    <h3 class="summary_header">Lorem ipsum dolor sit amet</h3>
                    <ul class="summary_list">
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                    </ul>
                </div>

                <div class="summary">
                    <h3 class="summary_header">Lorem ipsum dolor sit amet</h3>
                    <ul class="summary_list">
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                        <li class="summary_title_snip"><p>Lorem ipsum dolor sit amet</p></li>
                    </ul>
                </div>
            </div>
        </div>

    </body>
</html>


Give it a fixed height and tell the browser to introduce a scrollbar when there is too much content.

div.summary {
    height: 200px;
    overflow: auto;
}

jsFiddle Demo

Your layout breaks because of the characteristics of floats. In this situation, Box 3 will try to float itself to the earliest possible fitting place, and because Box 1 is higher than Box 2, it will have some free space next to it. Box 3 tries to occupy that, not leaving enough space for Box 4 (which you think is Box 3 I guess), so it falls to the next row. You can also try clearing the floats on Box 3, so it will always go to the next row (depending how fixed your layout is, this might be a good solution as well).


If you don't want to use fixed heights with overflow: auto as kapa suggest, you could use display: inline-block and the vertical align property.

Link: Why You Should Use Inline-Block when Positioning Elements

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜