删除 Iframe 周围的额外空白?

发布于 2024-11-25 03:25:22 字数 557 浏览 2 评论 0原文

我在页面中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

不可一世的女人 2024-12-02 03:25:22

刚刚看到你的小提琴,你的问题是因为你正在使用 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/

梦年海沫深 2024-12-02 03:25:22
iframe { display:block; }

iframe 是一个内联元素

iframe { display:block; }

iframe is a inline element

岁吢 2024-12-02 03:25:22

当您使用内联元素时,空白可能来自该元素所属的“行”(即基线下方的空间)。解决方案是将其添加到其父元素中。

line-height: 0;

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.

line-height: 0;
怂人 2024-12-02 03:25:22

如果没有 html 内容,解决起来有点困难,但请尝试一下:

iframe {
    margin: 0px !important;
    padding: 0px !important;
    background: blue; /* this is just to make the frames easier to see */
    border: 0px !important;
}

html, body {
    margin: 0px !important;
    padding: 0px !important;
    border: 0px !important;
    width: 100%;
    height: 100%;
}

添加 !important 将强制使用样式,因为我的猜测是您的样式正在相互覆盖。

Bit difficult to solve without your html content, but give this a try:

iframe {
    margin: 0px !important;
    padding: 0px !important;
    background: blue; /* this is just to make the frames easier to see */
    border: 0px !important;
}

html, body {
    margin: 0px !important;
    padding: 0px !important;
    border: 0px !important;
    width: 100%;
    height: 100%;
}

Adding the !important will force the style, coz my guess is that your styles are overwriting each other.

挽清梦 2024-12-02 03:25:22

我遇到了同样的问题,我通过浮动框架元素修复了它

iframe {

    margin: none;
    padding: none;
    border: none;
    line-height: 0;
    float: left; 
}

I had the same problem and i fixed it by floating the frame element

iframe {

    margin: none;
    padding: none;
    border: none;
    line-height: 0;
    float: left; 
}

也许该空白实际上是加载到 .txt 文件中的文档的外边距。尝试使用以下方式对加载的文档进行样式设置(对 源页面进行 CSS 样式设置):

html, body {
  border: 0px;
  margin: 0px;
  padding: 0px;
}

引用自 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:

html, body {
  border: 0px;
  margin: 0px;
  padding: 0px;
}

quoted from stackoverflow.com Here

云裳 2024-12-02 03:25:22

尝试在 周围使用 overflow:hidden; 的 div,例如

<div style="height: 29px; overflow: hidden;">
   <iframe frameborder=0 allowtransparency=yes scrolling=no src="../hng_frames/copyright_part.html" width="980" height="30">
      <p>Your browser does not support iframes.</p>
   </iframe>
</div>

try using a div with overflow: hidden; surrounding the <iframe>, like

<div style="height: 29px; overflow: hidden;">
   <iframe frameborder=0 allowtransparency=yes scrolling=no src="../hng_frames/copyright_part.html" width="980" height="30">
      <p>Your browser does not support iframes.</p>
   </iframe>
</div>
ぃ双果 2024-12-02 03:25:22

由于给出的答案都没有为我提供解决方案(我在实现 iFrame 时也偶然发现了奇怪的边距问题),我发现这工作正常:

<iframe frameborder="0" marginwidth="0" marginheight="0" src="/something"></iframe>

marginwidthmarginheight无效/官方支持的 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:

<iframe frameborder="0" marginwidth="0" marginheight="0" src="/something"></iframe>

marginwidth and marginheight are not valid / officially supported HTML5-tags but they work just fine in my tests...

不寐倦长更 2024-12-02 03:25:22

问题出在 line-height 设置上,尝试将 line-height: 0; 添加到 iframe 容器中。

The problem is line-height setting, try adding line-height: 0; to the iframe container.

原谅我要高飞 2024-12-02 03:25:22

您必须在您调用的页面上执行此操作,而不是在 iframe 中。

<iframe src=test.html></iframe> 

然后在 test.html 中,只需 margin:0 就足够了。

body { margin: 0}

You have to do it on the page that you call, not in the iframe.

<iframe src=test.html></iframe> 

Then in test.html, only margin:0 is sufficient.

body { margin: 0}
¢蛋碎的人ぎ生 2024-12-02 03:25:22

尝试 html, body {margin:0px;}

Try html, body {margin:0px;}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文