Are there any quick tricks for condensing multiple hover CSS properties into a single line of code?

Imagine wanting to apply the hover CSS property to multiple classes, you would typically have to write it like this:

a:hover, p:hover, div:hover{
// CSS properties 
}

It can become repetitive having to add ":hover" to each class. Is there a way to streamline this code by applying "hover" just once for multiple classes? Are there any shortcuts available?

Answer №1

To achieve different hover effects, you have two options: either use the same class for all hover effects like .myClass:hover {}, or use multiple classes that share a common starting text.

For example:

div[class^="icon-"]:hover {
  color: red;
}
<div class="icon-home">Div 1</div>
<div class="icon-search">Div 2</div>
<div class="icon-edit">Div 3</div>

Remember, it doesn't have to start with that text only. You can also use comparison operators like:

= = equals

^= = starts with

$= = ends with

*= = contains

~= = contains in a list

Answer №2

Currently, this feature is available only in Safari, but developers are working on implementing it for Firefox and Chrome as well.

If you want to utilize the :matches(a, p, div):hover selector, you can do so with PostCSS by using the postcss-preset-env plugin included in PostCSS.

Answer №3

According to the fundamental principles of CSS, it is not possible to achieve that. :hover and :visited are examples of CSS pseudo-classes. Since the elements in question (a, p, div) are different, they must be identified separately using a comma. If you were to write

a, p, div:hover{

it would apply the CSS styles to both a and p under normal conditions instead of only affecting div when hovered over.

Therefore, the correct way to approach this is by writing

a:hover, p:hover, div:hover

as we are specifying distinct hover states for each individual element.

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

Collapsed Grid System

Is it possible to create a CSS grid system that meets the following requirements: The grid should have arbitrary height (with consistent width) and when a grid overflows, it should stack vertically underneath the preceding grid, regardless of other grid ...

Is there a way to calculate the total of three input values and display it within a span using either JavaScript or jQuery?

I have a unique challenge where I need to only deal with positive values as input. var input = $('[name="1"],[name="2"],[name="3"]'), input1 = $('[name="1"]'), input2 = $('[name="2"]'), input3 = $('[name=" ...

Altering the "src" property of the <script> tag

Can the "src" attribute of a current <script> element be altered using Jquery.attr()? I believed it would be an easy method to enable JSONP functionality, however, I am encountering difficulties in making it operate effectively. ...

The image zoom function is malfunctioning when trying to adjust the image position

Having some trouble with a code for image zoom in/out. When I adjust the position of the image by applying margin left to either the image or the image container (#view), it affects the zoom functionality - causing the image to move to the left during zoom ...

Using Javascript to dynamically change the background image url in CSS

I've been tinkering with this for a while now. I believed this code would do the trick as I'm fetching the value from the input and setting the background-image URL to that value. Appreciate any help! The following code is within the head tag. ...

html issue with d3.js rendering images: works in firefox, but not in chrome

I am currently experiencing an issue with a D3 graphic that runs successfully in Firefox but fails to render in Chrome when using an HTML and JSON file. Upon checking the console, I see the following error message: d3.v3.min.js:1 Access to XMLHttpReque ...

Creating a custom blockquote style for a WordPress Twenty Fifteen child theme

Currently, I am creating a new child theme for Twenty Fifteen completely from scratch. The one area I am struggling with is customizing the appearance of the blockquote using CSS. Here is an example of the custom CSS I have added to the blockquotes within ...

Utilize the data storage API within Next.js or directly in the user's

Struggling to store this ini file on either the server or client, any help would be greatly appreciated. Additionally, I would like to display the ini info in the browser so that clients can easily copy and paste the information. However, I seem to be fac ...

What is the best way to design distinct buttons for various devices?

For my current responsive web design project, I have utilized Bootstrap extensively. Recently, I encountered a new issue that I'm uncertain of how to tackle. The problem arises with the following HTML code snippet: <!-- button color depending on d ...

Attempting to connect JQuery to a different page through a data attribute connection

I am currently working on a slider that connects slides from an external page through data attributes. Here is the setup: Main Page <div id="sitewrapper"> <section class="sliderwrapper"> <div id="slider" data-source="slides-pa ...

What is the best way to deactivate a submit button while an AJAX request is underway, and then reactivate it once a successful AJAX response is

I am working with the following HTML code: <form action="view_rebate_master.php" method="post"> <div class="form-group"> <label for="company_name" class="col-lg-12">Manufacturer</label> <div class="col-lg-12"> ...

Scrolling Horizontally with Bootstrap Cards

I'm trying to implement horizontally scrolling cards using Bootstrap V.5, like this: <div class="container-fluid py-2"> <div class="d-flex flex-row flex-nowrap" style="overflow: auto;"> <div cl ...

Enhance your React project by incorporating Material-UI card media elements with the ability to add

I am trying to figure out how to create an opaque colored overlay on top of an image using Material-UI. While it is possible with plain HTML, CSS, and JavaScript, I am facing some challenges with Material-UI. <Card> <CardMedia> <im ...

Choose an input that is not grouped with the rest

I am facing an issue where I need to group these three inputs together as one continuous form. However, there seems to be a problem with the select input not aligning correctly with the rest of the form. Is there something that I might be doing wrong? & ...

Troubleshooting jQuery ajax data retrieval issue in Internet Explorer 8

I'm currently utilizing jQuery submit to submit form data and retrieve it. The data is returned via ajax. When the data is received, I want to display the DIV Gallery HTML. To do this: <form action="" method="post" name="view_all" id="view_all" ...

What is the reason behind the CSS not rendering when async:false is used?

I find it peculiar and am actively seeking an explanation for this anomaly. In my code snippet, the AJAX call seems to be causing unexpected behavior: $('.edit-config').click(function() { var that = this; var msg; ip ...

Maintain consistent distance between checkboxes and their corresponding labels

I have a set of 10 checkboxes that look like this: [] Arts [] Dance [] Karate [] Volleyball [] Basketball Currently, I am using the following CSS styles: #tagstype_tags .tag_select { padding-left: 240px !important; width: 500px !impor ...

The jquery selector fails to retrieve all elements

On the upcoming web page, I am attempting to use Jquery to select all <li> elements. Specifically, I want to target all the products contained within <ul class=search-result-gridview-items">. You can find the products here: I have made attempt ...

Enhance React scrollbar functionality without relying on third-party dependencies

Currently working on enhancing the appearance of the scrollbar within my ReactJS project. Specifically, I am aiming for a design similar to this example: https://i.stack.imgur.com/2Q0P4.png Experimented with CSS properties like -webkit-scrollbar, -webki ...

PhoneGap switches up the error type with each consecutive run

Why does PhoneGap change errors after every time it is compiled? Sometimes it runs without any issues, but then the same code throws strange errors like parse error or function not found, even though no changes were made to the code. Here is the code that ...