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.

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

Generating interactive cards using an array

In an attempt to showcase information for three separate months using a single component, I am creating cards titled "Month 1," "Month 2," and "Month 3." Each card contains a table, and I am aiming to add headers to each card based on an array of titles. ...

Having trouble accessing the property of undefined in material ui cards

Currently, I am incorporating Material UI cards into my React project. One issue I encountered is trying to implement two functions, onMouseOver and onMouseOut, in my cards. However, when I run the code, I receive an error message stating "Uncaught TypeE ...

Creating a dynamic collection of N triangles in a container using CSS

In my current project, I am developing an Electron+Vue application that features a grid-styled map. Each cell on the map represents a room, and I want to indicate walls within these cells. Specifically, for directions North, South, East, and West, I can us ...

Help! I need assistance with removing the shadow from a table in material UI react. I've tried searching for the correct API but I can't seem to find the necessary

Is there a way to get rid of the shadow on the table? I've looked for the API but couldn't find the correct property. The examples from the library all have shadows, and I'm unsure how to remove them. View the shadow here Check out the cod ...

Converting units to rem dynamically in CSS: a comprehensive guide

Hey there, I'm currently trying to dynamically convert units into rem in CSS and facing some issues. I have set the root font-size as 23px The current font-size is 16px The expected result should be 16 / 23 => 0.695rem Question: I am looking for ...

css - Tips for reducing the speed of inertia/momentum scrolling on mobile devices

I have been working on creating a scrollable card gallery using a CSS grid layout. The structure I am following is similar to this: <div class="gallery-wrapper"> <div class="gallery-container"> <div id="card-1" class="gallery-ca ...

Dynamically scrolling an element using jQuery without specifying specific values

I need to keep an element fixed when scrolling on a page without setting specific height values. Do you know of any way to achieve this? Below is the code for reference: // Keep the orange box at the top after scrolling to a certain extent $(window).sc ...

Aligning text in a vertical progress bar using absolute positioning

Currently, I am trying to create a health bar-like element but I am facing difficulties in centering the text within it. Despite exhausting all my options, the text is either off-center when its length changes or extends beyond the borders of the bar. Her ...

The border on the right side of the accordion does not continue down

After creating an HTML page with an accordion in one div and a menu in another, I added a simple border styling to the right side of the menu using CSS properties like height, border-right-width, border-right-style, and border-right-color. height:100%; b ...

The CSS effect fails to take effect following an AJAX response

After a successful ajax response, I am attempting to apply a jQuery .css effect. However, it seems that this process is not working as expected because I am trying to retrieve the height of a newly created element and then applying it to another newly crea ...

Spin the text inside an <li> element generated by JavaScript

I am currently working on a unique script designed to create a custom number of slices for a circle. The items in the circle are being displayed as shown in this generated circle image. This is the Javascript code I have been using: for () { men ...

Modifying the Header Background Color

Looking for help on my forum at The header background on my site needs fixing. I am trying to change the entire header background to match the color of the logo background, but I'm unsure what codes need to be adjusted. Can anyone provide guidance? ...

Issue with Material UI Button when implementing createMuiTheme causing error

What could be causing this error to occur? const buttonStyle = createMuiTheme({Button: {borderRadius: 100%},}); ...

When a user scrolls over an element with a data-attribute,

Looking to create a dynamic header effect on scroll? I have two headers, menu1 by default and menu2 hidden with display:none. In specific sections of my website, I've added a special attribute (data-ix="change-header") to trigger the header change. T ...

Reducing the Bootstrap Navbar Collapse Width

After spending all day searching for an answer, I still haven't found a solution to my problem. Here is the code I am struggling with: <div class="visible-xs navbar navbar-ti navbar-fixed-top "> <div class="container"> <div class ...

Switching ng-Idle countdown time from seconds to minutes is possible by adjusting the configuration settings

I have implemented ng-idle in my application, where the warning popup appears after 10 seconds with the message: "Your session will be close in 10 seconds" However, I need to change this to minutes. The session should be closed in 5 minutes instead. How ...

Achieve consistent column heights with flexbox styling

I have a solution for achieving equal height columns using the CSS property display:table. Here is an example: .row { display: table; width: 100%; } .col{ display: table-cell; } However, in my case, I am using flexbox and the row class has ...

When flex items are stacked in a column, the bottom portion may be truncated

(Edit) Check out the codepen example here. I am working on creating a stack of cards using Vue.js and flexbox to showcase different apps on my website. Each card is represented by a component and I utilize Vue's `for` function to display cards with t ...

Bar graph constructed using a pair of div elements

I extracted two values from an array: $target = $data->data[2][32][3]; For this particular example, $target = 9.83%. $clicks = $data->data[1][32][3]; And in this case, $clicks = 7.15%. I have created a bar-like chart using three main div elements ...

JavaScript accordions failing to open

I've encountered an issue with my website that includes JS accordions. Strangely, they are not opening on the live site, but they function properly on Codepen. I checked the console in Chrome and found no error messages, however, when I looked at the ...