What is the best way to incorporate the animated link hover effect I have implemented, in combination with Bootstrap's stretched link feature?

Recently, I encountered a dilemma while enhancing the hover state of a link by adding an animated underline to it.

My approach involved using absolute position in the CSS, specifically setting bottom: 0; in the ::after pseudo class.

However, another requirement arose where I needed to implement Bootstrap's stretched link feature, which utilizes top: 0; in the stretched-link::after pseudo class.

Upon applying the stretched-link class to the button, the link's hover state was activated, but the animated line overlapped the linked element, contrary to my intended effect.

This conflicting scenario has left me puzzled about the optimal solution to resolve the issue.

Here is an example of a Traditional Bootstrap 5 Card with stretched-link applied:

<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container">
  <div class="row">
    <div class="col-sm-6 col-md-4 col-lg-4 d-flex align-items-stretch">
      <div class="card my-2">
        <img src="https://placehold.co/400x50" class="card-img-top" alt="...">
        
        <div class="card-body">
          <h5 class="card-title">Card title</h5>
          <p class="card-text">Some quick example text.</p>
          <a href="#" class="stretched-link">Go somewhere</a>
        </div>
      </div>
    </div>
  </div>
</div>

Meanwhile, here is a Traditional Bootstrap 5 Card with stretched-link and the added animated hover state:

