我正在制作一个带样式的简单HTML。我希望每个部分都有不同不透明度的颜色叠加。叠加层应该是覆盖整个屏幕但不包括顶部导航栏的全宽度,但在我的样本工作html中它也完全覆盖了顶部区域。我还注意到,当我在下面添加另一部分时,它将与第一部分放在同一位置。
每个部分都应该有不同的不透明度。
我设法通过按导航栏的高度调整顶部来修复它。
@import url('https://fonts.googleapis.com/css?family=Oswald:700');
html {
background: url(https://images.pexels.com/photos/28026/pexels-photo-28026.jpg?w=1260&h=750&auto=compress&cs=tinysrgb) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
margin: 0;
font-family: 'Oswald', sans-serif;
}
h1 {
color: #cccccc;
}
/* Remove margins and padding from the list, and add a black background color */
ul.topnav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
/* Float the list items side by side */
ul.topnav li {
float: left;
}
/* Style the links inside the list items */
ul.topnav li a {
display: inline-block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
transition: 0.3s;
font-size: 17px;
}
/* Change background color of links on hover */
ul.topnav li a:hover {
background-color: #555;
}
/* Hide the list item that contains the link that should open and close the topnav on small screens */
ul.topnav li.icon {
display: none;
}
.spacer {
margin-bottom: 20%;
}
.primary-head {
font-size: 70px;
font-weight: 700;
}
.sub-head {
font-size: 50px;
font-weight: 700;
color: #9b9595;
margin-top: -70px;
}
.section-one__overlay {
position: absolute;
padding: 0;
margin: 0;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(255,255,255,0.5);
}

<nav>
<ul class="topnav" id="myTopnav">
<li>
<a href="" title="">Home</a>
</li>
<li>
<a href="" title="">About</a>
</li>
</ul>
</nav>
<div class="section-one">
<div class="section-one__overlay">
<div class="spacer"></div>
<h1 class="primary-head">Lorem ipsum dolor.</h1>
<h2 class="sub-head">Lorem ipsum dolor sit.</h2>
</div>
</div>
&#13;
答案 0 :(得分:1)
您可以将叠加层section-one__overlay
的顶部设置为导航栏的高度。
.section-one__overlay {
position: absolute;
padding: 0;
margin: 0;
/*-- change top --*/
top: 53px;
left: 0;
width: 100%;
height: 100%;
background: rgba(255,255,255,0.5);
}
@import url('https://fonts.googleapis.com/css?family=Oswald:700');
html {
background: url(https://images.pexels.com/photos/28026/pexels-photo-28026.jpg?w=1260&h=750&auto=compress&cs=tinysrgb) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
margin: 0;
font-family: 'Oswald', sans-serif;
}
h1 {
color: #cccccc;
}
/* Remove margins and padding from the list, and add a black background color */
ul.topnav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
/* Float the list items side by side */
ul.topnav li {
float: left;
}
/* Style the links inside the list items */
ul.topnav li a {
display: inline-block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
transition: 0.3s;
font-size: 17px;
}
/* Change background color of links on hover */
ul.topnav li a:hover {
background-color: #555;
}
/* Hide the list item that contains the link that should open and close the topnav on small screens */
ul.topnav li.icon {
display: none;
}
.spacer {
margin-bottom: 20%;
}
.primary-head {
font-size: 70px;
font-weight: 700;
}
.sub-head {
font-size: 50px;
font-weight: 700;
color: #9b9595;
margin-top: -70px;
}
.section-one__overlay {
position: absolute;
padding: 0;
margin: 0;
top: 53px;
left: 0;
width: 100%;
height: 100%;
background: rgba(255,255,255,0.5);
}
&#13;
<nav>
<ul class="topnav" id="myTopnav">
<li>
<a href="" title="">Home</a>
</li>
<li>
<a href="" title="">About</a>
</li>
</ul>
</nav>
<div class="section-one">
<div class="section-one__overlay">
<div class="spacer"></div>
<h1 class="primary-head">Lorem ipsum dolor.</h1>
<h2 class="sub-head">Lorem ipsum dolor sit.</h2>
</div>
</div>
&#13;
答案 1 :(得分:1)
@import url('https://fonts.googleapis.com/css?family=Oswald:700');
html {
background: url(https://images.pexels.com/photos/28026/pexels-photo-28026.jpg?w=1260&h=750&auto=compress&cs=tinysrgb) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
margin: 0;
font-family: 'Oswald', sans-serif;
}
h1{
color: #cccccc;
}
/* Remove margins and padding from the list, and add a black background color */
ul.topnav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
/* Float the list items side by side */
ul.topnav li {float: left;}
/* Style the links inside the list items */
ul.topnav li a {
display: inline-block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
transition: 0.3s;
font-size: 17px;
}
/* Change background color of links on hover */
ul.topnav li a:hover {background-color: #555;}
/* Hide the list item that contains the link that should open and close the topnav on small screens */
ul.topnav li.icon {display: none;}
.spacer {
margin-bottom: 20%;
}
.primary-head {
font-size: 70px;
font-weight: 700;
}
.sub-head {
font-size: 50px;
font-weight: 700;
color: #9b9595;
margin-top: -70px;
}
.section-one__overlay {
position: absolute;
padding:0;
margin:0;
top:0;
left:0;
width: 100%;
height: 100%;
background:rgba(255,255,255,0.5);
z-index:100;
}
nav{
z-index: 10000;
position: relative;
}
&#13;
<nav>
<ul class="topnav" id="myTopnav">
<li><a href="" title="">Home</a></li>
<li><a href="" title="">About</a></li>
</ul>
</nav>
<div class="section-one">
<div class="section-one__overlay">
<div class="spacer"></div>
<h1 class="primary-head">Lorem ipsum dolor.</h1>
<h2 class="sub-head">Lorem ipsum dolor sit.</h2>
</div>
</div>
&#13;
请替换(.section-one__overlay)并在项目中添加(nav)以下代码
.section-one__overlay {
position: absolute;
padding:0;
margin:0;
top:0;
left:0;
width: 100%;
height: 100%;
background:rgba(255,255,255,0.5);
z-index:100;
}
nav{
z-index: 10000;
position: relative;
}
答案 2 :(得分:1)
这是你想要做的事吗?
@import url('https://fonts.googleapis.com/css?family=Oswald:700');
html {
background: url(https://images.pexels.com/photos/28026/pexels-photo-28026.jpg?w=1260&h=750&auto=compress&cs=tinysrgb) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
margin: 0;
font-family: 'Oswald', sans-serif;
}
h1 {
color: #cccccc;
}
/* Remove margins and padding from the list, and add a black background color */
ul.topnav {
list-style-type: none;
margin: 0;
padding: 0;
position:relative;
overflow: hidden;
background-color: #333;
}
/* Float the list items side by side */
ul.topnav li {
float: left;
}
/* Style the links inside the list items */
ul.topnav li a {
display: inline-block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
transition: 0.3s;
font-size: 17px;
}
/* Change background color of links on hover */
ul.topnav li a:hover {
background-color: #555;
}
/* Hide the list item that contains the link that should open and close the topnav on small screens */
ul.topnav li.icon {
display: none;
}
.spacer {
margin-bottom: 18%;
}
.primary-head {
font-size: 70px;
font-weight: 700;
}
.sub-head {
font-size: 50px;
font-weight: 700;
color: #9b9595;
margin-top: -70px;
}
.section-one{
position:relative;
min-height:150px;
}
.section-one__overlay {
position: absolute;
padding: 0;
margin: 0;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(255,255,255,0.5);
}
.section-two__overlay {
rgba(255,255,255,0.8);
}
&#13;
<nav>
<ul class="topnav" id="myTopnav">
<li>
<a href="" title="">Home</a>
</li>
<li>
<a href="" title="">About</a>
</li>
</ul>
</nav>
<div class="section-one">
<div class="section-one__overlay">
<div class="spacer"></div>
<h1 class="primary-head">Lorem ipsum dolor.</h1>
<h2 class="sub-head">Lorem ipsum dolor sit.</h2>
</div>
</div>
<div class="section-one ">
<div class="section-one__overlay section-two__overlay">
<div class="spacer"></div>
<h1 class="primary-head">Lorem ipsum dolor.asd</h1>
<h2 class="sub-head">Lorem ipsum dolor sit.</h2>
</div>
</div>
&#13;