When adding a border radius to an iframe, you'll notice delicate curved lines appearing on all four corners

I have implemented material UI's CardMedia component to display an Iframe with a embedded youtube link. To achieve rounded corners for the iframe, I set the border-radius to 30px. Although it successfully rounds the corners, it also introduces thin lines on all edges like shown in the image below.

https://i.sstatic.net/6TWK6.png

Despite setting the border-width to 0 and trying different border colors such as white or transparent, the issue persists. It seems that the iframe itself is causing this when applying the border radius. This problem is particularly noticeable on Windows-based browsers, while Mac's Chrome and Safari do not exhibit this behavior. Firefox also displays this issue.

The styles are being applied to the CardMedia component using the sx prop. Below is my current code snippet:

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

For a live example of a white video, you can view the JS Fiddle provided below. Skip about a minute into the embedded video to inspect the edges. JS Fiddle Link

If you prefer not to visit external links, here is a code sample for your reference:

.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>

Answer №1

Concern:

The issue with the curved lines appears to be caused by default styling from YouTube in combination with minimal spacing left by the browser. Upon inspecting the iframe, it can be observed that the following styles are present:

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

and

.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;
}

These two black backgrounds essentially manifest as faint curved lines on the corners. Removing them using the browser inspector eliminates the lines.

Unfortunately, there is no straightforward solution to this problem since editing the iframe styling is not feasible anymore (a related query would be found here). Additionally, YouTube consistently renders its body just on the visible side of your container, which results in the same outcome if there is a border radius or if a larger container is loaded into a smaller one like in this scenario:

.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>


Solution / Alternative approach:

An alternative option to consider is creating an artificial border radius that does not affect how the iframe loads. This involves:

.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>


Explanation:

This method involves adding a container for the iframe (positioned relative) and inserting a :before element within it to simulate the desired border radius effect.

Detailed information about the artificial border radius technique can be found here. In essence, this approach achieves the following:

1. As mentioned in the source:

  • Utilizing a gradient background with the origin set to the border box instead of the padding box.
  • Applying two opaque layers using the mask property, where the bottom layer covers the entire element and the top layer only covers the padding box (excluding the border area).
  • Excluding the top layer from the bottom layer to display only the border area.
  • Providing compatibility for browsers that do not fully support mask-composite through prefixes.