body {
  font-size: 18px;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.container {
  max-width: 800px;
}

.container p {
  line-height: 1.5;
}

a {
  text-decoration: none;
  color: #005fec;
  outline: 0;
  position: relative;
  display: inline-block;
}

a::after {
  content: "";
  position: absolute;
  width: 100%;
  transform: scaleX(0);
  height: 1px;
  bottom: 0;
  left: 0;
  background-color: #005fec;
  transform-origin: bottom right;
  transition: transform 0.25s ease-out;
}

a:hover::after {
  transform: scaleX(1);
  transform-origin: bottom left;
}

a:focus:not(:focus-visible) {
  outline: 0;
}

a:focus-visible {
  outline: 1px solid #005fec;
  outline-offset: 2px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet" />

<div class="container">
  <div class="row">
    <div class="col-sm-6 col-md-4 col-lg-4 d-flex align-items-stretch">
      <div class="card my-2">
        <img src="https://placehold.co/400x50" class="card-img-top" alt="...">
        
        <div class="card-body">
          <h5 class="card-title">Card title</h5>
          <p class="card-text">Some quick example text.</p>
          <a href="#" class="stretched-link">Go somewhere</a>
        </div>
      </div>
    </div>
  </div>
</div>

Answer №1

To address the issue with sizing, one workaround I've found is to introduce an element with relative positioning to manage the pseudo-element. In my approach, I utilize a span nested inside the anchor tag.

Additionally, I recommend steering clear of styling entire element types. It's preferable to use a custom class or a more specific selector to avoid unintended effects elsewhere in your design.

Remember, outlines play a crucial role in enhancing accessibility. It's advisable not to eliminate them unless you have a suitable alternative for users navigating your website via keyboard input, for instance.

body {
  font-size: 18px;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.container {
  max-width: 800px;
}

.container p {
  line-height: 1.5;
}

a.fancy-underline {
  text-decoration: none;
  display: inline-block;
  color: #005fec;
  outline: 0;
}

a.fancy-underline span {
  position: relative;
}

a.fancy-underline span::before {
  content: "";
  position: absolute;
  width: 100%;
  transform: scaleX(0);
  height: 1px;
  bottom: 0;
  left: 0;
  background-color: #005fec;
  transform-origin: bottom right;
  transition: transform 0.25s ease-out;
}

a.fancy-underline:hover span::before {
  transform: scaleX(1);
  transform-origin: bottom left;
}

a:focus:not(:focus-visible) {
  outline: 0;
}

a:focus-visible {
  outline: 1px solid #005fec;
  outline-offset: 2px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet" />

<div class="container">
  <div class="row">
    <div class="col-sm-6 col-md-4 col-lg-4 d-flex align-items-stretch">
      <div class="card my-2">
        <img src="https://placehold.co/400x50" class="card-img-top" alt="...">

        <div class="card-body">
          <h5 class="card-title">Card title</h5>

          <p class="card-text">Some quick example text.</p>

          <a href="#" class="stretched-link fancy-underline">
            <span>Go somewhere</span>
          </a>
        </div>
      </div>
    </div>
  </div>
</div>

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

The styling in CSS does not accurately reflect its relationship to the parent

My code includes a div with the ID of "outer" nested inside a parent div with the ID of "Container". I'm attempting to adjust the properties of the outer div relative to its parent, meaning its width, height, and other attributes should be based on th ...

Blurring of text that occurs after resizing in CSS

When I use scale transform to zoom a div in HTML, the text inside becomes blurred. Is there a solution to make the text clear like the original? @keyframes scaleText { from { transform: scale(0.5, 0.5); } to { transform: scale(2, 2); } } ...

Locate the beginning div tag within a string

I am attempting to identify the '<video' string within this variable that includes HTML content $string="<video autoplay="autoplay" loop="" muted="" poster="" preload="auto" style="width: 354.09836065574px; height: 600px; max-height:600px; ...

Displaying two images side by side in HTML using Bootstrap

As a beginner in HTML, I am faced with the challenge of placing two images of different sizes side by side on the same row using HTML and Bootstrap. Below is my current code: <div class="row"> <div class="col-lg-6 col-md-6 col-xs-6 col-sm-6"> ...

Guide on how to retrieve additional data from the API by pressing the "Load More" button

Hello, I am working on a project where I aim to display user data from an API called https://reqres.in/api/users?page=(the page number can be 1,2 or more) and present it in an HTML table using JavaScript with promises. Currently, I have successfully popula ...

What is preventing me from accessing the sub-elements of an AngularJS controller?

I know this question has probably been asked countless times before, but I'm struggling to find the right words to explain my specific issue. Essentially, I'm having trouble accessing something like $ctrl.thing.subelement. However, the following ...

Guide for utilizing the Primary Key in a Django context view to display data in an HTML template through a Python for loop

Within my views, I have the following method along with my HTML structure: def index(request): context = { "user": User.objects.get(id = request.session['user_id']), "book": Book.objects.all(), &qu ...

how to target the last child element using CSS commands

<a><div class="myDiv"></div></a> <a><div class="myDiv"></div></a> <a><div class="myDiv"></div></a> <a><div class="myDiv"></div></a> // This specific one is what ...

Arranging elements next to each other in HTML

I'm currently in the process of building my first website as a developer and I could really use some assistance. I'm trying to align two items next to each other horizontally, but since I don't have experience with coding HTML, I'm feel ...

Why does this image slider begin with blankness?

For my recent school project, I created an image slider. However, upon starting the page or reloading it, only the arrows and dots are displayed. View screenshot of the issue Below is the code snippet: // JavaScript function to control the slideshow sho ...

Ways to customize the datetime-local format in an HTML input field

When dealing with the HTML input of datetime type, consider the following: Datefield: <input type="datetime-local" data-date="" data-date-format="DD MMMM YYYY, h:mm:ss"> The script below includes important code. $("input").val(moment().format(&apo ...

What is the best way to convert a repetitive string into a reusable function?

I am currently retrieving data from an API and I want to display it on my website in a more user-friendly manner. The challenge I'm facing is that the number of variables I need is constantly changing, along with their corresponding values. So, I&apos ...

HTML: Ensure that a user fills out a minimum of one text box before submission

<tr><td> First Name: </td><td><input type="text" name="FirstName" required></td> </tr> <tr><td> Last Name: </td><td><input type="text" name="LastName" required> </td></tr> ...

Disappearing Div Dilemma: The Mystery of the Responsive Design

I am currently facing an issue where two div elements (`.left` and `.right`) are not displayed vertically when the width value is less than 800px. Strangely, the `.left` div disappears when I try to adjust the width. I have shortened the code for clarity. ...

Safari is having trouble correctly displaying a CSS3 gradient with transparency

Here's a tricky puzzle for you. The gradient below is not displaying correctly in Safari, but it works perfectly fine in Firefox and Chrome: background: linear-gradient(transparent 124px, #de6230); I've even attempted the following fix without ...

Create a responsive design for a webpage that adjusts font size based on the dynamic type settings in iOS

Is there a way to ensure that my website's font size dynamically adjusts for iOS users? For example, if a user changes the font size in their iPhone settings (Settings > General > Accessibility > Larger Text), I want the webpage font size to ...

What is the process for using jQuery to systematically alter the src attribute of all avatar images on Twitter.com?

Summary: I am learning JavaScript and jQuery, and wanted to write a script to replace "_normal.png" with ".png" for all "img.avatar" images on Twitter.com. I encountered an error which I will explain below. Background: Practice makes perfect, so I like to ...

Adjusting scroll position delay for a fixed element when using an Android device

I am experiencing an issue with an element that is supposed to become fixed when scrolling past 145px and then unfixed when scrolling back up. While this functionality works smoothly on desktops, there seems to be a delay on mobile devices, particularly on ...

The standard bootstrap navbar does not automatically create spacing for content below it

Hey there everyone, I’m facing an issue with my responsive bootstrap menu. When I click the toggle button to open the dropdown menu, it doesn’t push the other content down like it should. Instead, it just overlaps the content. I’ve been trying to sol ...

Tips on aligning HTML divs properly while printing with jQuery using flexbox CSS

Need help with aligning div content for print media query. Created a media query to target both screen and print. Print media query is similar to the screen media query. Display is fine on the screen, but misalignment occurs when trying to print. Fiddl ...