Animating the navbar with CSS transitions

https://i.sstatic.net/cV3X6.png

Seeking advice on adding a transition effect to my navbar. Specifically, I'd like to have a colored background appear below each link when hovered over. For example, when hovering over "HOME," a yellow color should display, and it should disappear when hovering over another link that triggers a different color. Any suggestions on how to achieve this effect?

Below is the HTML code:

             <nav class="navbar navbar-default navbar-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#nav-collapse">
                <span class="icon-bar" ></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>  
            </button>
            <img class="img-responsive" src="images/brandz.png" >
        </div>

    <div class="collapse navbar-collapse" id="nav-collapse">
        <ul class="nav navbar-nav">
            <li><a href="#">Students</a></li>
            <li><a href="#">Faculty</a></li>
            <li><a href="#">About us</a></li>
            <li><a href="#">Contact us</a></li>

        </ul>

        <ul class="nav navbar-nav navbar-right">
                    <li><a data-toggle="modal" href="#myModal"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
                    <!-- Modal -->
                        <div id="myModal" class="modal fade" role="dialog">
                            <div class="modal-dialog">
                    <!-- Modal content-->
                             <div class="modal-content">
                                 <div class="modal-header">
                                     <button type="button" class="close" data-dismiss="modal">&times;</button>
                                     <img src="images/logo.png" class="logo">
                                 </div>
                                 <div class="modal-body">
                                    <p>Some text in the modal.</p>
                                 </div>
                                 <div class="modal-footer">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                 </div>
                             </div>
                            </div>
                       </div>
        </ul>
    </div>
</div>
</nav>

Here is the associated CSS code:

    .navbar-default .navbar-nav > li > a {
    font-weight: 300;
    color: #949494;
    display: block;
    padding: 3px 20px 5px 60px;
    border-bottom: 3px solid transparent;
    line-height: 97px;


    }
    .navbar-default{
    background-color:#fff;

    }
    .nav>li>a {
    position: relative;
    }
    .navbar-default .navbar-right > li > a {
    padding-left: 70px;
    padding-right: 1px;
    }
   .navbar-default .navbar-toggle .icon-bar {
    background-color: #000000;
    margin:0 0 4px;
    width: 25px;
    height: 5px;

    }
    @media (max-width: 768px) {
    .img-responsive{
     width: 300px;
     height:50px;
     padding-left:50px;
     }
     }
     @media (max-width: 376px) {
     .img-responsive{
     width: 220px;
     height:50px;
     padding-left: 20px;
     }
     } 
     @media (max-width: 286px) {
     .img-responsive{
     width: 180px;
     height:50px;
     padding-left: 5px;
     }
     }

Answer №1

To achieve this effect, add a bottom border to the element and set it as transparent initially. Apply a transition property to create a smooth effect, and finally, make it visible on hover using CSS.

Check out this example on JSFiddle

Answer №2

For those looking to change text color to yellow on hover, use the following code snippet:

ul.nav.navbar-nav li a:hover {
  color: yellow;
}

A sample fiddle is available here: https://jsfiddle.net/virginieLGB/Lpdyqoy7/

To enhance the effect with a smooth transition (the fiddle showcases an orange transition):

ul.nav.navbar-nav li a {
  transition: all 1s ease;
}
ul.nav.navbar-nav li a:hover {
  color: yellow;
}

View the updated fiddle at: https://jsfiddle.net/virginieLGB/Lpdyqoy7/1/

Adding a "line underneath" effect can be achieved with the following code:

ul.nav.navbar-nav li a {
  transition: all 1s ease;
}
ul.nav.navbar-nav li a:hover {
  color: yellow;
  border-bottom: 1px solid yellow;
}

Check out the demonstration here: https://jsfiddle.net/virginieLGB/Lpdyqoy7/2/

Answer №3

Below is a brief example of what I believe you are aiming to accomplish.

ul {
  background: #3366FF;
}
li {
  display: inline-block;
  padding: 10px 0;
}
li a {
  display: block;
  color: white;
  padding: 50px 50px;
  text-decoration: none;
  border-bottom: 2px solid transparent;
}
li a:hover {
  color: #33FF66;
  border-bottom: 2px solid #33FF66;
}
<ul>
  <li><a href="">Home</a>
  </li>
  <li><a href="">Students</a>
  </li>
  <li><a href="">Faculty</a>
  </li>
  <li><a href="">About Us</a>
  </li>
  <li><a href="">Contact Us</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

Styling each individual page in R Shiny with CSS

I've created an R shiny application and am in the process of adding styling to it. I've incorporated some HTML code and want to customize elements like the background color using CSS. After doing some research online, I learned that I should use ...

Implementing full-height list elements using CSS

I'm still facing challenges with my lists... After receiving some assistance, I was able to create a horizontal list with perfect dimensions for my needs: each element taking up 33.33% width and adjusting its height based on the tallest element. I ac ...

Why is it that when the form is submitted, the value becomes unclear?

