Replace the arrow and implement collapsible div using CSS

My HTML code has the following structure:

<ul id="urlcss">
   <li class="nav-submenu">
      <a class="collapsed" href="#" data-toggle="collapse" data-target="#submenu0">
         ADMIN
      </a>
      <div class="collapse" id="submenu0" aria-expanded="false">
         // Some menus check the image below
      </div>
   </li>
</ul>

When viewed in a browser, it appears like this:
https://i.sstatic.net/LtXvL.png

The collapsed menu is shown like this:
https://i.sstatic.net/kqp6S.png

I am trying to add an arrow and change the background color when a user clicks on the li with nav-submenu class. I attempted using CSS but no changes occurred. This is what I want (Look at the arrow):
https://i.sstatic.net/AU4Nl.png

I tried adding this code snippet to create a holder-like effect, but it did not work:

.nav-submenu[data-toggle].collapsed:after {
    content: " ▾";
}
.nav-submenu[data-toggle]:not(.collapsed):after {
    content: " ▴";
}

Answer №1

Here is a sample I have prepared for you, along with a small snippet of JavaScript code to toggle the default class name.

const menus = document.querySelectorAll('[data-toggle="collapse']')
for (const menu of menus) {
  menu.addEventListener('click', function(event) {
    this.classList.toggle('collapsed');
  })
}
.nav-submenu a.collapsed:after {
    content: " ▾";
}
.nav-submenu a:not(.collapsed):after {
    content: " ▴";
}
.nav-submenu a:not(.collapsed)+div {
   display:none;
}
<ul id="urlcss">
   <li class="nav-submenu">
      <a class="" href="#" data-toggle="collapse">
         ADMIN
      </a>
      <div>
         // Some menus check the image below
      </div>
   </li>
</ul>

Answer №2

This example showcases the use of Bootstrap 5 for creating a navigation bar with dropdown menus. The minimal amount of CSS required makes it easy to achieve your desired look.

.nav-link.dropdown-toggle:after {
  transform: rotateX(-180deg);
  transition: transform 500ms;
}

.nav-link.dropdown-toggle.show:after {
  transform: rotateX(0deg);
}

.nav .nav-item:hover{
  transition: background-color 500ms
}

