Switching the arrow direction in Bootstrap 4 menu toggles

I'm a beginner with Bootstrap 4. My navigation menu includes a dropdown item with the standard caret pointing down, added automatically by Bootstrap.

Now, I want to make the caret point up after opening the dropdown menu for the nav item. Despite extensive online searching, I couldn't find a solution within Bootstrap 4 that doesn't involve font-awesome or workarounds.

Here is the HTML code for my menu (which works fine), including the dropdown item (Item 2):

<nav class="navbar navbar-expand-sm navbar-light">
  <a class="navbar-brand" href="#"><img src="img/logo.svg" width="80" height="80" alt="Logo"></a>
  <button type="button" class="navbar-toggler" data-toggle="collapse" data-target=".navbar-collapse">
    <span class="sr-only"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
  </button>
  <div class="collapse navbar-collapse">
    <div class="navbar-nav">
      <a class="nav-item nav-link active" href="#">Item 1<span class="sr-only">(current)</span></a>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="" data-toggle="dropdown">Item 2</a>
        <div class="dropdown-menu">
          <a class="dropdown-item" href="#">Item 2.1</a>
          <a class="dropdown-item" href="#">Item 2.2</a>
          <a class="dropdown-item" href="#">Item 2.3</a>
        <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="#">Item 2.4</a>
        </div>
      </li>
      <a class="nav-item nav-link" href="#">Item 3</a>
      <a class="nav-item nav-link" href="#">Item 4</a>
    </div>
  </div>
</nav>

A CSS or jQuery-based solution would be great, as long as it's a clean and direct implementation within Bootstrap. Is this feasible?

If anyone can assist me in altering the caret direction from down to up when the dropdown-item opens, preferably incorporating a smooth animation, I'd greatly appreciate it! Thanks in advance!

Answer №1

If you want to change the orientation, just follow these simple steps:

.dropdown-toggle[aria-expanded="true"]:after {
  transform: rotate(180deg);
}