This is a form with HTML and TypeScript code that I am working on. <form> <div class="form-group"> <input class="form-control" ([ngModel])="login" placeholder="Login" required> </div> <div class="form-group"> &l ...

issue with a non-functional sticky sidebar within a Tailwind post

I am trying to create a layout similar to this website. One of the key features I want to replicate is the sidebar that follows you as you scroll. Here is the code I have, but unfortunately, I can't seem to get it to work: <template> <di ...

The CSS styles are not immediately applied when the window is resized; they are only applied when scrolling

When utilizing the Angular approach to apply CSS, I noticed that changes are not applied when resizing the window. The canvas height adjusts correctly upon resize, but the table height only updates when we scroll the window. Is there a way to set the canva ...

I am looking to display the pop-up exclusively when the page is clicked, but unfortunately it is also appearing when the Menu is clicked

I've been working on this code and trying to make some modifications, but I just can't seem to find the right solution for my issue <!-- Updated main content --> <main> <p> Click on the menu button located in the top right ...

Incorrect form element style is only visible when contained within an unstyled HTML Details tag

A demonstration form control extracted from the Bulma CSS framework documentation performs as anticipated with Bulma 0.7.2 (most up-to-date version at the time of writing). However, when this form is contained within a standard html <details> tag, t ...

What is the best way to overlay a video onto an image using HTML and CSS?

Currently, I am experimenting with a layering technique in HTML/CSS. The idea is to place an image behind a video to enhance the overall look of the video section. Here is a sample photoshop mockup that demonstrates what I am aiming for: HTML div id= " ...

The functionality of Bootstrap v4 push and pull grid classes seems to be failing to meet expectations

In the process of developing a web application, I am using Bootstrap v4 (alpha 3) to create a responsive layout. Most elements are working well, however, I am facing difficulties rearranging my cards using the push and pull classes in Bootstrap. The intend ...

Difficulty achieving gradient effect with partial background color change in table-cell

In order to achieve a unique look for this particular design, I am seeking ways to alter the red background color of each cell in varying degrees - such as 100% for the first cell, 100% for the second cell, and 50% for the third cell. Update: After experi ...

Guide on creating an HTML5 rectangle for reuse using the Prototypal Pattern

I'm struggling to grasp Prototypal Inheritance through the use of the Prototypal pattern by creating a rectangle object and an instance of that rectangle. It seems like it should be straightforward, but I'm having trouble understanding why the Re ...

Conceal hyperlink repeatedly using jQuery

I am struggling with implementing a feature where I have two links, one displayed and the other hidden. Upon clicking the first link, it should disappear and the second link should be displayed. Clicking the second link should then hide itself and show the ...

"Media queries in CSS don't consistently apply all styles when reaching the minimum

The CSS styles I am using are as follows: @media (min-width: 1280px) { .window-padding { padding-top: 20px; padding-bottom: 20px; padding-left: 30px; padding-right: 30px; } } @media (min-width: 769px) { .windo ...

In Internet Explorer, when utilizing a table-cell with a variable height, the position should be set to absolute for

I'm struggling with getting a uniform height in my horizontal layout using display: table-cell. While it looks great in Chrome, I can't seem to get Internet Explorer to display it the same way. Check out this link for reference This is how I wa ...

navbar item arranged in a row using Bootstrap 5

I am looking to create a fixed top navbar with just one row, instead of two rows. Here is the code I currently have: <div class="col-md-3"> <ul class="navbar-nav mb-2 mb-lg-0"> <li class="nav-item"&g ...

How to concatenate a string variable to a hyperlink in a template file

Within my project, there is a file named registration_email.tpl that is passed as email_template in the bottle.template() function. This involves passing variables such as username, email_addr, role, creation_date, and registration_code. email_text = ...

Troubleshooting issue: Angular not resolving controller dependency in nested route when used with requirejs

When the routes are multiple levels, such as http://www.example.com/profile/view, the RequireJS is failing to resolve dependencies properly. However, if the route is just http://www.example.com/view, the controller dependency is resolved correctly. Below ...

If I create an App with Phonegap, HTML5, and Websql, what happens to the app's data when the user restarts their mobile device?

My application utilizes HTML5's WebSQL to store temporary notes and bookmarks, which will later be synchronized with the server. I am curious whether data stored in WebSQL will be lost upon mobile restart or cache clear, similar to how browsers on ph ...

Tips for setting up styles for h:panelgrid in JSF

When using a simple h:panelGrid to display a table, how can I specify styles for the table to enhance its appearance? I have been researching online but am feeling overwhelmed by all the information available. <h:panelGrid id="panel" columns="2" cellp ...

An error occurred while using Phonegap's FileTransfer() method - it seems that the File

After exploring various resources and finding no solutions, I have turned to this platform for help. I am attempting to initiate a .pdf download upon clicking on the following link: <a onclick="BeginDownload()" class="link" href="#">My guides</a ...