使弹出窗口的高度扩展到其子项的内容

时间:2019-02-24 10:23:10

标签: javascript html css

我想在弹出窗口中嵌入来自网站的视频。我希望视频的宽度等于视口的60%,并自动计算其高度,以保持宽高比。问题在于iframe会相应地更改宽度,但是我无法让包含的div扩展其高度来完全包含iframe。

这是我的代码:

df_b
#popup {
        display: none; /* Hidden by default */
        position: fixed; /* Stay in place */
        z-index: 20; /* Sit on top */
        padding-top: 100px; /* Location of the box */
        left: 0;
        top: 0;
        width: 100%; /* Full width */
        height: 100%; /* Full height */
        overflow: auto; /* Enable scroll if needed */
        background-color: rgb(0,0,0); /* Fallback color */
        background-color: rgba(0,0,0,0.85); /* Black w/ opacity */
    }
    
    #popup-content {
        background-color: #fefefe;
        margin: auto;
        padding: 0;
        width: 60%;
        border-radius: 10px;
        position: relative;
    }
    
    .close {
        position: absolute;
        top: 0;
        right: 0;
        color: #ddd;
        font-size: 50pt;
        text-shadow: 0 0 3px black;
    }
    
    .close:hover, .close:focus {
        color: #fff;
    }
    
    iframe {
        border-radius: 10px;
        border:0;
        padding: 60px 0;
        width: 100%;
    }

1 个答案:

答案 0 :(得分:3)

您应该使用宽高比。像这样:

#popup {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 20; /* Sit on top */
  padding-top: 100px; /* Location of the box */
  left: 0;
  top: 0;
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  overflow: auto; /* Enable scroll if needed */
  background-color: rgb(0,0,0); /* Fallback color */
  background-color: rgba(0,0,0,0.85); /* Black w/ opacity */
}

#popup-content {
  background-color: #fefefe;
  margin: auto;
  padding: 0;
  width: 60%;
  border-radius: 10px;
  position: relative;
}

.close {
  position: absolute;
  top: 0;
  right: 0;
  color: #ddd;
  font-size: 50pt;
  text-shadow: 0 0 3px black;
}

.close:hover, .close:focus {
  color: #fff;
}

.embed-container {
 position: relative;
 width: 100%;
 height: 0;
 padding-bottom: 56.27198%;
 background-size: cover;
 background-repeat: no-repeat;
 background-position: center center;
}

.embed-container iframe {
 position: absolute;
 top: 0;
 left: 0;
 width: 100%;
 height: 100%;
 border-radius: 10px;
 border:0;
 }
<div id="popup" style="display: block;">
  <div id="popup-content">
  <div class="embed-container">
    <iframe allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true" src="https://www.aparat.com/video/video/embed/videohash/5kInv/vt/frame" width="100%" height="100%"></iframe></div>   <button type="button" class="close"><strong>×</strong></button>
</div>
</div>