简体   繁体   English

如何使文字环绕图像而不留空格或换行符?

[英]How do I make text wrap around an image without leaving whitespace or linebreaks?

I want to have text wrap around an image (which I achieved) and when a line breaks not to have whitespace around the image. 我想在图像周围换行文字(这是我实现的),并且在换行时不要在图像周围留空格。

在此处输入图片说明

 <div id="container1"> <div id="container2" style="width: 300px"> <font color="black"> <h1 class="customfonter">Title</h1> <img src="https://via.placeholder.com/530x400" width="34%" height="auto" style="float: left; margin-right:1%;margin-top:1%;"> <pre style="width: auto;white-space: pre-wrap;word-wrap: break-word;">Randomtext Randomtext Randomtext Randomtext Randomtext Randomtext Randomtext Randomtext Randomtext Randomtext Randomtext Randomtext Randomtext Randomtext Randomtext Randomtext Randomtext Randomtext <a id="link" href="#">more</a></pre> </font> </div> <div> 

I couldn't find the solution I was looking for, so I settled with using margin-bottom to make sure no text got under the picture. 我找不到我想要的解决方案,因此我选择使用margin-bottom来确保图片margin-bottom没有文字。

The script I made to automatically change the height of the bottom margin with the HTML from my question: 我编写的脚本通过问题中的HTML自动更改了下边距的高度:

 setInterval(refresh, 1000/30); function refresh() { ih = document.getElementsByTagName('img')[0].getBoundingClientRect().height; ph = document.getElementsByTagName('pre')[0].getBoundingClientRect().height; document.getElementsByTagName('img')[0].style.marginBottom = `${Math.abs(ph - ih)}px`; } 
 <div id="container1"> <div id="container2" style="width: 300px"> <font color="black"> <h1 class="customfonter">Title</h1> <img src="https://via.placeholder.com/530x400" width="34%" height="auto" style="float: left; margin-right:1%;margin-top:1%;"> <pre style="width: auto;white-space: pre-wrap;word-wrap: break-word;">Randomtext Randomtext Randomtext Randomtext Randomtext Randomtext Randomtext Randomtext Randomtext Randomtext Randomtext Randomtext Randomtext Randomtext Randomtext Randomtext Randomtext Randomtext <a id="link" href="#">more</a></pre> </font> </div> <div> 

尝试以行高播放文本以使其最适合,或为图像设置边距右以在文本和图像之间形成相同的“空白”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM