是否在iframe后面落后?

时间:2019-04-09 21:23:39

标签: html css web iframe dropdown

我正在尝试使下拉菜单保持在页面上iframe元素上方。我知道这个问题经常被问到,但是我的问题是,当我将位置设置为相对而不是绝对时,我的iframe不再使用,所以(据我所知)我不能使用z-index。我知道我的代码很乱,我实质上只是想将视频以相同的比例保持在页面中央,同时还要保持响应速度,然后将下拉菜单保持在视频上方。

这是我的CSS

.dropbtn {
 background-color: inherit;
 font-family: 'Inconsolata', monospace;
 color: red;
 font-size: 18px;
 border: none;
}

.dropdown {
 position: relative;
 display: inline-block;
}

.dropdown-content {
 display: none;
 position: relative;
 min-width: 50px;
 background-color: white;
 padding: 10px;
}

.dropdown-content a {
 text-decoration: none;
 display: block;
 margin-top: 10px;
}

.dropdown-content a:hover { 
  color: black;
  text-shadow: 1px 1px rgba(256, 0, 0, .4);
}

.dropdown:hover .dropdown-content {display: block;}

.dropdown:hover .dropbtn {background-color: white;}

这是HTML

<nav>
  <a class="logo" href="index.html"></a>
  <div id="navbar">
  <span class="dropdown">
    <button class="dropbtn">work</a></button>
    <div class="dropdown-content">
      <a href="video.html">video</a>
      <a href="design.html">design</a>
      <a href="photo.html">photo</a>
    </div>
  </span>
  <a href="about.html">about</a>
  <a href="submit.html">submit</a>
  <a href="store.html">store</a><br>
</div>
</nav>

<style>

#vidframe {
  position: relative;
  overflow: hidden;
  padding-top: 56.25%;
}

#vid {
    position: absolute;
    top: 0;
    left: 0;
    width: 60%;
    height: 60%;
    margin-left: 20%;
    border: 0;
}

</style>

<body>
<div id="vidframe">
<iframe id="vid" src="https://www.youtube.com/embed/zb_m_ZomwHA" frameborder="0"></iframe>
</div>
</body>

1 个答案:

答案 0 :(得分:2)

这是一个基于您的代码的简单解决方案:

.dropbtn {
    background-color: inherit;
    font-family: 'Inconsolata', monospace;
    color: red;
    font-size: 18px;
    border: none;
}

.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    min-width: 50px;
    background-color: white;
    padding: 10px;
    z-index: 2;
}

.dropdown-content a {
    text-decoration: none;
    display: block;
    margin-top: 10px;
}

.dropdown-content a:hover {
    color: black;
    text-shadow: 1px 1px rgba(256, 0, 0, .4);
}

.dropdown:hover .dropdown-content {
    display: block;
}

.dropdown:hover .dropbtn {
    background-color: white;
}

#vidframe {
    overflow: hidden;
    text-align: center;
    position: relative;
    padding-bottom: 56.25%;
    padding-top: 25px;
    height: 0;
    z-index: 1;
}

#vid {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
}
<nav>
    <a class="logo" href="index.html"></a>
    <div id="navbar">
        <span class="dropdown">
    <button class="dropbtn">work</button>
    <div class="dropdown-content">
      <a href="video.html">video</a>
      <a href="design.html">design</a>
      <a href="photo.html">photo</a>
    </div>
  </span>
        <a href="about.html">about</a>
        <a href="submit.html">submit</a>
        <a href="store.html">store</a>
        <br>
    </div>
</nav>

<div id="vidframe">
    <iframe id="vid" src="https://www.youtube.com/embed/zb_m_ZomwHA" frameborder="0"></iframe>
</div>