Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to a clickable element while still using "position:absolute"?

On desktop (or big screens) my logo is clickable.

But on mobile screens, my logo isn’t clickable.

I think it is because of the position:absolute of the logo.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But I need to keep that because otherwise, the menu isn’t centered anymore on desktop screens.

I’ve made a codepen so you can see it live : codepen.io/philiposbgos/pen/vYQNWdo

I’ve tried many other ways to center it (flex box, flex grow, text-align center, etc.) but none of them kept my menu centered on desktop excepted position:absolute.

So in order to keep my logo clickable on mobiles, I’ve tried to set my .logo class to a media query and give it a relative position.

But it still didn’t work.

I really don’t understand what is happening. I’ve been on it since this morning and can’t find a way to keep the design as it is and just make my logo clickable on mobiles screen.

Thanks in advance!

>Solution :

The logo is covered by other elements at some dimensions. Add z-index:1; to the .logo selector to position it on top of the other elements.

body {
  margin: 0;
}

header {
  background-color: #2529D8;
  height: 70px;
  padding-top: 4px;
  padding-bottom: 4px;
}

.menu {
  display: flex;
  align-items: center;
  height: 100%;
  max-width: 960px;
  padding: 0 20px;  
  position: relative;
  margin:auto;
}
.menu-items {
  list-style: none;
  margin: 0 auto;
  padding: 0;
  display: flex;
  align-items: center;
  transition: transform 0.3s ease;
}
.menu li {
  margin: 10px 10px;
}

.menu a {
  color: white;
  font-family: "Roboto", sans-serif;
  text-decoration: none;
}

.menu a:hover {
  color: white;
  text-decoration: none;
}

.hamburger-menu {
  display: none;
  cursor: pointer;
  position: relative;
  z-index: 2;
}

.hamburger-line {
  width: 20px;
  height: 2px;
  background-color: white;
  margin: 4px 0;
  transition: transform 0.3s ease;
}

#menu-toggle {
  display: none;
}

#menu-toggle:checked ~ .menu-items {
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 68px;
  left: 0;
  right: 0;
  background-color: #2529D8;
  padding: 20px;
  z-index: 1;
  transform: translateX(0%);
}

#menu-toggle:checked ~ .hamburger-menu .hamburger-line:nth-child(2) {
  opacity: 0;
}

#menu-toggle:checked ~ .hamburger-menu .hamburger-line:nth-child(1) {
  transform: rotate(-45deg) translate(-6px, 6px);
}

#menu-toggle:checked ~ .hamburger-menu .hamburger-line:nth-child(3) {
  transform: rotate(45deg) translate(-6px, -6px);
}

@media (max-width: 768px) {
  .menu {
    justify-content: flex-end;
    margin-right:30px;
  }

  .menu-items {
    display: none;
  }

  .hamburger-menu {
    display: block;
    
  }
}

.logo {
  float: left;
  height: 100%;
  max-height: 100%;
  width: auto;
  margin-left: 45px;
  flex-grow: 1;
  position: absolute;
  left: 0;
  width: 172px;
  height: 64px;
  z-index:1;
}
<header>
    <a href="https://www.gamingchairmaster.com">
      <img src="https://www.gamingchairmaster.com/wp-content/uploads/gamingchairs.png" alt="Logo" class="logo" />
    </a>
    <nav class="menu"> 
      <input type="checkbox" id="menu-toggle" />
      <label for="menu-toggle" class="hamburger-menu">
        <div class="hamburger-line"></div>
        <div class="hamburger-line"></div>
        <div class="hamburger-line"></div>
      </label>
      <ul class="menu-items">
        <li><a href="https://www.gamingchairmaster.com/reviews/">Reviews</a></li>
        <li><a href="https://www.gamingchairmaster.com/guides/">Guides</a></li>
      </ul>
    </nav>
  </header>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading