Z-index not working to keep an image on top… Toggle on/off image, menu gets hidden

纵饮孤独 提交于 2020-06-01 05:09:28

问题


So I am using the input checkbox to toggle on/off the mascot logo on top of the main container div's background. The logo has to start in the top 0 left 0 just like the background but when I toggle it on to show the mascot logo the menu with the check boxes is hidden.

My z-index won't work and I am looking for any solutions to keep the menu group of checkboxes on top of all.

Thanks

body, html {
  height: 100%;
}

#map {
  background-image: url(https://image.freepik.com/free-vector/vector-illustration-mountain-landscape_1441-71.jpg);
  height: 1080px;
  width: 1080px;
  background-position: center;
  background-repeat: no-repeat;
  background-size: 1080px 1080px;
  position: relative;
  }

  #menu {
    display: block;
    width: 100px;
  }

input:checked + .hidable {
    display: none;
    position: absolute;
    z-index: 10;
}
  
input:not(:checked) + .showable {
    display: none;
    position: absolute;
    z-index: 10;
  }

  #mascot1 {
    position: absolute;
    top: 0px;
    left: 0px;
    z-index: 1;
  }
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<title>Splash Page</title>
</head>
<body>

  <div id="map">
    <div id="menu">
      <input type="checkbox" /> Mascot 1
      <div class="showable"><img id="mascot1" src="https://banner2.cleanpng.com/20180510/fkq/kisspng-tiger-mascot-5af4168e2aa482.2537194215259459981747.jpg" alt="Map Locations"></div>
      <input type="checkbox" /> Mascot 2
      <div class="showable"><img id="mascot2" src="https://banner2.cleanpng.com/20180510/fkq/kisspng-tiger-mascot-5af4168e2aa482.2537194215259459981747.jpg" alt="Map Locations"></div>
      <input type="checkbox" /> Mascot 3
      <div class="showable"><img id="mascot3" src="https://banner2.cleanpng.com/20180510/fkq/kisspng-tiger-mascot-5af4168e2aa482.2537194215259459981747.jpg" alt="Map Locations"></div>
    </div>
  </div>



</body>
</html>

回答1:


add a negative z-index to all the image and z-index:0 to the main wrapper. You can then remove all the other z-index

body,
html {
  height: 100%;
}

#map {
  background-image: url(https://image.freepik.com/free-vector/vector-illustration-mountain-landscape_1441-71.jpg);
  height: 1080px;
  width: 1080px;
  background-position: center;
  background-repeat: no-repeat;
  background-size: 1080px 1080px;
  position: relative;
  z-index: 0
}

#menu {
  display: grid;
  grid-template-columns:auto 1fr;
}

input:checked+.hidable,
input:not(:checked)+.showable {
  display: none;
}

.showable {
  position: absolute;
  top: 0px;
  left: 0px;
  z-index: -1;
}
<div id="map">
    <div id="menu">
      <input type="checkbox" /> Mascot 1 long text here
      <div class="showable"><img id="mascot1" src="https://banner2.cleanpng.com/20180510/fkq/kisspng-tiger-mascot-5af4168e2aa482.2537194215259459981747.jpg" alt="Map Locations"></div>
      <input type="checkbox" /> Mascot 2
      <div class="showable"><img id="mascot2" src="https://banner2.cleanpng.com/20180510/fkq/kisspng-tiger-mascot-5af4168e2aa482.2537194215259459981747.jpg" alt="Map Locations"></div>
      <input type="checkbox" /> Mascot 3
      <div class="showable"><img id="mascot3" src="https://banner2.cleanpng.com/20180510/fkq/kisspng-tiger-mascot-5af4168e2aa482.2537194215259459981747.jpg" alt="Map Locations"></div>
    </div>
  </div>

Related for mroe details: Why can't an element with a z-index value cover its child?




回答2:


Did you want it to be this way ?

body,
html {
  height: 100%;
}

#map {
  background-image: url(https://image.freepik.com/free-vector/vector-illustration-mountain-landscape_1441-71.jpg);
  height: 1080px;
  width: 1080px;
  background-position: center;
  background-repeat: no-repeat;
  background-size: 1080px 1080px;
  position: relative;
}

#menu {
  display: block;
  width: 100px;
}

input:checked+.hidable {
  display: none;
  position: absolute;
  z-index: 10;
}

input:not(:checked)+.showable {
  display: none;
  position: absolute;
  z-index: 10;
}
<div id="map">
  <div id="menu"><input type="checkbox" />Mascot 1
    <div class="showable"><img id="mascot1" src="https://banner2.cleanpng.com/20180510/fkq/kisspng-tiger-mascot-5af4168e2aa482.2537194215259459981747.jpg" alt="Map Locations"></div><input type="checkbox" />Mascot 2
    <div class="showable"><img id="mascot2" src="https://banner2.cleanpng.com/20180510/fkq/kisspng-tiger-mascot-5af4168e2aa482.2537194215259459981747.jpg" alt="Map Locations"></div><input type="checkbox" />Mascot 3
    <div class="showable"><img id="mascot3" src="https://banner2.cleanpng.com/20180510/fkq/kisspng-tiger-mascot-5af4168e2aa482.2537194215259459981747.jpg" alt="Map Locations"></div>
  </div>
</div>


来源:https://stackoverflow.com/questions/62094915/z-index-not-working-to-keep-an-image-on-top-toggle-on-off-image-menu-gets-hi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!