treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Div box problem

  • I need help with a div block problem.

    I want it to look like this:
    Correct Layout

    But instead, it displays like this:
    How it appears

    Here is the HTML code:
    <div id="Content">
    <div id="holidays">
    <h3>Upcoming City Holidays</h3>
    <p><span>Christmas</span><br />Sunday, Decemeber 25th</p>
    <p><span>Thanksgiving</span><br />Wednesday, Decemeber 25th</p>
    </div>
    <div id="announcements">
    <h3>Annoucements</h3>
    <div>
    <h4>New City Policy<br /><span>By: John Doe</span></h4>
    <h4>New City Policy<br /><span>By: John Doe</span></h4>
    <h4>New City Policy<br /><span>By: John Doe</span></h4>
    <h4>New City Policy<br /><span>By: John Doe</span></h4>
    <h4>New City Policy<br /><span>By: John Doe</span></h4>
    </div>
    </div>
    </div>


    This is the Style Sheet:

    #holidays, #announcements {
    float: left;
    width: 200px;
    border: 1px solid #000;
    padding: 0px;
    text-align: center;
    margin-right: 5px;
    height: 150px;
    overflow: hidden;
    }
    #holidays h3, #announcements h3 {
    background-color: #006;
    display: block;
    color: #FFF;
    text-align: center;
    padding-top: 2px;
    padding-bottom: 2px;
    font-size: 14px;
    margin-bottom: 20px;
    }
    #announcements {
    margin-right: 0px;
    text-align: left;
    width: 471px;
    }
    #announcements div {
    overflow: scroll;
    overflow-style: marquee-line;
    height: 100%;
    }
    #announcements h3 {
    margin-bottom: 0px;
    }
    #announcements h4 {
    margin-top: 10px;
    margin-bottom: 10px;
    margin-left: 10px;
    font-size: 14px;
    }
    #announcements span {
    font-style: italic;
    font-weight: normal;
    font-size: 12px;
    }
    #holidays p {
    padding: 0px;
    font-size: 12px;
    margin-top: 0px;
    font-style: italic;
    }
    #holidays span {
    font-weight: bold;
    font-style: normal;
    }
    #Content {
    width: 680px;
    padding-left: 60px;
    padding-right: 60px;
    padding-bottom: 5px;
    padding-top: 0px;
    background-image: url(../../layout/content_background.jpg);
    clear: both;
    margin-top: 0px;
    margin-right: auto;
    margin-bottom: 0px;
    margin-left: auto;
    }

  • #Content { overflow: hidden; }
  • Try adding this...

    <div style="clear: both;"></div>


    ...just above the closing tag of the "#Content" div.
  • why promote the use of extraneous divs? There are plenty of better ways to contain floats.
  • Looks like the overflow: hidden worked but why?

    The other problem I have now is the h3 tag is not flush to the containing div tag.
    h3 no flush
  • Never-mind, I figured it out. I needed to add a "marign-top: 0px" for the h3 tags. I believe it was because the box had no boundary to push against. Still curious why overflow: hidden worked though.
  • The overflow: hidden (or auto) is but one way to contain floats - its in the specs. It creates a new block formatting context, which will contain any floats. Another popular containment method is the clearfix method (some call it a hack, but its definitely not) which uses a pseudo class to add content after the float and clear it. This works similar to adding the extraneous div and clearing, but without the markup. There are other ways as well - no one way is perfect, so its best to know the various methods.