1

I know a lot of people have asked questions on float before, but from even looking at them I can't see what I'm doing wrong. I just want

HELLO
THERE

To be just to the left of the ul. Based on this answer I tried adding clear:both to the parent div. And based on this answer I tried adding overflow:hidden to the parent. Neither worked.

In all cases the UL is under the HELLO THERE and to the right.

        <div>
            <div style"float:left;">
                <div>HELLO</div>
                <div>THERE</div>
            </div>
            <div style="float:right;">
                <ul>
                    <li>A</li>
                    <li>B</li>
                    <li>C</li>
                </ul>
            </div>
        </div> 
1
  • 1
    You're missing an = sign in your style"float:left;"... Commented Aug 30, 2011 at 19:23

5 Answers 5

3

Just float them both to the left. No need to do any sort of clearing or anything for what you want. Check out the demo below.

Live Demo

Markup

<div>
    <div class="left">
        <div>HELLO</div>
        <div>THERE</div>
    </div>
    <div class="left">
        <ul>
            <li>A</li>
            <li>B</li>
            <li>C</li>
        </ul>
    </div>
</div> 

CSS

.left{float: left}
Sign up to request clarification or add additional context in comments.

Comments

3

Your float: left; isn't being applied because you forgot an =. You should float both divs left if you want them to be next to each other:

<div>
    <div style="float:left;">
        <div>HELLO</div>
        <div>THERE</div>
    </div>
    <div style="float:left;">
        <ul>
            <li>A</li>
            <li>B</li>
            <li>C</li>
        </ul>
    </div>
</div>

You may want to add overflow: hidden to the parent div, or an empty div like:

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

at the end of it, to make the content after that div be below it instead of beside it:

*JSFiddle Example*

Comments

2

Change the ul to float:left as well. As long as there is horizontal space, it should then float next to the HELLO THERE.

2 Comments

No it won't, if the division is floated to the right, it becomes an inline-block with a width determined by it's filling content. Floating an unordered list inside that will have no effect.
Thank you. I meant the div in which the ul was sitting. My bad for not being perfectly clear.
1

Rather than manually clearing your elements, use a cross browser clearfix class like so:

.clearfix:after {
    content: ".";
    display: block;
    clear: both;
    visibility: hidden;
    line-height: 0;
    height: 0;
}

.clearfix {
    display: inline-block;
}

html[xmlns] .clearfix {
    display: block;
}

* html .clearfix {
    height: 1%;
    overflow: visible;
}

And apply it to your parent div:

<div class="clearfix">
    <div style="float:left;">
        <div>HELLO</div>
        <div>THERE</div>
    </div>
    <div style="float:right;">
        <ul>
            <li>A</li>
            <li>B</li>
            <li>C</li>
        </ul>
    </div>
</div> 

Comments

0

I know there will be like 1000 answer by the time this get posted, but you need to specify a width for the outer most div. Because without that it defaults to 'auto', which means it will squash down as much as possible, which is why the <ul> is being pushed down to the next line.

<div style="width:100%;">
    <div style="float:left;">
        <div>HELLO</div>
        <div>THERE</div>
    </div>
    <div style="float:left;">
        <ul>
            <li>A</li>
            <li>B</li>
            <li>C</li>
        </ul>
    </div>
</div> 

5 Comments

I was under this assumption first but this, jsfiddle.net/loktar/6RtJ2/2 says otherwise.
Divisions are block-level by default and automatically stretch to fill the width of the parent container. You do not need to specify a width.
@animuson, div's that have no content (ie. <div></div>) don't stretch to there parents width, when you float elements it removes them from the flow, thus the div above with the floated elements taken out, becomes empty. See this fiddle: jsfiddle.net/cZf7a
@Cubed Eye Studios: That only affects the visible area of the division. Any floated elements still float to the right correctly even if there is no 'content' inside the division. Either way, you do not need to specify a width here.
@Cubed Eye Studios: To further add, you should investigate things first. If you inspect that division that has no content in something such as Firebug, you'll see that the division does stretch to the width of the parent element, it just has a height of 0... div [696x0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.