CSS: Why don't these two div (or span) elements float next to each other

最后都变了- 提交于 2019-12-24 02:33:36

问题


In the following code, I need the div elements #ldiv and #rdiv to be placed next to each other.

So I am using the CSS float property. I looked up this answer, and I think I am following it, but still the div's won't position next to each other. The second div displays in the next line.

Then I thought that div is a block level element, so I replaced the div by span elements. But that did not help either.

JSFiddle here.

<div style="padding:25px; width:400px;">

    <div style="background-color:#bf5b5b;">
    <span>Yes</span>
    <span>No</span></div>

        <div id="option_one_div">
            <div id="ldiv" style="float:left; background-color:#74d4dd; width:150px;">
            <label for="rbutton_radio_1_0" style="margin-left:30px; margin-right:30px;">
                <input for="rbutton_radio_1_0" type="radio" name="radio" value="0"/></label>
            <label for="rbutton_radio_1_1" style="margin-left:30px; margin-right:30px;">
                <input for="rbutton_radio_1_1" type="radio" name="radio" value="1"/></label>
            </div>
            <div id="rdiv" style="float:right; background-color:#74d4dd; margin-left:151px; padding-left: 20px; padding-right: 20px">
            <span>Label of first group of Radio Buttons radio buttons.</span>
            </div>
    </div>

</div>

回答1:


In your situation you can use display:table in container(#option_one_div) in your example and display:table-cell in children elements(#ldiv, #rdiv) like this:

<div style="padding:25px; width:400px;">

    <div style="background-color:#bf5b5b;">
    <span>Yes</span>
    <span>No</span></div>

        <div id="option_one_div" style="display: table;">
            <div id="ldiv" style="background-color:#74d4dd; width:150px;display:table-cell;">
            <label for="rbutton_radio_1_0" style="margin-left:30px; margin-right:30px;">
                <input for="rbutton_radio_1_0" type="radio" name="radio" value="0"/></label>
            <label for="rbutton_radio_1_1" style="margin-left:30px; margin-right:30px;">
                <input for="rbutton_radio_1_1" type="radio" name="radio" value="1"/></label>
            </div>
            <div id="rdiv" style="display:table-cell; background-color:#74d4dd; margin-left:151px; padding-left: 20px; padding-right: 20px">
            <span>Label of first group of Radio Buttons radio buttons.</span>                
            </div>
    </div>

</div>

fiddle

As you can see you don't need floats.




回答2:


use width with float in div

<div id="rdiv" style="float:right; background-color: #74d4dd; /* margin-left: 151px; */ padding-left: 20px; width: 210px;padding-right: 20px">
            <span>Label of first group of Radio Buttons radio buttons.</span>
            </div>

plz check




回答3:


the total width of the elements (incl. margin/border) can not be greater than the surrounding divs width of 400px else the floating elements will be put into the next line ... see akz's answer for a quick fix




回答4:


Just remove float:right. It will work.

  <div id="rdiv" style="background-color:#74d4dd; margin-left:151px; padding-left: 20px; padding-right: 20px">

DEMO




回答5:


the main problem is you didn't add width to the id="rdiv" and used margin-left:151px when you use some element with float, you have to add its width

<div>
    <div style="float: left;width: 110px;height: 90px;margin: 5px;">a</div>
    <div style="float: left;width: 110px;height: 90px;margin: 5px;">b</div>
    <div style="float: left;width: 110px;height: 90px;margin: 5px;">c</div>
</div>

to solve the problem, you can do one of the following things:

1: change your code to:(notice you set margin-left:151px; in th id="rdiv" and I changed it to lower value)

<div id="rdiv" style="float:right; background-color:#74d4dd; margin-left:15px; padding-left: 20px; padding-right: 20px; width:50px;">

2: remove your float=right in the id="rdiv" so the id="ldiv" could be in the float version of this element



来源:https://stackoverflow.com/questions/25504670/css-why-dont-these-two-div-or-span-elements-float-next-to-each-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!