Replace 'hover' functionality with a different action when another particular class is present

One of the buttons in my project looks like this:

<button type="button" class="btn btn-link btn-link-dark">
  <i class="material-icons">help</i>
</button>

By default, when I hover over it, the button turns blue. However, I want to change this behavior.

The following CSS code achieves the desired effect:

.btn-link:hover {
    color: white;
}

My challenge is that I only want this color change to occur when the btn-link-dark class is present.
I've tried these approaches separately, but they did not work as expected:

.btn-link-dark .btn-link:hover {
    color: white;
}

.btn-link-dark:hover .btn-link:hover {
    color: white;
}

I am trying to avoid adding a third class to solve this issue.

Answer №1

When you insert a space between the classes, you are utilizing the descendant combinator.

.btn-link-dark .btn-link

This translates to "An element with class .btn-link that is a descendant of an element with class .btn-link-dark". To specify that an element should have both class names, do not use a space between them, for example, .btn-link-dark.btn-link:hover:

.btn-link-dark.btn-link:hover {
  color: white;
}
<button type="button" class="btn btn-link btn-link-dark">
  <i class="material-icons">help (hover effect)</i>
</button>
<button type="button" class="btn btn-link">
  <i class="material-icons">help (no effect)</i>
</button>

Answer №2

If you need to override a style, you can use the !important; declaration.

This method is handy when you want to replace existing hover effects with your own custom styles.

 .btn-link-dark.btn-link:hover {
    color: red;
  }

  .btn-link-dark.btn-link:hover {
    color: green !important;
  }
<button type="button" class="btn btn-link btn-link-dark">
  <i class="material-icons">help</i>
</button>

Answer №3

 .btn-link.btn-link-dark:hover {
    color: blue;
  }

  .btn-link:hover {
    color: red;
  }
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<button type="button" class="btn btn-link btn-link-dark">
  <i class="material-icons">help</i>
</button>

https://jsfiddle.net/Sampath_Madhuranga/aq9Laaew/158765/

This code snippet demonstrates button link styles. Feel free to check it out!

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 alignment of data-table columns appears to be off to the right

Currently, I am working on data-tables and have successfully created one. However, I am facing an issue with aligning the data to the right of some columns. Snippet: var data=[ { "amount": 518212, "billdate": "2018-08-04", "outlet": "JAYA ...

Align the text to the left on the same line as the content

I'm trying to align text and an image on the same line, but there's some empty space at the top of the text. Is there a way to align them? <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="styl ...

JavaScript - Hover function not functioning properly

Having some difficulty with the .hover() function I am fetching dribbble shots that are displayed on page load. However, I am experiencing issues when trying to add a class via hover on these shots. Interestingly, the hover functionality works perfectly ...

The title tag appears to be malfunctioning as it is being shown as regular paragraph text on the live

After a year of programming websites, I encountered a strange issue for the first time. The text from my title tag is appearing live on my website, allowing me to click on it and highlight it. Here is the markup I used: <!doctype html> <html> ...

html distinguishing between various fields within a form

As a newcomer to website development, I am trying to enhance the readability of my form with fields. Currently, all the fields are displayed one after another without any separation. Is there a specific HTML function that can help group these fields toge ...

XPath: Begin from a specific node and retrieve all the furthest descendants starting with the second child

<?xml version="1.0"?> <shipTo country="US"> <name><strong>Alice Smith</strong></name> <street>123 Maple Street</street> <city><hi>Mill Valley</hi></city> ...

Pressing the tab key makes all placeholders vanish

case 'input': echo '<div class="col-md-3"> <div class="placeholder"> <img src="images/person.png" /> &l ...

The collapse of HTML elements converging inwards within the Bulma Framework and customized with SASS

Messy Screen Recently delving into the world of Bulma, I decided to take a shot at customizing my CSS. Unfortunately, it seems that after some tweaking, my screen layout has gone haywire. Can anyone shed light on why two elements might refuse to maintain ...

How can I create a carousel-style container in HTML that functions similarly to the one on Upwork?

Is there a way to transform my existing container into a carousel similar to the one on Upwork? https://i.sstatic.net/dJZjy.png I am looking to convert my current layout into a carousel, just like the example image provided: .flex-container { displa ...

What are the benefits of incorporating CSS into a CSS block rather than utilizing inline output with HtmlHelper in CakePHP?

Just a few days ago, I embarked on the journey of learning CakePHP through their blog tutorial. Now, I am diving into writing my own small project to gain hands-on experience with the framework. After going through their documentation, I discovered two ...

Use Bootstrap 4.3.1 to seamlessly weave text around a video on your website

I've been grappling with this, but I just can't seem to crack it. <div class="container-fluid"> <div class="row"> <div class="col-lg-4 order-1 mb-auto mt-auto"> & ...

The necessary min-width for the media query has not been implemented

Despite creating a basic example using media queries, I'm puzzled that the effect is not being applied at the exact min-width, but rather at (offset + min-width). The HTML document is currently empty. <!DOCTYPE html> <html lang=""> <h ...

To access the CSV file within Selinium using Python, simply click on the "Download CSV image"

I am brand new to working with Selenium. My current task involves downloading a csv file from a specific webpage (). The process includes selecting options from dropdown menus such as View Options Contracts for, choosing BANKNIFTY, selecting an Expiry Date ...

The React modal refuses to disappear, stubbornly clinging to the original component it sprang from

Hey there, I'm a beginner in learning React and I'm encountering an issue with my popup modal component. Even after showing the modal, I can still interact with and click on the main UI elements. Can anyone assist me with this problem? https://i ...

Is there a way to hide an HTML element from view while still allowing it to be found by the browser?

Scenario: A collection of cards with hidden names, but still necessary to search by name Picture album cards without any album names visible, only the images, yet still searchable using Ctrl+F Is there a particular feature or CSS code that would enabl ...

Obtain the background color of an element using the source attribute in Selenium WebDriver

Understanding the x-path is crucial, such as < img id="" src="/A/A/B.png" style=""> My task involves identifying the color of the image, but unfortunately, the style attribute does not provide any information about color. It only includes details a ...

The appearance of success in the form is not being adequately shown

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script> <link rel="stylesheet" href= ...

Transition smoothly between two images with CSS or jQuery

I am working on a project where I need to implement a hover effect that fades in a second image above the initial image. The challenge is to ensure that the second image remains hidden initially and smoothly fades in without causing the initial image to fa ...

Having trouble with CSS box alignment issues in the top margin

I am working on customizing a CSS box for a navigation menu. Here is the current CSS code: .navigation-div { text-align:right; margin-top:14px; box-shadow:0px 0px 10px rgba(0,0,0,0.47); padding: 0; color:#E3E3E3; background-color: ...

What seems to be the issue with the scroll bars on my website?

I recently created a website using HTML and Dreamweaver. However, I've encountered a frustrating issue where the page continues scrolling endlessly when I reach the bottom, making it impossible to scroll back up. In addition, there is an annoying ho ...