HTML 中的不透明度错误

Opacity error in HTML

我需要不透明背景,一切正常,但是当我向背景添加不透明时,我无法使用我的栏。如果您复制并粘贴代码,您会看到所有不透明度的内容,但我不能使用栏按钮,我只希望背景的不透明度为 0.5,而不是整个网站。

#background {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: url('https://i.ytimg.com/vi/3KNQfA21QyQ/maxresdefault.jpg');
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-size: 100%;
    opacity: 0.5;
    filter:alpha(opacity=80);
}
ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #333;
}

li {
    float: left;
}

li a, .dropbtn {
    display: inline-block;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
}

li a:hover, .dropdown:hover .dropbtn {
    background-color: red;
}

li.dropdown {
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    text-align: left;
}

.dropdown-content a:hover {background-color: #f1f1f1}

.dropdown:hover .dropdown-content {
    display: block;
}
<ul>
  <li class="dropdown">
    <a href="javascript:void(0)" class="dropbtn">Dropdown</a>
    <div class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
 </li>
   <li class="dropdown">
     <a href="javascript:void(0)" class="dropbtn">Home</a>
    <div class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
 </li>
    <li class="dropdown">
     <a href="javascript:void(0)" class="dropbtn">News</a>
    <div class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </li>
</ul>

你的CSS是"okay",乱七八糟的是HTML。首先尝试在整个结构之前重新排列此 body:

<body class="my-container" bgcolor="black">
<ul>
<li class="dropdown">
<a href="javascript:void(0)" class="dropbtn">dropdown</a>
<div class="dropdown-content">
  <a href="#">Link 1</a>
  <a href="#">Link 1</a>
  <a href="#">Link 1</a>
</div>
</li>
</ul>
<div id="background"></div>
</body>

然后,将#background z-index 设置为低于整个结构:

ul { position: relative; z-index: 3; }
#background { position: fixed; z-index: 1; }