.nav .nav-item:hover{
  background-color: lightblue;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-iYQeCzEYFbKjA/T2uDLTpkwGzCiq6soy8tYaI1GyVh/UjpbCx/TYkiZhlZB6+fzT" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5/dist/js/bootstrap.bundle.min.js" integrity="sha384-u1OknCvxWvY5kfmNBILK2hRnQC3Pr17a+RTT6rIHI7NnikvbZlHgTPOOmMi466C8" crossorigin="anonymous"></script>

<ul class="nav flex-column">
  <li class="nav-item">
    <a class="nav-link active" aria-current="page" href="#">Active</a>
  </li>
  <li class="nav-item dropdown">
    <a class="nav-link dropdown-toggle" data-bs-toggle="dropdown" href="#" role="button" aria-expanded="false">Dropdown</a>
    <ul class="dropdown-menu">
      <li><a class="dropdown-item" href="#">Action</a></li>
      <li><a class="dropdown-item" href="#">Another action</a></li>
      <li><a class="dropdown-item" href="#">Something else here</a></li>
      <li>
        <hr class="dropdown-divider">
      </li>
      <li><a class="dropdown-item" href="#">Separated link</a></li>
    </ul>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#">Link</a>
  </li>
  <li class="nav-item">
    <a class="nav-link disabled">Disabled</a>
  </li>
</ul>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error 500: An invalid data type was encountered in an express.js node.js environment

Currently, I am in the process of developing an Authentication page using a combination of node.js, express.js, and mysql2. The user ID and password entered on the webpage are then passed through app.post('/login',...). However, upon submitting t ...

The play button in the customized React AudioPlayer may sometimes need to be tapped twice in order to start

I've encountered an issue with my simple audio player (React/Vite) that transitions to the next track automatically or manually. Everything seems to be functioning correctly except for the initial press of the play button. The player opens with the pa ...

What is the best way to sum two values in an array object and then replace the first value with the result?

I have an array containing two objects: let xyz = [ {amount: 1000, rate: 4}, {amount: 100, rate: 2}, {amount: 700, rate: 1} ]; The task is to multiply amount * rate and update the values in the amount key. Desired output: let result = [ {amo ...

Determine the length of the content within an HTML container that has

I am attempting to retrieve the length of the container's content in HTML dynamically. However, I am only receiving the object instead of the desired result. <script> $(document).ready(function (e) { var adsense_box_len = $(&apo ...

This function named error is implemented in native code

My website is built in C# using asp.net. When the Onchange() event is triggered on the Dropdownlist, I call this jQuery function which displays an error: function error(){[native code]} <script type="text/javascript"> function GetDescription ...

Issue with transferring JQuery variable to JavaScript

Using JQuery, I am attempting to retrieve a variable from PHP located at a specific URL and then utilize that variable in JavaScript to modify the content on the webpage. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">& ...

What's the best way to ensure a paragraph element larger than its container is centered using bootstrap?

Need help with centering a paragraph element (email) that is larger than its container: https://i.sstatic.net/f3E55.png Here's the code I have so far: <div class="col-lg-2 col-lg-offset-1 text-center"> <a href="mailto:{{ site.email }}" ...

Version 10.0 of sails is having trouble with an undefined 'schema' when using mysql

i'm currently experimenting with sails js version 0.10.0 using the sails-mysql adapter 0.10.6 I have set up two models: Customer.js module.exports = { connection: 'someMysqlServer', attributes: { name: { type: 'string& ...

Guide to verifying the scope of a dynamic array in Javascript

Hey there, I'm currently trying to verify if a value falls within a specific range in a dynamic array. Let's say I have an amount of 3555, and an array of values like [1000,2000,999999]. Typically we would use conditional statements to check for ...

There is no automatic margin calculation for the input element, however, the margin can still be adjusted using metrics and

I'm encountering an issue with the width of an input element within a table cell. The input element is defined in the following way: <td><input class="form-control input-small my-input-narrow" type="number"></td> Unfortunately, the ...

Develop a Playwright test script that includes fetch requests

My goal is to create an end-to-end test using Playwright for a website built with React. The website functions as follows: Upon initial rendering, it fetches a random fact from one API and then uses the first three words of that fact to fetch a random p ...

Utilizing props for toggling the navigation list, incorporating nested arrays or objects

My issue involves two components that are loading data. I want the links to be output like this: group1 linka linkb However, they are currently displaying like this: group1 linka group1 linkb I believe the problem lies in how I am handling the ...

Adjust the table height to 25% and include a vertical scrollbar using Bootstrap styling

I have been working on designing a web page layout that includes two status tables positioned side by side at the top. While I have successfully set this up, I am facing an issue with implementing a vertical scroll feature specifically for the right-hand t ...

Parsing Devx DataGrid filter object for generating SQL queries

Sharing the solution here in case anyone finds it helpful. This code snippet helps convert the Devx Grid Filters into a group of objects to construct the query. ...

The loading of jQuery AJAX is restricted within the <select> tag

<html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $(".region").load('getRecords.php?start=regions'); }); </script> </hea ...

Increase or decrease values by clicking on Material UI IconButton

Looking to incorporate the functionality of an onClick() event for a Material UI IconButton that can either increase or decrease the Calories value for each item in the table. I've referenced the code from the Table React component page. https://i.ss ...

External JavaScript functions remain hidden from the HTML page

Having issues with JavaScript functions. I created functions in a separate file named index.js, but when I use them in index.html like "onclick = function()", the file doesn't recognize the function. <!doctype html> <html lang="{{ app()-> ...

Uncovering the origin of a problematic included file

When using Sencha Touch, it is common to encounter issues related to missing files. In most cases, simply including the file resolves the issue. However, there are instances where the problem lies in a faulty include, requiring you to locate where the file ...

column-break-inside: prohibited; // ineffective

I'm trying to format my text into two columns while maintaining control over where the column breaks occur. Here is the CSS code I am using: body { } #content { column-count: 2; } #left_col { break-inside:avoid-column; } #ri ...

Unique styling for underlines in pop-up with custom CSS

When exploring my codesandbox project, I encountered 2 questions: How can I create a custom underline CSS similar to the UI image below the file name and trash icon? I tried using text-decoration:none, but it doesn't match the UI. How do I center a U ...