Customize Bootstrap 4's .btn-link style to include a smooth transition for under

Recently, I've been working on customizing Bootstrap 4's .btn-link by adding a special hover effect - a line that smoothly transitions from left to right beneath the link when the mouse hovers over it.

Although things are mostly going according to plan, I've encountered a snag when dealing with a button that has a dropdown feature, like this:

<button class="btn btn-link shadow-none text-decoration-none dropdown-toggle" type="button" id="price-dropdown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            price

In this situation, the dropdown arrow appears below the text. How can I go about resolving this issue? Any suggestions would be greatly appreciated.

Below is the CSS code I'm currently using:

  .btn-link {
    position: relative;
    display: inline-block;
    cursor: pointer;
  }
  .btn-link,
  .btn-link:active,
  .btn-link:focus,
  .btn-link:hover {
    color: inherit;
    text-decoration: none;
  }
  .btn-link::after {
    content: "";
    position: absolute;
  }
  .btn-link::after {
    top: 100%;
    height: 3px;
    width: 0%;
    left: 5%;
    background-color: rgb(41, 185, 153, 0.6);
    transition: 0.5s ease all 0s;
  }
  .btn-link:hover:after {
    width: 90%;
    transition: 0.3s ease all;
  }

UPDATE:

I made a modification changing ::after to ::before, but now the alignment of the accordion image seems off.

https://jsfiddle.net/2ud7nqy0/

Answer №1

Consider relocating the animation to ::before.

Can you explain what is happening in your specific situation?

You mistakenly applied the animation to the ::after pseudo-element. The btn-link utilizes the same pseudo-element for its arrow with the following style. https://i.sstatic.net/EkCUp.png

Introducing styles to the ::after pseudo-element may lead to conflicts. To avoid this, transfer the styles to the ::before pseudo-element.

.btn-link {
  position: relative;
  display: inline-block;
  cursor: pointer;
}

.btn-link,
.btn-link:active,
.btn-link:focus,
.btn-link:hover {
  color: inherit;
  text-decoration: none;
}

.btn-link::before {
  content: "";
  position: absolute;
}

.btn-link::before {
  top: 100%;
  height: 3px;
  width: 0%;
  left: 5%;
  background-color: rgb(41, 185, 153, 0.6);
  transition: 0.5s ease all 0s;
}

.btn-link:hover:before {
  width: 90%;
  transition: 0.3s ease all;
}
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<button class="btn btn-link shadow-none text-decoration-none dropdown-toggle" type="button" id="price-dropdown"
  data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
  price
</button>

UPDATE: It is advisable to create a custom class for applying new styles. In this case, your accordion relies on the :before pseudo-element. Therefore, adding styles directly to the btn-link class could disrupt the layout. Instead, introduce a new class like btn-custom and apply the custom styles there.

See a functional example here: https://jsfiddle.net/esvj70ny/

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

What is the best way to contain several elements in a flexbox with a border that doesn't span the entire width of the page?

Just dipping my toes in the waters of web development and I've come across a challenge. I'm attempting to create a flexbox container for multiple elements with a border that doesn't stretch across the entire width of the page, but instead en ...

Images are appearing misaligned in certain sections

I have been utilizing the freewall jQuery plugin for organizing images in my website. While the layout of my first section, located at , is functioning properly, I am encountering some issues with its height. The code snippet that I am currently using is a ...

The integration of jQuery in PHP is experiencing some technical challenges

There's one issue driving me crazy: I created a left-floating file tree view and included it in my website. However, when I include my website in the file treeview, it becomes unclickable in Chrome, and in IE 9, the thumbnail part for viewing PDF docu ...

Silhouette as the backdrop image

