Z-index 无法将图像保持在顶部...切换 on/off 图像,菜单被隐藏
Z-index not working to keep an image on top... Toggle on/off image, menu gets hidden
所以我使用输入复选框来切换 on/off 主容器 div 背景顶部的吉祥物徽标。徽标必须像背景一样从顶部 0 左 0 开始,但是当我打开它以显示吉祥物徽标时,带有复选框的菜单被隐藏。
我的 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;
}
#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>
你想要这样吗?
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>
向所有图像添加负片 z-index
,向主包装添加 z-index:0
。然后您可以删除所有其他 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>
相关 mroe 详细信息:
所以我使用输入复选框来切换 on/off 主容器 div 背景顶部的吉祥物徽标。徽标必须像背景一样从顶部 0 左 0 开始,但是当我打开它以显示吉祥物徽标时,带有复选框的菜单被隐藏。
我的 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;
}
#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>
你想要这样吗?
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>
向所有图像添加负片 z-index
,向主包装添加 z-index:0
。然后您可以删除所有其他 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>
相关 mroe 详细信息: