删除 Iframe 周围的额外空白?
我在页面中使用 iframe,并偶然发现了一个奇怪的问题。我像这样设置 iframe css
iframe {
margin: none;
padding: none;
background: blue; /* this is just to make the frames easier to see */
border: none;
}
但是,iframe 周围仍然有空白。我在 Chrome 和 Firefox 中测试过,都是一样的。我四处寻找,却什么也没找到。此空格也不会显示在 Chrome 控制台中。 另外,我也已经有了以下 CSS:
html, body {
margin: 0px;
padding: 0px;
border: 0px;
width: 100%;
height: 100%;
}
JSFiddle: 这里
I am using iframes in my page, and have stumbled across a weird issue. I set the iframe css like so
iframe {
margin: none;
padding: none;
background: blue; /* this is just to make the frames easier to see */
border: none;
}
However, there is still whitespace surrounding the iframe. I tested it in both Chrome and Firefox, and it's the same. I searched around, and I couldn't find anything. This whitespace doesn't show up in the Chrome console, either.
Also, I already have the following CSS as well:
html, body {
margin: 0px;
padding: 0px;
border: 0px;
width: 100%;
height: 100%;
}
JSFiddle: here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
刚刚看到你的小提琴,你的问题是因为你正在使用
display:inline-block
。这会考虑 html 中的空白。display:inline-block
因其困难而臭名昭著,而且浏览器支持也很不稳定。选项 1:
尝试删除 html 中的空白有时可以解决问题。
选项 2:
使用不同的显示属性(例如
display:block
)肯定可以解决问题。实例:http://jsfiddle.net/mM6AB/3/Having just seen your fiddle your issue is because you are using
display:inline-block
. This takes whitespace in your html into account.display:inline-block
is notorious for being difficult and has dodgy browser support.Option 1:
Try removing the white space in your html can sometimes sort the problem.
Option 2:
Using a different display property such as
display:block
will definitely sort the problem. Live example: http://jsfiddle.net/mM6AB/3/iframe 是一个内联元素
iframe is a inline element
当您使用内联元素时,空白可能来自该元素所属的“行”(即基线下方的空间)。解决方案是将其添加到其父元素中。
When you are using an inline element, the whitespace might be from the "line" the element is part of (ie. the space below the baseline). The solution then is to add this to its parent element.
如果没有 html 内容,解决起来有点困难,但请尝试一下:
添加
!important
将强制使用样式,因为我的猜测是您的样式正在相互覆盖。Bit difficult to solve without your html content, but give this a try:
Adding the
!important
will force the style, coz my guess is that your styles are overwriting each other.我遇到了同样的问题,我通过浮动框架元素修复了它
I had the same problem and i fixed it by floating the frame element
也许该空白实际上是加载到 .txt 文件中的文档的外边距。尝试使用以下方式对加载的文档进行样式设置(对 源页面进行 CSS 样式设置):
引用自 stackoverflow.com 此处
Maybe that whitespace is actually the outside margin of the document loaded in the . Try styling the loaded document (CSS styling the source page) with:
quoted from stackoverflow.com Here
尝试在
周围使用
overflow:hidden;
的 div,例如try using a div with
overflow: hidden;
surrounding the<iframe>
, like由于给出的答案都没有为我提供解决方案(我在实现 iFrame 时也偶然发现了奇怪的边距问题),我发现这工作正常:
marginwidth 和 marginheight无效/官方支持的 HTML5 标签,但它们在我的测试中工作得很好......
Since none of the given answers provided a solution for me (I also stumbled across the weird margin issue when implementing an iFrame) I found this to be working fine:
marginwidth and marginheight are not valid / officially supported HTML5-tags but they work just fine in my tests...
问题出在 line-height 设置上,尝试将
line-height: 0;
添加到 iframe 容器中。The problem is line-height setting, try adding
line-height: 0;
to the iframe container.您必须在您调用的页面上执行此操作,而不是在 iframe 中。
然后在 test.html 中,只需
margin:0
就足够了。You have to do it on the page that you call, not in the iframe.
Then in test.html, only
margin:0
is sufficient.尝试
html, body {margin:0px;}
Try
html, body {margin:0px;}