0

I want to fit a YouTube iframe embed in the middle of a page (70% width) so I'm doing this:

.wrapper {
    margin: auto;
    width: 70%;
}

.wrapper iframe {   
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
}

The problem is, I'm getting black bars around the video to fill the available space. If instead I used padding (as in this answer) the iframe would only fit horizontally and might require scrolling in the vertical direction. Any idea what might help? (older browser support is not an issue)

Edit: I know the aspect ratio of the video I want to play.

Vojtěch Strnad
  • 2,420
  • 2
  • 10
  • 15

2 Answers2

0

You can try to use flex css for this issue. you can use the static height width to the iframe as per your requirement

.wrapper  {
display: flex;
align-items: center;
justify-content: center;
}
.wrapper iframe{
width: 100px;
height: 100px;
}
0

Black bars appear when the height/width ratio of the iframe is too high (over .5, approximately).

Use this CSS to maintain a fixed height/width ratio, even when the wrapper is resized:

.wrapper {
  width: 70%;
  padding-top: 35%; // 35/70 = 0.5, the ratio can be changed here
  margin: auto;
  position: relative;
}
.wrapper iframe {
  width: 100%;
  position: absolute;
  height: 100%;
  transform: translate(0, -100%);
}
Nino Filiu
  • 16,660
  • 11
  • 54
  • 84