I am a bit new to HTML and CSS. I am not sure how to fix my grids with overlay overlapping my sticky navbar. I created the grid using this method:
<section id="projects">
<h2>PROJECTS</h2>
<div class="grid-container">
<div class="grid-item">
<p><b>Project 1</b></p>
<img src="image1.png" alt="image1" class="image1>
<div class="overlay">
<div class="projects-text">
Placeholder
<br><a class="repo-text" href="">Repo Link</a>
</div>
</div>
</div>
<div class="grid-item">
<p><b>Project 2</b></p>
<img src="image2.jpg" alt="image2" class="image2">
<div class="overlay">
<div class="projects-text">
Placeholder
<br><a class="repo-text" href="">Repo Link</a>
</div>
</div>
</div>
<div class="grid-item">
<p><b>Project 3</b></p>
<img src="image3.jpg" alt="image3" class="image3">
<div class="overlay">
<div class="projects-text">
Placeholder
<br><a class="repo-text" href="">Repo Link</a>
</div>
</div>
</div>
</div>
</section>
and with this CSS:
.grid-container {
display: grid;
grid-template-columns: auto auto auto;
padding: 10px;
grid-column-gap: 20px;
grid-row-gap: 20px;
}
.grid-item {
background-color: white;
border: 5px solid lavender;
border-radius: 15px;
padding: 20px;
font-size: 30px;
text-align: center;
justify-content: center;
height: 350px;
position: relative;
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: .5s ease;
background-color: lavender;
}
.grid-item:hover .overlay{
opacity: 1;
}
This is my navbar:
<nav>
<ul>
<li><a href=""></a></li>
<li><a href=""></a></li>
<li><a href=""></a></li>
<li><a href=""></a></li>
<li><a href=""></a></li>
</ul>
</nav>
nav {
overflow: hidden;
width: 100%;
}
nav ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
top: 0;
width: 100%;
position: fixed;
}
nav li {
font-family: "";
float: left;
}
nav a {
display: block;
text-align: center;
padding: 14px 16px;
text-decoration: none;
position: relative;
}
nav a::after {
content: "";
display: block;
position: absolute;
left: 0;
bottom: 0;
width: 0;
height: 5px;
transition: width 0.3s ease;
}
nav a:hover::after {
width: 100%;
}
After commenting out lines of code, I realized the problem was with the position:relative
in .grid-item, but if I remove it or change the property, the overlaying effect doesn’t work anymore. How do I fix this, or is there a better way to implement this?
2
Answers
position: fixed
takes theul
element out of normal flow, making the header collapse to 0 height and probably hiding it entirely.Using a flexbox container instead would let you split the page’s height into a static header and a body that fills the remaining space.
In fact you need grid only at nav level, if at all you desire to use grid somewhere on your page.