Is there a way to trigger the end of a CSS animation based on the button's activation?

My button has a cool CSS animation:

gif_demo_css_animation

.btn_nav {
    color: black;
}

.btn_nav::after {
    content: '';
    position: absolute;
    left: 0;
    bottom: -3px; /* Adjust line position as needed */
    width: 100%;
    height: 2px; /* Line thickness */
    background-color: #689351; /* Line color */
    transition: all 0.3s ease; /* Smooth animation transition */
    opacity: 0; /* Initially invisible */
}


.btn_nav:hover::after {
    bottom: -1px; /* Line position on hover */
    opacity: 1; /* Make the line visible */
}
<button class='btn_nav'>button</button>

When I make the button active, I want this same effect to appear. Can I achieve the same result? I've tried reusing the CSS but it ends up coloring the button's background. I also attempted to use border-bottom, but it doesn't align properly.

Answer №1

It's not very common to use <button> elements instead of links for a user menu, but it is possible to style them to look like links.

Typically, buttons will inherit the system font family and size, so we usually opt for using <a> tags within <ul> and <li> tags enclosed in a <nav> tag.

To make a <button> resemble a link, you can remove its border and background, as well as override the default font settings by inheriting from the <html> or <body> font properties.

I placed your two buttons inside a <nav> tag and positioned them side by side using flexbox with slight spacing between them.

Choosing appropriate units is also crucial. Using px may not be ideal as it lacks flexibility. Opting for the em unit allows for adjustments in font size to reflect changes in padding and spacing, maintaining balance in design.

In the CSS, I've included some comments in French for reference purposes.

html {
  /* To match your animated GIF. */
  font: 20px/1.4 Helvetica, Arial, sans-serif;
}

.user-menu {
  display: flex; /* Side by side, default is row. */
  gap: .5em; /* Adding a small space between buttons. */
}

.btn_nav {
  position: relative; /* Required for absolute ::after element. */
  font: inherit; /* Prevent inheriting OS button styles. */
  color: black;
  background: none; /* Removing button's gray background. */
  border: none; /* Eliminating button borders. */
  padding: .4em 1em; /* Adding space around the button. */
  cursor: pointer; /* Changing mouse cursor to hand. */
}

.btn_nav::after {
  content: '';
  position: absolute; /* Relative to .btn_nav */
  left: 0;
  right: 0;
  bottom: -.1em; /* Position the line slightly below button base. */
  height: 2px; /* Line thickness. */
  background-color: #689351; /* Line color. */
  transition: all 0.3s ease; /* Smooth animation transition. */
  opacity: 0; /* Initially invisible. */
}


/* On hover over button. */
.btn_nav:hover::after {
  bottom: 0; /* Move line to sit on button base (consider padding). */
  opacity: 1; /* Make line visible. */
}
<nav class="user-menu">
  <button class="btn_nav">Sign Up</button>
  <button class="btn_nav">Log In</button>
</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

Tips for preserving hyperlinks while elegantly containing them within a designated space

Today, while experimenting with new angular features, I encountered an issue that is shown in the link below: https://i.sstatic.net/p4pNc.png The "tags" on the page are expanding beyond the boundaries of their parent container (the white div). They wrap, ...

Adjust the color of the hyperlink within the Lightbox feature

