CSS Spoilers
Кроссбраузерные спойлеры на HTML5 и CSS3 с использованием элемента details.
by Yuri Moskvin
HTML
<link rel="stylesheet" href="https://web3r.ru/css/embed/fiddle.min.css">
<script src="https://web3r.ru/js/embed/fiddle.min.js"></script>
<main>
<h2>Lorem Ipsum is simply</h2>
<details>
<summary tabindex="1"><span>Lorem Ipsum is simply dummy text of the printing</span></summary>
<p><b>Lorem Ipsum</b> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever <i>since the 1500s</i>, when an unknown printer took a galley of type and scrambled it to make a type...</p>
</details>
<details>
<summary tabindex="2"><span>Lorem Ipsum is simply</span></summary>
<p><img src="//picsum.photos/100/100/?random" alt="" /><b>Lorem Ipsum</b> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever <i>since the 1500s</i>, when an unknown printer took a
galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets
containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</details>
<details>
<summary tabindex="3"><span>Lorem Ipsum has been the industry's standard</span></summary>
<p><b>Lorem Ipsum</b> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever <i>since the 1500s</i>, when an unknown printer took a galley of type and scrambled it to make a type...</p>
</details>
<details>
<summary tabindex="4"><span>When an unknown printer took</span></summary>
<p><b>Lorem Ipsum</b> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever <i>since the 1500s</i>, when an unknown printer took a...
SCSS
// Spoiler styles
$img-marker: 'data:image/svg+xml;base64,PHN2ZyBoZWlnaHQ9IjM0IiB2aWV3Qm94PSIwIDAgMjQgMjQiIHdpZHRoPSIzNCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cGF0aCBkPSJNOC41OSAxNi4zNGw0LjU4LTQuNTktNC41OC00LjU5TDEwIDUuNzVsNiA2LTYgNnoiLz48L3N2Zz4=';
details {
position: relative;
margin-bottom: .5rem;
min-height: 1rem;
max-height: 3rem;
transition: min-height .15s linear, max-height .5s linear;
-webkit-transition: min-height .15s linear, max-height .5s linear;
will-change: max-height;
overflow: hidden;
// summary
summary {
// remove default marker
&::-webkit-details-marker { display: none; }
&::-moz-list-bullet { list-style-type: none; }
// content-fit width
display: inline-block;
// space for custom marker
padding-left: 1.5em;
// aux
cursor: pointer;
outline: 0;
transition: color .12s;
-webkit-transition: color .12s;
// border
span { border-bottom: 1px currentColor dotted; }
// custom marker
&::before {
content: "";
left: 0;
top: .4em;
position: absolute;
background: url($img-marker) no-repeat 50% 50% / 1em 1em;
width: 1em;
height: 1em;
transition: transform .1s linear;
-webkit-transition: transform .1s linear;
}
// hover
&:hover { color: #d06c6c; }
// content of spoiler
& ~ * {
padding-left: 1.5em;
opacity: 0;
transition: opacity .15s linear;
-webkit-transition: opacity .15s linear;
}
}
// opened spoiler
&[open] {
min-height: 2em;
max-height: 20em;
summary {
color: #d06c6c;
// show details content
& ~ * { opacity: 1; }
// rotate custom marker
&:before {
transform: rotate(90deg);
-webkit-transform: rotate(90deg);
-moz-transform: rotate(90deg);
}
}
}
}
// fix for ie & edge
@mixin ie_fix() {
details {
max-height: none;
...