Hover 및 :focus-within, Focus 상태 및 ARIA가 포함된 간단한 CSS 드롭다운 메뉴
<style>
a {
text-decoration: none;
}
nav {
font-family: monospace;
}
ul {
background: darkorange;
list-style: none;
margin: 0;
padding-left: 0;
}
li {
color: #fff;
background: darkorange;
display: block;
float: left;
padding: 1rem;
position: relative;
text-decoration: none;
transition-duration: 0.5s;
}
li a {
color: #fff;
}
li:hover,
li:focus-within {
background: red;
cursor: pointer;
}
li:focus-within a {
outline: none;
}
ul li ul {
background: orange;
visibility: hidden;
opacity: 0;
min-width: 5rem;
position: absolute;
transition: all 0.5s ease;
margin-top: 1rem;
left: 0;
display: none;
}
ul li:hover > ul,
ul li:focus-within > ul,
ul li ul:hover,
ul li ul:focus {
visibility: visible;
opacity: 1;
display: block;
}
ul li ul li {
clear: both;
width: 100%;
}
</style>
<nav role="navigation">
<ul>
<li><a href="#">One</a></li>
<li><a href="#" aria-haspopup="true">Two</a>
<ul class="dropdown" aria-label="submenu">
<li><a href="#">Sub-1</a></li>
<li><a href="#">Sub-2</a></li>
<li><a href="#">Sub-3</a></li>
</ul>
</li>
<li><a href="#">Three</a></li>
</ul>
</nav>
in
이 옵션을 사용하면 사용자가 드롭다운 버튼을 클릭하여 하위 메뉴를 표시합니다.
<style>
body {background-color : #ededed; font-family : "Open Sans", sans-serif;}
h1 {padding: 40px; text-align: center; font-size: 1.5em;}
li a {text-decoration : none; color : #2d2f31;}
nav {
width : 300px;
background: #d9d9d9;
margin : 40px auto;
}
span {
padding : 30px;
background : #2d2f31;
color : white;
font-size : 1.2em;
font-variant : small-caps;
cursor : pointer;
display: block;
}
span::after {
float: right;
right: 10%;
content: "+";
}
.slide {
clear:both;
width:100%;
height:0px;
overflow: hidden;
text-align: center;
transition: height .4s ease;
}
.slide li {padding : 30px;}
#touch {position: absolute; opacity: 0; height: 0px;}
#touch:checked + .slide {height: 300px;}
</style>
<h1>CSS Dropdown Menu</h1>
<nav>
<label for="touch"><span>titre</span></label>
<input type="checkbox" id="touch">
<ul class="slide">
<li><a href="#">Lorem Ipsum</a></li>
<li><a href="#">Lorem Ipsum</a></li>
<li><a href="#">Lorem Ipsum</a></li>
<li><a href="#">Lorem Ipsum</a></li>
</ul>
</nav>
HTML