I created a basic Accordion menu using just HTML and CSS. The functionality works well, but I'm facing an issue where clicking on an open accordion doesn't close it.
Can this be achieved with CSS alone or would I need to use JavaScript?
.middle a {
text-decoration: none;
}
.menu {
width: 100%;
overflow: hidden;
}
.item {
border-top: 1px solid #2980b9;
overflow: hidden;
}
.btn {
display: block;
padding: 16px 20px;
background: #3498db;
color: white;
position: relative;
}
.btn:before {
content: "";
position: absolute;
width: 14px;
height: 14px;
background: #3498db;
left: 20px;
bottom: -7px;
transform: rotate(45deg)
}
a i {
margin-right: 10px;
margin-left: 10px
}
.smenu {
background: white;
overflow: hidden;
transition: max-height 0.3s;
max-height: 0;
}
.smenu a {
display: block;
padding: 16px 26px;
font-size: 14px;
margin: 4px 0;
position: relative;
}
.smenu a:before {
content: "";
position: absolute;
width: 6px;
height: 100%;
background: #3498db;
left: 0;
top: 0;
transition: 0.3s;
opacity: 0;
}
a:hover:before {
opacity: 1;
}
.item:target .smenu {
max-height: 100%;
}
.item {
list-style-type: none
}
<div class="middle">
<div class="menu">
<li class="item" id="aboutus">
<a href="#aboutus" class="btn">About Us</a>
<div class="smenu">
<a href="#">1</a>
<a href="#">2</a>
<a href="#">3</a>
<a href="#">4</a>
<a href="#">5</a>
<a href="#">6</a>
</div>
</li>
<li class="item" id="contactus">
<a href="#contactus" class="btn">Contact Us</a>
<div class="smenu">
<a><i class="fas fa-phone"></i>000-000-000</a>
<a><i class="fas fa-envelope-open"></i>Send Us An Email</a>
<a>ABCDEFG</a>
<a>P.O> Box 00000</a>
<a>New York, New York 000000</a>
<a><i class="fas fa-clock"></i>Monday - Friday: 9AM - 5PM ET</a>
<a><i class="fas fa-window-close"></i>Saturday-Sunday: Closed</a>
<a><i class="fas fa-desktop"></i>Online: 24/7</a>
</div>
</li>
<li class="item" id="sitelinks">
<a href="#sitelinks" class="btn">Site Links</a>
<div class="smenu">
<a href="#">1</a>
<a href="#">2</a>
<a href="#">3</a>
<a href="#">4t</a>
<a href="#">5</a>
<a href="#">6</a>
</div>
</li>
</div>
</div>