Div Background

Last post 02-04-2008 3:54 PM by adamargyle. 3 replies.

Sort Posts:

  • Div Background

    02-04-2008, 11:22 AM
    • Loading...
    • adamargyle
    • Joined on 12-13-2007, 6:45 AM
    • Posts 41
    I have a div element that I wish to have a white background for all content contained within it:

    #main {

    width:666px;

    margin:20px 10px 10px 2px;

    background:#ffffff;padding:0px 0px 10px 10px;

    }

    This works when entering text as the white background extends for all the content, however I have a problem (in firefox) when I put a table within the div the background does not extent down behind the table, is there something I need to add so it does.

    HTML:

    <div id="main">

     

     

    <table border="1" cellspacing="0" cellpadding="0" align="left" width="537">

    <tr>

    <td valign="top">Top Questions</td>

    </tr>

    <tr>

    <td valign="top" align="left">

    <table border="0" cellspacing="0" cellpadding="0">

    <tr>

    <td valign="top">

    <div class="faqText">

    <div onClick="expandcontent(this, 'q1')" class="faqQuestion"><span class="showstate"></span><img src="/images/clr_spcr.gif" width="4" height="1" border="0">My order has shipped but I still can't track it. How can I find out where it is?</div>

    <div id="topquestions01" class="switchcontent">Tracking information may not be available for the first 24 to 48 hours after you receive your shipping confirmation email.<br><br></div>

    </div>

    </td>

    </tr>

    </table>

    </td>

    </tr>

     

    </table>

     

    </div><!-- close #main -->

     

  • Re: Div Background

    02-04-2008, 12:01 PM
    Answer

    It's because divs, by default, don't trigger "layout".  They aren't intended to contain anything unless we tell them to.  In contrast, tables always trigger layout.  i.e., divs react to their context, while tables react to their content.

    Add    position: absolute;    to your #main style.

    See here for more information on the topic.

    When you ask a question, remember to click "mark as answered" when you get a reply which answers your question.


    My latest ASP.NET AJAX blog entries.
  • Re: Div Background

    02-04-2008, 1:13 PM
    • Loading...
    • bpw
    • Joined on 09-25-2004, 5:57 AM
    • Warrington, England
    • Posts 356

    If you didn't have the tables, the problem might go away: 

        <div id="main">
           <div>Top Questions
            </div>
            <div class="faqText">
              <div onClick="expandcontent(this, 'q1')" class="faqQuestion">
                <span class="showstate">
                </span>
                <img src="/images/clr_spcr.gif" width="4" height="1" border="0">My order has...
              </div>
              <div id="topquestions01" class="switchcontent">Tracking information may not be...<br><br>
              </div>
           </div>
        </div><!-- close #main -->

     If you need to keep the tables, an alternative to position: absolute (which can be tricky) is to add:

    <div style="clear:both"></div>
     Directly below the outer table.
    Paul Weston
  • Re: Div Background

    02-04-2008, 3:54 PM
    • Loading...
    • adamargyle
    • Joined on 12-13-2007, 6:45 AM
    • Posts 41

    by positioning absolute the issue is resolved

    thanks

    Adam

Page 1 of 1 (4 items)
Microsoft Communities
Page view counter