I am facing an issue with a bootstrap dropdown that remains open even after the screen is resized to hide the dropdown button. I have searched for a solution, but none of the answers I found have helped me so far.
Is there a way to close the dropdown or remove the "show" class for the menu based on a media query? I want the dropdown menu to close smoothly like on some other websites.
What CSS code can I use to achieve this? Currently, the dropdown stays open until it loses focus, but I want it to close automatically. Below is a snippet of the CSS I am using:
.sm-screen-dropdown-menu {
top: 48px !important;
box-shadow: rgba(0, 0, 0, 0.16) 0px 10px 36px 0px, rgba(0, 0, 0, 0.06) 0px 0px 0px 1px;
border: none;
border-radius: 0px;
padding: 0;
min-width: 200px;
}
@media (max-width: 600px) {
.hide-sidebar {
display: none !important;
}
.sm-screen-dropdown {
display: flex;
font-size: 20px;
margin-right: 15px;
margin-left: 10px;
text-decoration: none;
}
.sm-screen-dropdown:hover {
cursor: pointer;
}
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6b0904041f181f190a1b2b5e4559455b">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css" integrity="sha512-1sCRPdkRXhBV2PBLUdRb4tMg1w2YPf37qatUFeS7zlBy7jJI8Lf4VHwWfZZfpXtYSLy85pkm9GaYVYMfw5BC1A==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<div class="dropdown center">
<div class="sm-screen-dropdown" dropdown-toggle type="button" data-bs-toggle="dropdown" aria-expanded="false">
<i class="fa-solid fa-bars menu-icon"></i>
</div>
<ul class="dropdown-menu sm-screen-dropdown-menu" aria-labelledby="dropdownMenuLink">
<li>
<a class="dropdown-item" href="/">Home</a>
</li>
<div class="dropdown-divider no-margin"></div>
<div class="ms-2">
<li>
<a class="dropdown-item" href="{% url 'home' %}">Groups</a>
</li>
<li>
<a class="dropdown-item" href="{% url 'taglist' %}">Tags</a>
</li>
</div>
</ul>
</div>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="30525f5f44434442514070051e021e00">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-A3rJD856KowSb7dwlZdYEkO39Gagi7vIsF0jrRAoQmDKKtQBHUuLZ9AsSv4jD4Xa" crossorigin="anonymous"></script>