I am currently using Lightbox Plus. I made some changes to the link colors in the Lightbox.min.css file: .lightbox a{ color: #E74C3C; } .lightbox a:hover{ color:white; } .lightboxOverlay a:{ color: #E74C3C; } .lightboxOverlay a:hover{ color:white; ...

OnHover in CSS: Modify the properties of another element in addition to its own

Currently, I am working on developing a collapsible sidebar menu for my project. The objective is to have the text displayed when hovering over it and hidden when not in use. While I have found solutions for changing properties of other elements upon hover ...

What is the best method for designing a filtering menu with a "Narrow By" option?

Looking to create a sidebar menu similar to the functionality on mcmaster.com. This specific feature allows users to efficiently filter products and toggle through different options dynamically. Upon selecting the "metric" option, the entire page adjusts t ...

Having trouble getting Javascript to reveal hidden elements based on their class

I am having some trouble making specific fields in a form appear based on the selection made from a dropdown menu. Below is a simplified snippet of my code, which should change the display from 'none' to 'block'. Can you help me figure ...

Randomly select and incorporate 5 classes into the mix, ensuring they are dispersed throughout and not

Hello, I'm currently working on a project that involves a simple balloon game. However, I am facing some challenges with modifications. To illustrate, let me provide a basic example using <ul> and <li>. <ul class="balloons"> <l ...

Floating DIVs wrapped in a responsive layout

I can't help but wonder if I have a layout like this: <div class="container"> <div class="left"> Left </div> <div class="right> Right </div> </div> Changing the view port to 320 pixels requires the right div to ap ...

Issue with column-break-before toggle function only operational in Safari

In my website layout, I have arranged li elements in a 2 column design. I am looking to implement a specific functionality where if a div element (inside an li element) is clicked, it should add a class with the following CSS properties: -webkit-column-br ...

Horizontal navigation bar encroaching on slideshow graphics

I'm encountering difficulties aligning the horizontal menu below the image slider. When I have just an image without the slider, the menu aligns properly (vertically), but as soon as I introduce the code for the slider, the menu moves to the top and d ...

What sets apart a static website from a dynamic website that have both been created through coding?

Can you clarify the distinction between a static and dynamic website built using html/css/js code? Could you provide some examples to illustrate this difference? ...

What is the process for verifying which classes have been assigned to a specific component?

I am working with a Vue component and I would like to determine, from within the component itself, which classes have been applied to it. Is there a way to accomplish this? Currently, I am using a workaround where I pass the class as a prop: <my-comp ...

Tips for customizing pagination in Core UI smart table styling

Is it possible to modify the color of pagination buttons in Core UI smart table for Angular? Below is a snapshot of my code: ...

Bug: Experiencing issues with horizontal scrolling exclusively on Chrome mobile

I'm currently working on a website and I'm facing an issue with a horizontal scroll appearing only on the mobile version when using Chrome browser (Firefox works perfectly fine). Despite trying various solutions like adding overflow-x:hidden; to ...

The HR tag does not display anything following the mailing

Struggling to design a newsletter template with different lines for each product using the HR tag. However, none of my attempts with divs, tables, or CSS properties have been successful. The template looks fine in Chrome, but when sent to the provider, th ...

Challenge with correct application of Bootstrap-vue styles

I've been struggling to get bootstrap-vue up and running. Whenever I try to copy and paste the navbar component from boostrap-vue.js.org, parts of it are missing and I can't figure out why. While I manage to create router links with b-links that ...

Bottom-aligning text in Tailwind CSS

Creating two <p> tags to store text: <p v-if="my_property <= 0" class="text-green-500 text-lg font-bold">{{Math.abs(my_value)}}%</p> <p v-else class="text-red-500 text-lg font-bold">{{my_value}}%< ...

How to temporarily disable CSS hover effects

I have a menu with some links <ul id="nav"> <li> <a href="#">Project</a> <div class="subs"> <div> <ul> <li> <h3>Save</h3> <ul> <li> <a i ...

I'm looking for a way to have an element shift in a particular direction only when two keys are pressed simultaneously

Having trouble moving a square diagonally when two keys are pressed simultaneously. Trying to create an if statement that detects both key presses at the same time and triggers the movement. However, all attempts have failed so far, leaving uncertainty abo ...

How can the parent div adjust its height to match the tallest child div?

Currently working on a web project where I have a div named "page", housing two other divs - one on the left called "navigation" and the one on the right named "content". I am aiming to set the height of the "page" div to match the height of the tallest di ...

Incorporating Tailwind CSS into Vue transitions reveals an issue where the fade out effect

I'm working with Tailwind CSS and Vue.js to create a modal component. Since Tailwind doesn't natively support Vue 2, I had to implement the transitions myself. You can check out the desired effect here. Below is the code snippet: <template> ...