/*for the animation*/
.dropdown-toggle:after {
  transition: 0.5s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<nav class="navbar navbar-expand-sm navbar-light">
  <a class="navbar-brand" href="#"><img src="img/logo.svg" width="80" height="80" alt="Logo"></a>
  <button type="button" class="navbar-toggler" data-toggle="collapse" data-target=".navbar-collapse">
    <span class="sr-only"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
  </button>
  <div class="collapse navbar-collapse">
    <div class="navbar-nav">
      <a class="nav-item nav-link active" href="#">Item 1<span class="sr-only">(current)</span></a>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="" data-toggle="dropdown">Item 2</a>
        <div class="dropdown-menu">
          <a class="dropdown-item" href="#">Item 2.1</a>
          <a class="dropdown-item" href="#">Item 2.2</a>
          <a class="dropdown-item" href="#">Item 2.3</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="#">Item 2.4</a>
        </div>
      </li>
      <a class="nav-item nav-link" href="#">Item 3</a>
      <a class="nav-item nav-link" href="#">Item 4</a>
    </div>
  </div>
</nav>

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

Invisible Thinglink image revealed upon resizing browser

I am currently repurposing a pre-existing theme that has a drop-down menu showcasing an image. I am attempting to integrate a Thinglink into the content section of this drop-down, but unfortunately, the image does not appear until I adjust the size of the ...

The alignment of the table header and body becomes misaligned when attempting to freeze the header

I am currently working on making the header visible while scrolling down the page. I managed to achieve this by using display:block; in the CSS for the body element. However, I am facing an issue where the columns of the header are not aligned with the ...

Is it necessary to reset the variable following an ajax request?

There are two ajax functions in my code: $("body").on("click", ".follow", function() { var followingId = $(this).data("following"); var followData = {"following_id" : followingId} $(this).addClass('disabled'); $.ajax({ ur ...

Is it a good idea to relocate the document.ready function into its own JavaScript function?

Can the following code be placed inside a separate JavaScript function within a jQuery document.ready, allowing it to be called like any other JavaScript function? <script type="text/javascript"> $(document).ready(function() { $('div#infoi ...

How can I improve the smoothness of my CSS transition when resizing background images?

I'm currently working on creating an intro movie for a game and I thought it would be interesting to use CSS animations. However, I've noticed that some parts of the animation appear bumpy and inconsistent. It seems that depending on the transiti ...

Examine the spans and delete the first-child node

Basically, this functionality allows the user to cycle through hidden information by clicking on it. Here is how you can structure your HTML: <div id="facts"> <span>click to cycle</span> <span>fact 1</span> <s ...

Prevent a submit button from submitting a form using jQuery

I am working on developing a password manager similar to a chrome extension. The goal is to detect the username and password fields when a user tries to log in to a website, such as Facebook, and then prompt the user if they would like to save these creden ...

Is there a way to deactivate a button upon clicking and then substitute it with a new button that serves a different purpose in AngularJS?

Is there a way to deactivate a button once clicked and substitute it with another button that performs a different task using AngularJS? Here is the button in question: <button type="submit" class="btn btn-small btn-default" ng-disabled="isteam==0 || ...

Customizing the input field to have a width of 100%

Could someone provide a solution, like a link or working example, for creating input fields with rounded corners that have a width of 100% and a customizable right border? Here is the current structure I am using: <span class="rounded left-corner" ...

Ways to refresh a page once the 'ok' button is clicked with sweetalert

Hey there! I recently used sweetalert in my code. swal("Great work!", "You successfully clicked the button!", "success") This code triggers a message box with an 'okay' button. I am wondering if it is possible to automatically refresh the page ...

Layers of CSS images

Is there a way to create multiple layers for images on a webpage so they can move independently without affecting other elements? I attempted to use z-index, which did work, but it caused the page to increase in height. Additionally, when using jQuery.posi ...

the ajax success function is malfunctioning

I'm encountering an issue with my ajax function. I am trying to change the CSS for certain HTML elements on 'success', using the following code: success:function(d){ $('#name').css('weight','normal'); ...

Region Covered by Mouse Over Does Not Extend Across Whole Div

On my website, there is an arrow located on the middle right side that, when hovered over with the mouse, reveals a sidebar. Clicking on each icon in the sidebar further extends it to reveal more content. However, the issue I am facing is that the sidebar ...

Issue with Datepicker functionality within the <th> element is causing it to malfunction

Currently, I am attempting to utilize the Bootstrap datepicker, but I am facing an issue when trying to implement it within a table, specifically inside a th element. Below is the structure of my table: <table id="table" class="table table-bord ...

The Magnificent jQuery Widget Factory's _trigger Instance

When utilizing the _trigger function to initiate events, I often come across a recurring issue that I struggle to fully comprehend. The problem arises when there are multiple instances of my widget on the same page. In such cases, the most recently instan ...

Activate the last div within the identical class

When I make an ajax call to fetch more results and append them to the existing ones on the same page, I want to display a scrolling spinner while the contents are being fetched. I am adding a div at the bottom of the fetched results like this: <div cla ...

"Utilizing Bootstrap to create a visually appealing image and text combination in the

I need to arrange the image, first span, and second span in a vertical stack for xs devices. The alignment should be centered for xs devices. If you want to see my code: CLICK HERE <div class="row"> <div> <div class="col-md-9 c ...

What are the elevated sections in bootstrap?

Having come from Semantic-UI and starting to learn Bootstrap, I must say that despite its widespread popularity, I find Bootstrap a bit lacking compared to what Semantic-UI has to offer in terms of basic stylings. Currently, I am on the lookout for the Bo ...

The sub-menu options are constantly shifting... How can I keep them in place?

Here we go again... I'm having trouble with my sub-menu elements moving when I hover over them. I've searched for a solution but haven't found anything helpful. The previous advice I received about matching padding and adding transparent bo ...

Activate all inactive input and selection elements

I need a solution to enable all disabled input and select elements before submitting the form in order to retrieve the values of those disabled elements. Here is what I have tried: function enable() { $('input[type="text"], input[type="chec ...