views:

142

answers:

5
+1  Q: 

CSS floating issue

I know this is a srs beginner question, but here goes:

I am trying to get a box, with an icon in the top left corner, and all the content of that box will be to the right of the icon.

This is the code I am using:

<div class="statsbox float_left">
    <img src="images/chart_bar.png" class="float_left"> 
    <div class="float_left">
        <p class="statsbox_header"> User Uploaded Images </p>
        <ul class="statsbox">
            <li class="statsbox"> 122 uploaded images </li>
            <li class="statsbox"> 13 images pending approval </li>
            <li class="statsbox"> 15 anonymous images </li>
            <li class="statsbox"> 97 unique users </li>
        </ul>
    </div>
    <div class="clear"></div>
</div>
<div class="clear"></div>

it seems like it should work, but it turns into this:

it fails

the <div> tags are outlined.
You can see that the <div> containing the list overlaps the image for some reason.

Note: if the unordered list isn't in that div, then this works as expected. It's like the presence of bullets push it over further to the left.

Also note: this still doesn't work if I wrap the <img> in it's own div.

What am I doing wrong here? The float_left class is predictably just float: left, clear is just clear: both, and the other classes are not defined yet, in case that is relevant.

A: 

Did you try display:inline ?

c0mrade
Tried it on the `ul`, the `div` s and everything under `float_left`, it didn't seem to change anything
Carson Myers
A: 

the 2nd inner div needs a width, or else it'll never fit. try this:

<style type="text/css">
* {
    border:1px solid red;
}
.statsbox {
    width:400px;
}
.float_left
{
    float:left;
}
/* stats needs a width */
#stats
{
    width:150px;
}
</style>
<div class="statsbox float_left">
    <img src="images/chart_bar.png" class="float_left">
    <div class="float_left" id="stats">
        <p class="statsbox_header"> User Uploaded Images </p>
        <ul class="statsbox">
            <li class="statsbox"> 122 uploaded images </li>
            <li class="statsbox"> 13 images pending approval </li>
            <li class="statsbox"> 15 anonymous images </li>
            <li class="statsbox"> 97 unique users </li>
        </ul>
    </div>
    <div class="clear"></div>
</div>
<div class="clear"></div>
M4rk
unfortunately this just expands the right edge of everything and leaves the left edge overlapping the icon.
Carson Myers
hmm maybe just an issues width the wrong width, i took a guess on the widths to illustrate the fix.Can you post the CSS snippet you've used for this of html?thanks
M4rk
There wasn't really any relevant CSS to this html, just float and clear. However the solution involved some CSS for the `<ul>` tag, I'm posting it as an answer
Carson Myers
A: 

If I add display: inline to .float_left, everything works as expected.

.float_left{ float: left; display: inline; }

Screengrab

Update it seems the border specified on the divs is also playing a part. removing them breaks the display in FF3.5.

Chris
That's odd -- the borders in the picture in my question were put there by the developer toolbar plugin for firefox :) also, I figured it out -- the culprit was an attribute for the unordered list to draw the bullets outside the flow of text.
Carson Myers
ah, no, I set `div { border: 1px solid red; }` in CSS. The developer toolbar, I believe, uses `outline` rather than a border
Chris
A: 

The <div> containing the <ul> seems to expand to the left because of the list. I found out if I set the list-style-type to none, then it worked (similarly if I removed the list entirely it worked as expected).

It was like the list text was getting drawn on the very left edge of the <div>, and then the bullets would push it back to the left.

So I found the list-style-position attribute and put list-style-position: inside in my ul.statsbox selector.

I guess you can use inside to say the bullets should be inside the flow of text, and outside to say the bullets should be drawn outside the flow of text.

ul.statsbox {

    list-style-position: inside;

}

.float_left {

    float: left;

}

.float_right {

    float: right;

}

.clear {

    clear: both;

}

This CSS applied to the HTML in the question, creates the expected outcome.

Carson Myers
+3  A: 

Could I offer an alternative? I think both your markup and your css are unnecessarily verbose. This is a slightly rough example, but hopefully is enough to illustrate my point:

<style type="text/css">
    .statsbox {
        background: transparent url(path_to_image) no-repeat 9px 20px;
        float: left;
        width: 300px;
    }

    #stats h3 {
    }

    #stats {
        padding-left: 40px;
    }

    #stats ul {
        padding-left: 1em;
    }

</style>

<div class="statsbox">
    <div id="stats">
        <h3> User Uploaded Images </h3>
        <ul>
           <li> 122 uploaded images </li>
           <li> 13 images pending approval </li>
           <li> 15 anonymous images </li>
           <li> 97 unique users </li>
        </ul>
    </div>
</div>

The key things I'm doing here are:

  1. Removing the img element and applying it as a background image of .statsbox instead. This actually gives you lots of flexibility about where the image goes, without it impacting on the other elements in the html.

  2. Avoiding using list-style-postition: inside - using this property means the bullets are basically rendered as a part of the text line they are on; if the lines happen to wrap for any reason, the bullets will fail to line up properly.

  3. Made <p class="statsbox_header"> User Uploaded Images </p> an h3 instead. You've indicated that it's a heading so you might as well use that element, as it leads to cleaner markup/css.

  4. Removed the class "statsbox" from the <li> elements as CSS inheritance means you should be able to style these just by using a selector such as .statsbox li or similar. Which again makes your markup and styles simpler.

Sorry, I'm a bit rushed for time ATM, but if you need me to expand upon this answer, then please just ask :)

Sam Murray-Sutton
+1, This would have been my answer had I seen this question 2 hours ago. :)
Lance McNearney
This is perfect, thank you
Carson Myers