我正在使用Material UI的CardMedia组件来呈现一个iFrame,其中我嵌入了一个YouTube链接.我希望iFrame有圆角,所以我将边界半径设置为30px.它起作用了,但问题是,它还在所有四个边缘都画了一些细线,如下图所示.

enter image description here

我已将边框宽度设置为0.我try 将边框 colored颜色 设置为白色或透明,但都不起作用.当我应用边界半径时,某个iframe把它带进来了.这主要是在基于Windows的浏览器上注意到的.然而,除了 firefox 有这个问题外,Mac的Chrome和Safari中没有这个问题.

我正在通过SXprops 将样式应用到CardMedia组件.以下是我当前的代码:

cardContainer: {
    height: '360px',
    maxWidth: '640px',
    borderRadius: '30px',
    webkitBorderRadius: '30px',
    mozBorderRadius: '30px',
    borderWidth: '0',
}

这里有一个JS小提琴和一个白色视频的现场例子:在嵌入的视频中跳过大约一分钟,判断边缘. JS Fiddle Link

如果您不想打开任何外部链接,下面是为您提供的代码示例.

.custom {
    height: 360px;
    width: 640px;
    border-radius: 30px;
    border-width: 0;
}
<!DOCTYPE html>
<html>
<body>

<h1>The iframe element</h1>

<iframe class="custom" src="https://www.youtube.com/embed/OtVUZGhAHBc" title="lines on Iframe curves">
</iframe>

</body>
</html>

推荐答案

发行:

曲线背后的原因似乎是YouTube的一些默认样式,以及浏览器留下的一些非常小的间距.如果您判断iframe,您会发现在您的iframe中有以下样式:

body {
  ...
  background-color: #000;
  ...
}

.html5-video-player:not(.ytp-transparent), .html5-video-player.unstarted-mode, .html5-video-player.ad-showing, .html5-video-player.ended-mode, .html5-video-player.ytp-fullscreen {
  background-color: #000;
}

This 2 black backgrounds are basically the thin curve lines on the 4 corners. If you search 和 remove them from the browser inspector the lines will disappear.

However I am afraid there isn't a simple solution to this since you can't edit the iframe styling anymore (an related question would be this one), 和 youtube somehow renders it's body just on the visible side of your container always. And if you have border radius this will result in the same outcome -> including if you load a bigger container inside a smaller one like this:

.custom-wrapper {
    width: 635px;
    height: 355px;
    border-radius: 30px;
    overflow: hidden;
    position: relative;
    margin: 20px
}
.custom {
    width: 655px;
    height: 375px;
    position: absolute;
    top: -10px;
    left: -10px;
    overflow: visible
}
.custom iframe {
  width: 100%;
  height: 100%;
}
<h1>The iframe element</h1>


<div class="custom-wrapper">
<div class="custom">

<iframe src="https://www.youtube.com/embed/OtVUZGhAHBc" title="lines on Iframe curves">
</iframe>
</div>
</div>


解决方案/替代选项:

如果创建不会影响IFRAME加载方式的人工边界半径,您可以try 执行以下操作.类似于:

.custom {
  margin: 20px;
  height: 360px;
  width: 640px;
  position: relative;
}

.custom::before {
  content: "";
  position: absolute;
  border-radius: 50px;
  padding: 20px;
  background: linear-gradient(45deg, #fff, #fff);
  -webkit-mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
  mask-composite: add, add;
  -webkit-mask-composite: xor;
  mask-composite: exclude;
  inset: -15px;
  z-index: 999;
  width: calc(100% - 5px);
  height: calc(100% - 5px);
  pointer-events: none;
}
<h1>The iframe element</h1>
<div class="custom">
  <iframe width="640px" height="360px" src="https://www.youtube.com/embed/OtVUZGhAHBc" title="lines on Iframe curves">
  </iframe>
  <div class="white-bg"></div>
</div>


解释:

Basically we added a container for the iframe (positioned relative) 和 inside it also added a :before element which will act like the border radius we want.

You can read more about the artificial border radius approach here but basically what this does is:

1. From the link:

  • Appling a gradient as background 和 we make its origin the border box (by default it's the padding box).
  • Using the mask property, we apply two opaque layers. The bottom one will cover the whole element 和 the top one will cover only the padding box (so it will not cover the border area)
  • 排除顶层和底层,以便只显示边界区域
  • 一些浏览器仍然不支持掩码合成,所以我们使用前缀版本.

2. Extra:

  • Since in this case we want to make the border only round the corners we have it inset: -15px; (or top: -15px; left: -15px;) 和 width: calc(100% - 5px); height: calc(100% - 5px); . This prevent any margin from being displayed.

PS: You might want to play a little with this values since they might not be perfect, but the look pretty close to your requirements. Basically if you make the width 和 height bigger it would trim more but in this case you need to reposition the :before element. If you get any lines you don't want on the edges just make the container smaller or move it slightly so the :before elements covers them.

  • border-radius: 50px;你想要30,但这不是真正的边界半径,所以30不会完全符合你的情况.但是,您也可以使用此值.

  • 这是可以更改的,但基本上是一个很大的索引,用于将元素定位在iframe上.还增加了pointer-events: none;以启用点击iframe(所以使用YouTube窗口).

  • changed the colours for the background 和 the gradient to white (#fff) -> if you have another bg this should match the other one.

  • 也增加了填充到20px,以便更容易隐藏角落.

Css相关问答推荐

如何使用css创建带有曲线的自定义按钮

如何防止背景重复图像被截断

Vue3日期 Select 器:作用域样式不起作用

如果父元素是弹性元素,则不会显示带有纵横比的空div

将鼠标悬停在具有效果的元素上

Next.js 条件样式

当您到达页面底部时,如何使页脚 div 将附加的 div 向上推

悬停时如何更改 navbarPage 链接的文本 colored颜色 (在shiny 的应用程序中)?

根据选项 colored颜色 更改 Select 文本 colored颜色

如何为使用jsTreeR拖入层次 struct node 的元素添加顺序编号?

在 vuetify 中跨 `default.vue` 和 `index.vue` 维护网格系统

如何将 .btn-group 从 twitter-bootstrap 居中?

css3比例周围的空白

如何创建一个与 Bootstrap 3 配合得很好的粘性页脚

css显示:表格不显示边框

CSS Select /样式第一个词

以原始大小的 50% 显示图像

Angular 动画的目的是什么?

如何使用 JavaScript 获取元素的背景图片 URL?

CSS停止图像下的文本换行