2017-02-13 117 views
0

我有一个包含复选框和标签的设置宽度的容器div。问题在于,不是在复选框旁边浮动,而是在复选框下面放置标签。我怎样才能使标签浮动旁边的复选框。这是HTML和CSS,我在这里有一个jsFiddle浮动复选框旁边的div

<div id="CheckboxContainer"> 
<input type="checkbox" id="TheCheckbox" /> 
<div id="TheCheckboxLabel"> 
This text should float next to the checkbox instead of being under. 
</div> 
</div> 

#CheckboxContainer{width:300px;margin:20px auto;background:red;overflow:hidden;} 
#TheCheckbox{float:left;display:inline-block;margin:10px 10px;} 
#TheCheckboxLabel{float:left;display:inline-block;margin:10px 10px;} 

谢谢。

+0

为什么你需要它浮动?默认行为https://jsfiddle.net/7np4y68k/1/ – yBrodsky

+0

仅使用float:留在ID“TheCheckbox”并从两个ID中移除display:inline-block TheCheckbox和TheCheckboxLable [https://jsfiddle.net/g2vzycsa/1 /] –

回答

1

仅漂浮#TheCheckbox,并在#TheCheckboxLabel上添加overflow: hidden;

#CheckboxContainer { 
 
    width: 300px; 
 
    margin: 20px auto; 
 
    background: red; 
 
    overflow: hidden; 
 
} 
 
#TheCheckbox { 
 
    float: left; 
 
    margin: 10px 10px; 
 
} 
 
#TheCheckboxLabel { 
 
    margin: 10px 10px; 
 
    overflow: hidden; 
 
}
<div id="CheckboxContainer"> 
 
    <input type="checkbox" id="TheCheckbox" /> 
 
    <div id="TheCheckboxLabel"> 
 
    This text should float next to the checkbox instead of being under. 
 
    </div> 
 
</div>

0

Flexbox可以轻松实现。

#CheckboxContainer{width:300px;margin:20px auto;background:red;display:flex;}
<div id="CheckboxContainer"> 
 
<input type="checkbox" id="TheCheckbox" /> 
 
<div id="TheCheckboxLabel"> 
 
This text should float next to the checkbox instead of being under. 
 
</div> 
 
</div>