Beispiel:CSS2 z-index3.html
Aus SELFHTML-Wiki
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" media="screen" href="./Beispiel:SELFHTML-Beispiel-Grundlayout.css" >
<title>Stapelkontext - 2</title>
<style>
.rot {
z-index: 1;
}
.blau {
z-index: auto;
}
.gold {
z-index: auto;
}
/* -------- */
.rot,.gold,.blau {
position: absolute;
width: 8em;
aspect-ratio: 1/1;
}
.rot {
top: 5em;
left: 4em;
background: red;
}
.blau {
top: 7em;
left: 7em;
background: steelblue;
}
.gold {
top: 10em;
left: 10em;
background: gold;
border: thin solid steelblue;
}
p {margin-top:15em;}
body{ margin: 0;padding: 1em;}
</style>
<script>
document.addEventListener('DOMContentLoaded', function () {
document.querySelector("button").addEventListener("click", function () {
const wrapper1 = document.querySelector(".wrapper1");
if (wrapper1) {
const currentOpacity = getComputedStyle(wrapper1).opacity;
wrapper1.style.opacity = currentOpacity === "0.99" ? "1" : "0.99";
}
});
});
</script>
</head>
<body>
<h1>Stapelkontexte - 2</h1>
<div class="wrapper1">
<div class="rot"></div>
</div>
<div class="wrapper2">
<div class="blau"></div>
</div>
<div class="wrapper3">
<div class="gold"></div>
</div>
<p>Eigentlich werden die Boxen in der Reihenfolge rot, blau, gold gezeichnet.<br>Durch <code>z-index: 1;</code> wird die rote Box nach vorne geholt.<br>Wie kann man sie ohne z-index und ohne weiteres HTML wieder nach hinten bewegen?<br><button>Umschalten</button></p>
</body>
</html>