html - 如何将文本环绕在右下角的 div 周围?

标签 html css css-float

每次我尝试在 CSS 中做一些看似简单的事情时,它都行不通。

我有一个包含 460x160 图像的内容 div。我想做的就是将图片放在右下角,然后用文字环绕它。

<div id="contents">
    <img src="..." />
    text text text text text text ...
</div>

所以我希望它看起来像:

------------------
| text text text |
| text text text |
| text text -----|
| text text |    |
------------------

用左上角或右上角的图像做这件事很简单:

#contents img { float:right; }

------------------
| text text |    |
| text text -----|
| text text text |
| text text text |
------------------

现在如何将它推到底部? 到目前为止我想到的最好的是:

#contents img { float:right; margin-top: 90%} // really needs to be 100%-160px

------------------
| text text      |
| text text      |
| text text -----|
| text text |    |
------------------

在这种情况下,文本不会打印在页边空白处,因此图像上方有空白区域。

#contents { position:relative; }
#contents img { position:absolute; right:0; bottom:0; }

-or-

// move the img tag under the text in the html and:
#contents img { float:right; margin-top:-160; }

------------------
| text text text |
| text text text |
| text text -----|
| text text | te |
------------------

在这种情况下,文本打印在图像上方或下方。

那么...我怎样才能做到这一点?

最佳答案

肯定有人问过before (2003) , 和 before (2002) , 或 before (2005)

最后一个链接实际上建议 javascript-based solution ,但对于固定(即非流体)解决方案。

然而,它与 other advices found 是一致的

The only way to do that is to put the floated element somewhere in the middle of the text. It's impossible to get it perfect all of the time.

this one :

It consists of floating a vertical "pusher" element (such as img, but it's probably smarter to just use an empty div), then floating the desired object under it, using the clear property. The major problem with this method is you still have to know how many lines there are of text. It makes things MUCH easier though, and could definitely be coded with javascript, just need to change the height of the "pusher" to the height of the container minus the height of the float (assuming your container isn't fixed/min height).

无论如何,作为discussed in this thread ,没有简单的解决方案...


Cletus在评论中提到这个thread from 2003 ,这再次说明了它不容易实现的事实。
但是,它确实引用了这个 Eric Meyer's article , 这接近您想要达到的效果。

By understanding how floats and the normal flow relate to each other, and understanding how clearing can be used to manipulate the normal flow around floats, authors can employ floats as a very powerful layout tool.
Because floats were not originally intended to be used for layout, some hacks may be necessary to make them behave as intended. This can involve floating elements that contain floats, "clearing" elements, or a combination of both.


然而,Chadwick Meyer建议 his answer基于 :before CSS selector 的解决方案(Leonardanswer 的变体)。
它确实 work here .


2021 年 4 月更新:Temani Afif建议 his answer使用 Flexbox 结合 shape-outside。
但是请查看 Backwards Compatibility of Flexbox , 即使its support所有浏览器都很好。

关于html - 如何将文本环绕在右下角的 div 周围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22460886/

相关文章:

css - 使用 Singularitygs 删除一行中的最后一个装订线?

css - 背景 div 根据包含的元素调整高度

javascript - jQuery UI 无法在事件函数之外获取选定的选项卡

css - 静态文件未在 django 1.6 中加载

html - 如何使用CSS将移动设备上的水平菜单更改为垂直菜单

javascript - 在内部创建动态元素的父 div 上没有高度

html - 调整屏幕大小时如何控制内联与堆叠?

html - Chrome 中的 CSS3 动画加载器黑色

javascript - 在 highcharts/highstock 中添加自定义下拉菜单

javascript - 将输入元素限制为特定字符串