Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

div background color not working with inputs floated left

Tags:

html

css

I need the following div to have a background color - simple right. But, it doesn't work correctly. I've done some testing, and figured out its the floats on the spans that are screwing it up. So, how do i fix this?

.days{
background-color:#000;
}

span {
display:block;
width:200px;
float:left; 
}

<div class="days">
        <span>  
        <input id="Field9" name="Field9" type="checkbox"  value=""   size="3" maxlength="4" tabindex="4"  />
        <label class="days" >Monday</label>
        </span> <span>
        <input id="Field5" name="Field10" type="checkbox"  value="" size="3" maxlength="4" tabindex="4"    />
        <label   class="days" >Tuesday</label>
        </span> <span>
        <input id="Field5" name="Field11" type="checkbox"  value="" size="3" maxlength="4" tabindex="4"    />
        <label   class="days"  >Wednesday</label>
        </span> <span>  
    <!-- goes till sunday --!>
</div>
like image 521
dgamma3 Avatar asked Feb 14 '11 03:02

dgamma3


People also ask

How do I float a div to the left?

Use CSS property to set the height and width of div and use display property to place div in side-by-side format. float:left; This property is used for those elements(div) that will float on left side. float:right; This property is used for those elements(div) that will float on right side.

What happens if you use a float property on an absolutely positioned element?

Absolutely positioned page elements will not affect the position of other elements and other elements will not affect them, whether they touch each other or not. There are four valid values for the float property. Left and Right float elements those directions respectively.

Why is my div background image not showing?

Assuming that your . png file actually exists, try setting the background size and repeat tags. If that doesn't work, try checking in your browser's developer tools for the response codes and making sure that the url is correct.


1 Answers

When you float elements, you take them out of the normal flow of the document, and as a result, other non-floated elements do not make room for them.

What you're observing is that your div no longer takes the full height of its child elements because they're floated. All you need to do is to "clear" or undo the floating, and make days take the full height, and its background color will show. My preferred way of clearing floats is to give your containing element overflow: hidden:

.days
{
    background-color:#000;
    overflow: hidden;
}

For more info about clearing floats, check out the Quirksmode.org article about it, which includes an explanation of the overflow: hidden method.

like image 125
wsanville Avatar answered Sep 30 '22 18:09

wsanville