2. Additional notes:

  • Adjusting the border-radius: 50px to create rounded corners, even though the desired value may differ from the actual border radius value.
  • Setting a high z-index: 999 for proper positioning over the iframe, alongside utilizing pointer-events: none; to allow clicking on the iframe itself.
  • Changing colors for the background and gradient to white (#fff) to match existing backgrounds.
  • Increasing padding to 20px for better corner concealment.

Note: These values can be fine-tuned based on individual requirements. Adjustments may include altering dimensions, repositioning elements, or experimenting with color schemes to achieve the desired look while eliminating unwanted lines around the edges.

Answer №2

Encountered this issue where it seemed to be related to anti-aliasing, particularly affected by window size. I came up with a solution that involved:

.iFrameWrapper {
  display: block;
  overflow: hidden;
  border-radius: 12px;
  border: 1px solid gray;
}

.iFrame {
  border-radius: 10px;
}

Adjusting the border size on the iFrame slightly helped to alleviate the problem of conflicting edges aligning. When set to either 12px or 0px, the issue resurfaced.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to eliminate black borders from a pop-up window?

When working with material UI, I noticed that the default import of a component includes black borders. Is there a way to modify the code so the box is completely white without any black borders? Looking for a snippet that can help me achieve this. Curren ...

Fix the uneven shape border/outline

I made 3 circles using CSS (:after) and noticed that the border looks uneven with certain background colors. Any suggestions on how to resolve this issue? You can view the problem here: Just below the post title, you'll find the first blue circle bo ...

Creating a user interface library using Storybook

Currently, I am in the process of developing a UI library for my organization using React with Material-UI as a peer dependency in the package.json file. I wanted to incorporate Storybook to create a playground for the components I am constructing. However ...

What is the best way to keep the heights of two divs in sync?

Is there a way to make two divs have the same height, even though their content determines their individual heights? I am looking for a solution that doesn't involve using a table or a parent div. I'm new to JavaScript, so I'm hoping there i ...

What causes the lack of upward movement for a div element when the div above it is floated to the left?

Recently, I've been delving into the world of float property in CSS. This is the HTML code snippet I'm working with: .left { float: left; text-align: center; width: 25%; } .normal { text-align: center; width: 25%; } <div class="lef ...

Using type annotations is restricted to TypeScript files only, as indicated by error code ts(8010)

I encountered an issue with React.MouseEvent<HTMLElement> const handleOpenNavMenu = (event: React.MouseEvent<HTMLElement>) => { setAnchorElNav(event.currentTarget); }; const handleOpenUserMenu = (event: React.MouseEvent<HTMLElement ...

The Font Awesome icon is not displaying on top of the Materialize CSS progress/determinate bar

I am struggling to display a Font Awesome icon on top of a Materialize CSS progress/determinate div. Unfortunately, the icon is not fully visible and part of it gets clipped or hidden. Despite trying various solutions, I have not been successful. .progres ...

Step-by-step guide on creating a sequential glowing effect for list items with CSS and JQuery

I would like to create a glowing effect on each icon individually. The idea is for each icon to glow for a brief moment and then return to its normal state before moving on to the next icon in line. I am considering using text-shadow for the glow effect an ...

What is causing the CSS3 tabbed area to be restricted?

Experimenting with CSS tabs found at http://css-tricks.com/examples/CSSTabs/: Sample six .w3c { min-height: 250px; position: relative; width: 100%; } .w3c > div { display: inline; } .w3c > div > a { margin-left: -1px; position: relative; left: 1 ...

Is there a way to reset the dynamic flexslider when a click event occurs?

Seeking a way to refresh the flexslider on a click event. I have embedded the flexslider in a Bootstrap pop-up and need it to update when the user clicks. The issue arises when I try to refresh the slider as it fails to display properly, likely due to losi ...

How can I retrieve the reference number of an item by clicking a button within a list item in an unordered

Presented here is a collection of data points and a button nested within an ul <ul> <li class="mix" category-1="" data-value="600.35" style="display:block;"> <figure> <figcaption> <h3> ...

Using Flexbox to align content in a column with space between items

Is there a way to move the read more button to the bottom of the card using flexbox in a column layout? The top section, middle paragraph, and see more button are each contained within their own div inside the parent card div. https://i.stack.imgur.com/NG ...

Is there a way to create an internal link to another HTML templating engine page within Express.js?

I am currently facing an issue with two Pug files, index.pug and search.pug, stored in a /views folder. In my index.pug file, I have the following line of code: a(href="/search.pug") Search In my JavaScript file, I have specified the view engine as P ...

The fit-content max-height property is not functioning properly in Firefox

This CSS approach is effective in browsers like Chrome. p.ex1 { max-height: fit-content; height: 250px; border:solid 1px black; overflow: auto; } The goal is to utilize the full height, allowing scrolling when necessary, while also adjusti ...

Escaping from its container, text breaks free with the power of CSS

Experiencing difficulties with CSS flex layout that are proving challenging to resolve. HTML: <div class="image-view"> <div class="info"> <span class="label title">d37ci4x.jpg</span> <span class="label scale ...

Eliminate gaps between lines in a wrapped Flexbox layout

I am trying to eliminate the spaces between rows in a horizontal list flexbox. I am creating an alphabetical list and I want it to flow seamlessly without any gaps. This is what I have so far: #example { display: block; margin-left: auto; margin-r ...

Material Design - Adjustable Sidebar

Looking for advice on creating a reactive Appbar and Drawer. Can anyone provide guidance or examples? The goal is to have the drawer dynamically undock and hide when the browser is small, and dock the drawer when larger. Ideally, this would work similar t ...

Strange spacing appears after image tags

My efforts to remove the space (red) between the <img> and the <div> have been unsuccessful. I have minimized it as much as possible. After researching similar threads, it seems that whitespace or newlines between inline-box elements can cause ...

Guide on customizing webpack to incorporate SCSS in a React application

After creating my React app using create-react-app (with React ver. 16.6.3), I decided to incorporate SCSS into my components. To begin, I ran the eject script and made necessary adjustments in webpack.config.dev.js: { test: cssRegex, exclude: cssMo ...

Retrieve the image and insert it using an img tag

Working on a project, I want to include Instagram profile pictures by embedding them. Although I have the image URL, I am struggling to integrate it into my HTML page. Take a look at this sample: This provided link displays the Instagram profile picture. ...