What is preventing the transition property from functioning correctly?

Is it possible to apply the transition property for ::hover and ::before at the same time, enabling dynamic changing of the width property? Right now, it only seems to work for hover.

.nav-list{
    // border: 1px blue solid;
    display: flex;
    align-items: center;
    list-style: none;
    > li {
        padding-right: 1.2rem;
        
        > a { 
            position: relative;
            text-decoration: none; 
            color: var(--Dark-grayish-blue);
            &:hover{
                color: rgb(0, 0, 0);
                transition: all 0.5s; /* working here */
                &::before{
                    position: absolute;
                    content: '';
                    left: 0;
                    top: 2.5rem;
                    width: 100%;
                    transition: width 3s; /* doesn't work here */
                    height: .3rem;
                    border-radius: .1rem;
                    background: var(--Orange);
                }
            }
        }
    }
    
}
<ul class="nav-list">
        <li class="item"><a href="#Collections" class="link">Collections</a></li>
        <li class="item"><a href="#Men" class="link">Men</a></li>
        <li class="item"><a href="#Women" class="link">Women</a></li>
        <li class="item"><a href="#About" class="link">About</a></li>
        <li class="item"><a href="#Contact" class="link">Contact</a></li>
   </ul>

Answer №1

One reason why the transition is not working is because ::before is only generated on hover. In order for the transition to function properly, ::before should be defined in advance so that it starts from a default state and then transforms based on the :hover property:

.nav-list {
  display: flex;
  align-items: center;
  list-style: none;
}

.nav-list .item {
  padding-right: 1.2rem;
}

.nav-list .item .link {
  position: relative;
  text-decoration: none;
  color: gray;
}

.nav-list .item .link::before {
  position: absolute;
  content: '';
  left: 0;
  top: 1.5rem;
  width: 100%;
  transform: scaleX(0);
  transform-origin: bottom left;
  transition: transform .5s;
  height: .3rem;
  border-radius: .1rem;
  background: orangered;
}

.nav-list .item .link:hover {
  color: rgb(0, 0, 0);
  transition: color 0.5s;
}

.nav-list .item .link:hover::before {
  transform: scaleX(1);
}
<ul class="nav-list">
  <li class="item"><a href="#Collections" class="link">Collections</a></li>
  <li class="item"><a href="#Men" class="link">Men</a></li>
  <li class="item"><a href="#Women" class="link">Women</a></li>
  <li class="item"><a href="#About" class="link">About</a></li>
  <li class="item"><a href="#Contact" class="link">Contact</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

Is there a way to modify just the homepage url of the logo on a WordPress website using the OceanWP theme?

My website, abc.com, is set up with Angular for the homepage and WordPress for the blogs. The WordPress site is located in a subfolder within abc.com. You can see the file structure in the image below. I am now looking to change only the homepage link on ...

I'm experiencing an issue where the anchor links in my Node/Express application are not properly redirecting

As I embark on my first node.js/express.js project, I am faced with a unique challenge when it comes to my directory structure. Within the index.html and favorites.html files, I have carefully included anchor tags to facilitate smooth navigation between t ...

How come my wrapper box doesn't extend to the full width of the screen when I set it to width: 100%?

Can someone explain why my wrapper box doesn't fill the whole screen when I use width: 100%, but it does when I use width: 100vw? I have set "width=device-width" on my page and the minimum width for the page is 1400px. Thank you all for your assistan ...

Issue with page not updating after local storage change and returning to previous page

I have encountered an issue where in Firefox and Safari, the value retrieved from localstorage on the first page is not updated until I manually refresh the page after setting the localstorage value on the second page. Disabling the cache did not resolve t ...

Create a dynamic layout with two navigational sub-menu blocks positioned side by side

When I hover over a navigation menu item, it brings up 6 sub menu items. I want these sub-items to appear in two blocks of 3 that are adjacent to each other. I tried using display: flex on the parent element .menu-item-1, but it doesn't seem to be wo ...

What is the best way to generate a list from a text file in javascript?

I have a document called department.txt which lists various departments: Chemistry Physics Mathematics Other I'm looking to generate a dropdown menu <select> in my HTML by importing this file. How can I achieve this using Javascript? There are ...

Items on Bootstrap have been expanded to fit larger screens

I'm currently working on a webpage () and facing an issue with the size of objects on a specific screen resolution. When users click on items in the accordions, multiple information cards are displayed. However, on larger screens, these cards appear ...

Tips for retrieving the <asp:placeholder> element within different tags

My webpage features a navigation bar with multiple options, including Register and Login. <ul> <li><asp:PlaceHolder ID="PlaceHolder2" runat="server"></asp:PlaceHolder></li> <li>Register</li> <li>Login& ...

Responsiveness of the element along the vertical axis

I have an element that, when clicked, should display additional content. However, when the headline contains a lot of text, the element does not expand vertically as it should. Thank you for your help! Check out how it looks currently This is my HTML co ...

Disabling GPS with HTML5/phonegap: A step-by-step guide

Looking to create a function that can toggle GPS on and off for both iPhone and Android devices ...

Button fails to refresh in Internet Explorer

While the button functions smoothly in Chrome and Safari, it encounters issues in Internet Explorer. In Chrome, the button changes instantly upon clicking. However, in IE, although the status changes in the database upon clicking the button, the visual cha ...

Fetching the jquery variable within an HTML document: A step-by-step guide

Here is the content of my check.js JavaScript file, which includes a function: function fun2(userid) { $.ajax({ type: 'POST', url: 'ex.php', data: {userid: userid}, success: function (data) { ...

Setting objects in parallel in HTML involves using the display property along with the value

Creating two sign-up forms using HTML and CSS Bootstrap. Want them to be parallel, tried using the position tag but there is unwanted space created between the forms. View the output How can I prevent this circle of space from being generated? <main cl ...

Looking for assistance with implementing touch gestures on an HTML website?

I am in the process of developing a web application, and I have implemented a menu bar that shifts when a user clicks on an arrow. Each click activates a new menu option. Currently, I am looking to provide users with the ability to swipe over the screen t ...

Type the vinculum symbol above a number using HTML

As a Blogger, it is crucial for me to be able to write bars above numbers. Despite numerous searches on Google, there seems to be a lack of articles addressing this specific need. Most of the resources available focus on putting bars above letters or tex ...

Contrasting JavaScript form submission with a form that includes a submit button

Could someone clarify the theoretical difference between using JavaScript to submit a form and having a form with a submit button? (I'm currently utilizing the Struts framework, but I believe that shouldn't impact the functionality.) For exampl ...

Issues with the styling of C3.js

I'm encountering some issues with a visualization I'm trying to create using C3.js. The top and side edges of my scatter chart are getting cropped (likely due to changing the radius of the bubbles), but I don't believe that should be cau ...

Update an HTML tag with a new one using PHP's DOMDocument

I'm attempting to convert all instances of <P> tags in a given document to <DIV>. Here is the code I have written for this task, however it doesn't appear to be functioning as expected: $document = new DOMDocument; $document->load ...

Can the tags beneath a div be scraped using xpath?

Consider the following: <div class="name" data-starred-src="A Full Url" data-non-starred-src="Some Other URL" data-reorderid="SomeID" data-fbid="SomeID" id="SomeID" style="position: absolute; margin-top: 0px; margin-left: 0px;"> This particular div ...

Unable to submit form when using image type buttons

Here is a basic example to demonstrate my code : if(isset($_POST['login'])) { do this } <form method='POST' action="index.php"> Username : <input type='text' name='username'/> Password : <input ty ...