在浮动元素周围放置边框
假设我有类似以下代码的内容,我想在左右浮动的两个图像之间显示一些文本。
<img src="testImage1.png" alt="Test Image 1" style="float:right;" />
<img src="testImage2.png" alt="Test Image 2" style="float:left;" />
<p>Test Text</p>
我想在两个图像和文本周围添加边框。我尝试在上述所有 3 个标签周围放置
并使用 style="border:2px blacksolid;"
。虽然这增加了边框,但似乎没有考虑到图像。也就是说,我们得到如下内容(使用 StackOverflow 和 Google 徽标)。我猜测发生这种情况是因为浮动元素不被视为 < 的一部分;div>
。我是一名软件开发人员,而不是 Web 开发人员,所以我不是 CSS 专家。但我确实记得浮动元素在某种程度上被“忽略”了。谁能详细描述发生了什么以及如何解决它?
Say I have something like the following code, where I want to display some text between two images that I am floating left and right.
<img src="testImage1.png" alt="Test Image 1" style="float:right;" />
<img src="testImage2.png" alt="Test Image 2" style="float:left;" />
<p>Test Text</p>
I want to add a border around the two images and the text. I tried putting a <div>
around all 3 of the above tags and using style="border:2px black solid;"
. While this adds a border, it seems to not take the images into account. That is, we get something like the following (using StackOverflow and Google logos).
I am guessing this is happening because the floating elements are not being considered as part of the <div>
. I am a software developer, not a web developer, so I am no expert in CSS. But I do think I recall that floating elements are kind of "ignored" in a way. Can anyone give a detailed description of what is going on and how to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这种情况下添加一个值为
hidden
或auto
的overflow
可以解决该问题。检查下面的小提琴:
http://jsfiddle.net/XMrwR/
Adding an
overflow
in this case with a value ofhidden
orauto
remedies the issue.Check the fiddle below:
http://jsfiddle.net/XMrwR/
在 CSS 中,浮动元素默认不会向父元素添加高度。
解决方案很简单,设置
overflow:hidden
即可。小提琴:http://jsfiddle.net/JNets/
In CSS, floated elements do not add height to a parent by default.
The solution is simply to set
overflow: hidden
.fiddle: http://jsfiddle.net/JNets/
将此行添加到您的 CSS 属性中:
Add this line to your CSS properties: