开发者

CSS: how to get two floating divs inside another div

开发者 https://www.devze.com 2022-12-12 03:55 出处:网络
I\'m sure this a common problem, but couldn\'t find the exact answer :) I have two divs inside another div. I want the two divs to be on the same level, one floating to the left and the other开发者_如

I'm sure this a common problem, but couldn't find the exact answer :)

I have two divs inside another div. I want the two divs to be on the same level, one floating to the left and the other开发者_如何学C to the right. But they won't get inside the parent div unless I use position: absolute on the parent. But then the child-divs won't stay on the same level :S

#main {
    margin-left: 30px;
    margin-top: 20px;
    position: absolute;
}

#left_menu {
    width: 150px;
    float: left;
}

#content {
    margin-left: 20px;
    float: right;
    border: 1px solid white;
}

    <div id ="main">
    <div id ="left_menu>&blablabal</div>
    <div id ="content">blablb</div>
    </div>


your margin-left of #content should include the width of #left_menu. Thus should be

#content {
    margin-left: 170px;
    /* float: right; */ /* no need for a float here */
    border: 1px solid white;
}

You also don't need a position:absolute for your #main (unless other purposes)

So finally:

<style type="text/css"><!--
#main {
    margin-left: 30px;
    margin-top: 20px;
}

#left_menu {
    width: 150px;
    float: left;
}

#content {
    margin-left: 170px;
    border: 1px solid white;
}
.c{clear:both;}
--></style>
    <div id="main">
    <div id="left_menu>&amp;blablabal</div>
    <div id="content">blablb</div>
    </div>
    <div class="c"></div>

.c is to clear and pushes the bottom content off the floats.


What about this its all to do with your width on your container.

This works for me.

    <style type="text/css"><!--
        .Content{
        Width:100%;
        }

        .FloatLeft{
        float:left;
        }
        .FloatRight{
        float:Right;
        }
-->
    </style>

        <div class="Content">
        <div class="FloatLeft"></div>
        <div class="FloatRight"></div>
        </div>


you will need to 'float' the main div, or use a clearing <div> or <br> after your content and left menu <div>s.


The problem is not "staying on the same level", but it's about the size of the container div.

This might help you: http://archivist.incutio.com/viewlist/css-discuss/63079


The nicest and easiest thing to do is to set overflow: hidden on the container, #main. I don't think this works in IE6 though.


try giving the main div an overflow: hidden; and taking away it's position: absolute; which will give it a height equivalent to the greater height of the floating divs

Also, I don't know if you copied it from your page, but you're missing a close quotation in your left_menu id=""


#main{
display:inline-block;
width:100%;
}

and remove absolute to the parent;

#left_menu,#content{
 ....
 vertical-align:top;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号