html - CSS 向右浮动奇怪的行为

标签 html css

我在图像板上有 2 个线程彼此堆叠,每个线程都有一些文本和一个图像文件。

我希望图像始终位于线程的右侧,而不是位于回复按钮的顶部。

当我通过 float: right; 将图像向右浮动时图像不断堆叠在一起,形成一个类似 Twig 的结构

如何强制具有图像后类的图像不被破坏 <div>以下线程的结构并留在屏幕右侧?

<div class="you" id="thread_27" data-board="b" align="Right">
  <div class="files">
    <div class="file">
      <p class="fileinfo">File: <a href="/b/src/1454704253855.jpg">1454704253855.jpg</a> <span class="unimportant">(78.69 KB, 1024x768, <span class="postfilename">soft-color-background.jpg</span>)</span>
      </p>
      <a href="/b/src/1454704253855.jpg" target="_blank"><img class="post-image" src="/b/thumb/1454704253855.png" style="width: 255px; height: 192px; max-width: 98%;" alt=""></a>
    </div>
  </div>
  <div class="post op" id="op_27">
    <p class="intro">
      <input class="delete" name="delete_27" id="delete_27" type="checkbox">
      <label for="delete_27"><span class="name">Anonymous (You)</span>
        <time data-local="true" datetime="2016-02-05T20:30:54Z">02/05/16 (Fri) 22:30:54</time>
      </label>&nbsp;<a class="post_no" id="post_no_27" onclick="highlightReply(27)" href="/b/res/27.html#27">No.</a><a class="post_no" onclick="citeReply(27)" href="/b/res/27.html#q27">27</a><a href="/b/res/27.html">[Reply]</a></p>
    <div class="body">xxxxxx2</div>
  </div>
  <br class="clear">
  <hr>
</div>
<div class="you" id="thread_26" data-board="b" align="Right">
  <div class="files">
    <div class="file">
      <p class="fileinfo">File: <a href="/b/src/1454704190918.jpg">1454704190918.jpg</a> <span class="unimportant">(157.33 KB, 1024x768, <span class="postfilename" title="mac-style-presentation-background.jpg">mac-style-presentation-bac….jpg</span>)</span>
      </p>
      <a href="/b/src/1454704190918.jpg" target="_blank"><img class="post-image" src="/b/thumb/1454704190918.png" style="width: 255px; height: 192px; max-width: 98%;" alt=""></a>
    </div>
  </div>
  <div class="post op" id="op_26">
    <p class="intro">
      <input class="delete" name="delete_26" id="delete_26" type="checkbox">
      <label for="delete_26"><span class="name">Anonymous (You)</span>
        <time data-local="true" datetime="2016-02-05T20:29:51Z">02/05/16 (Fri) 22:29:51</time>
      </label>&nbsp;<a class="post_no" id="post_no_26" onclick="highlightReply(26)" href="/b/res/26.html#26">No.</a><a class="post_no" onclick="citeReply(26)" href="/b/res/26.html#q26">26</a><a href="/b/res/26.html">[Reply]</a></p>
    <div class="body">xxxxx</div>
  </div>
  <br class="clear">
  <hr>
</div>

当我添加 CSS 时

.post-image
{
  float: right;
}

它破坏了一切。

jsfiddle before

js fiddle after floating post-image to the right

最佳答案

发生这种情况是因为向左或向右浮动clear 控制 float 行为,因此,理想的方法是使容器内的所有 float 属性通常为 div。有时,浏览器会以不同的方式呈现它,因此每当进入新容器或同级容器时,理想的方法是清除 float(如果您不需要 float 属性)。 在你的代码中你可能可以

添加此内容:

.clear{
  clear:both;
 }

为了更好地理解。另外,不要忘记使用它,将其放置在需要的地方。

关于html - CSS 向右浮动奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35233756/

相关文章:

html - 512mb 内存设备上 Android 版 Chrome 中的渐变绘制问题

javascript - 带有固定标题的巨大 HTML 表格,标题单元格和行单元格之间对齐

jquery - 响应 Metro-UI-CSS

jquery - 有没有办法在 DIV 中进行视差工作

css - 如何通过滚动使溢出的 SVG 内容可见?

css - material ui表中如何改变选中行的文字颜色

javascript - 单击多个按钮时更改 div 的宽度

javascript - 我想添加菜单 "page-scroll-effects-master"。它由 "jquery"组成

PHP 画廊,缩略图列表

css - 如何在CSS中使用nth-child获取不同的类选择器