并排显示两个<div> s

Jon*_*ood 33 html css css-float

我的目标是<div>并排显示两个s,两个内容<div>在顶部对齐.我不希望在第二个文本中包含长文本<div>.

最后,我不想设置第二个的宽度,<div>因为我需要标记以不同的宽度工作.

示例标记位于http://jsfiddle.net/rhEyM/下方.

CSS

.left-div {
    float: left;
    width: 100px;
    height: 20px;
    margin-right: 8px;
    background-color: linen;
}
.right-div {
    float: left;
    margin-left: 108px;
    background-color: skyblue;
}?
Run Code Online (Sandbox Code Playgroud)

HTML

<div class="left-div">
    &nbsp;
</div>
<div class="right-div">
    My requirements are <b>[A]</b> Content in the two divs should line
    up at the top, <b>[B]</b> Long text in right-div should not wrap
    underneath left-div, and <b>[C]</b> I do not want to specify a
    width of right-div. I don't want to set the width of right-div
    because this markup needs to work within different widths.
</div>
Run Code Online (Sandbox Code Playgroud)

max*_*pan 36

尝试使用Flex,因为这是html5的新标准.

http://jsfiddle.net/maxspan/1b431hxm/

<div id="row1">
    <div id="column1">I am column one</div>
    <div id="column2">I am column two</div>
</div>

#row1{
    display:flex;
    flex-direction:row;
justify-content: space-around;
}

#column1{
    display:flex;
    flex-direction:column;

}


#column2{
    display:flex;
    flex-direction:column;
}
Run Code Online (Sandbox Code Playgroud)


Ray*_*oal 21

我从第二个div中移除了浮动以使其工作.

http://jsfiddle.net/rhEyM/2/


Dr.*_*eon 9

试试这个:(http://jsfiddle.net/TpqVx/)

.left-div {
    float: left;
    width: 100px;
    /*height: 20px;*/
    margin-right: 8px;
    background-color: linen;
}
.right-div {

    margin-left: 108px;
    background-color: lime;
}??
Run Code Online (Sandbox Code Playgroud)
<div class="left-div">
    &nbsp;
</div>
<div class="right-div">
    My requirements are <b>[A]</b> Content in the two divs should line up at the top, <b>[B]</b> Long text in right-div should not wrap underneath left-div, and <b>[C]</b> I do not want to specify a width of right-div. I don't want to set the width of right-div because this markup needs to work within different widths.
</div>
<div style='clear:both;'>&nbsp;</div>
Run Code Online (Sandbox Code Playgroud)

提示:

  • 只需float:left在最左边的div中使用.
  • 没有真正的理由可以使用height,但无论如何......
  • <div 'clear:both'>&nbsp;</div>在你的最后一个div之后使用的好习惯.