There is a span element that I am using in the following way - <span class="tick"></span> This creates a decorative "tick" for a checkbox. The CSS code used for this is - input[type="checkbox"] + span > span.tick { display: block; ...

Create a moving background gradient with styled-components

Currently, I am working on setting the background of the <Paper /> component using Material-UI version 1.0.0-beta.25 to display a gradient of colors. The colors are dynamically added by clicking the Add button and selecting one from the color picker. ...

Is it possible to make an image gradually appear and disappear?

Is there a way to create a continuous fade in and out effect for an image, transitioning between 40% and 100% opacity? I attempted using a CSS3 opacity property, but it only allows for 0% and 100%, causing the fade effect to be ineffective. Does anyone h ...

Adjust the div height to 100% in Bootstrap to center text, even with the presence of a navbar

I'm currently using Bootstrap 5 and facing an issue with getting text centered vertically on the page. I attempted to make the container of the div take up 100% of the page by using h-100, but it resulted in overflow due to the navbar occupying some s ...

The fluid table within the Bootstrap container is not adapting to different screen

Can't figure out why the bootstrap container fluid table is not responsive. Even though I used an example of My First Bootstrap Page that works fine and is responsive, this particular table remains unresponsive. I want to avoid using a scroll but will ...

Struggling to perfectly align text in the middle of a div container that also has

Seeking help with aligning text vertically within a div that also contains an image. Previous attempts with CSS properties like vertical-align: center; and line-height:90px have proven ineffective. Removing the image allows for the successful use of line- ...

Bootstrap 4: Concealed and Revealed Columns?

Is anyone else experiencing an issue where xs is hidden in xs views? I have a hunch it might be due to changes in Bootstrap v4, but I'm curious if there's a way to still make it work without delving into the CSS. My current setup uses the default ...

Utilizing negative margins in Bootstrap for row positioning

The design of Bootstrap rows includes a margin (left and right) of -15px. This is primarily due to two factors: The .container has a padding (left and right) of 15px The col-* classes include a gutter of 15px. To prevent the empty space caused by the g ...

What is the appropriate way to add left padding or left margin to the modal footer in Bootstrap 5.3

I have been working on creating a modal that resembles a chat box. I have completed most of the work, but I am facing an issue with the input area not being fixed. My goal is to have the input text displayed on the footer of the modal, but it seems like th ...

The origin of the image is specified within the HTML document

I recently encountered an issue while trying to include images in my HTML file. When I used a local file path like background-image: url('file:///C:/Users/faycel/Desktop/site%20guide/paris.jpg'), the image displayed correctly. However, when I tri ...

Clicking two times changes the background

I am facing an issue with three boxes on my website. Each box is associated with a corresponding div. When I click on any box, the div displays and the background color turns red. However, when I click on the same box again, the div disappears but the back ...

Is there a way to modify the style value within AngularJS?

<div class="meter"> <span style="width: 13%"></span> </div> I have the following HTML code snippet. I am looking to update the width value using AngularJS. Does anyone have a solution for this issue? ...

Utilizing the Jquery hover feature to reveal or conceal an element

My Hover function is designed to display and hide sub menus when a person hovers on them. The issue I'm facing is that the menu disappears when I move the mouse down towards it. Can someone help me identify what I am doing wrong here? ...

Discovering an HTML Element in a JavaScript Array with Specific Styling: A Step-by-Step Guide

I am in the process of developing a website that consists of different sections. The concept is simple - by clicking on a button located at the bottom of the page, it will reveal the corresponding div section. For styling purposes, I have initially hidden ...

Browsing through dual snap points simultaneously on Google Chrome

I'm currently developing a website that incorporates scroll snapping for 3 viewport-sized <section> elements set up like so: html, body { scroll-behavior: smooth; scroll-snap-type: mandatory; scroll-snap-points-y: repeat(100vh); scrol ...

Sophisticated Sidebar Design using Bootstrap 5

Looking to design a responsive sidebar for navigation on my page, with selected and hover properties. Visualizing the selected option to appear like this: Selected (active) option in sidebar The goal is to have the image cover the left side of the backgr ...

Develop a dynamic button using JavaScript to fetch information from an array

I'm struggling with incorporating this button creation code into my HTML using JavaScript. The code for creating the button element in JS file looks like this: var numery = ['A','B','C']; var numer = document.createE ...