css - 如何将两个div并排放置?

标签 css html css-float

我需要构建一个列表,左侧有文本,右侧有照片。

这是我的 HTML:

<div class="wrap">
    <div class="one">
        <p>This is text to be displayed in this div. It can be long, or short, or anything.</p>
    </div>
    <div class="two">photo here</div>
</div>
<div class="wrap">
    <div class="one">
        <p>#2: This is text to be displayed in this div...</p>
    </div>
    <div class="two">photo here</div>   
</div>    

这是 CSS:

div.wrap {
    border: 1px solid #000000;
    overflow: auto;
}
div.one {
    border: 1px solid #AAAAAA;
}
div.two {
    background-color: #DDDDDD;
    width: 200px;
    height: 300px;
    float: right;
}

问题: http://jsfiddle.net/egXuq/

我尝试过的: 我研究了一些关于SO的解决方案,并且......

  • overflow: auto 放入父 div
  • float div.two 右侧 --> 还是一个在另一个下面...需要帮助,请。

最佳答案

您还需要 float div.one

div.one {
    border: 1px solid #AAAAAA;
    float: left;
}

Updated Fiddle.

--

如果您对 float 或元素定位感到好奇,您可以在 MDN's documentation 上阅读更多内容。简单的解释是这样的:

When an element is floated it is taken out of the normal flow of the document. It is shifted to the left or right until it touches the edge of it's containing box or another floated element.

关于css - 如何将两个div并排放置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19283852/

相关文章:

javascript - p5 Canvas 无法通过css调整大小

css - 给css添加两个关键帧 Action

html - 图标下显示的 CSS 旋转文本

html - 如何强制 float div 移动到(非 float )表格下方而不是在没有空间时调整大小?

css - 包装 div 的垂直滚动

html - CSS3 使用固定 Flex div 向上滑动和向下滑动

javascript - 如何设置不同的打印介质

html - 无法让文字漂浮在背景图片上

jquery - 无法在 div 内 float 跨度

jquery - 使用 jQuery 扩展 div,而不移动 float 到其上的 div