Is there a way to link the rollover state of two elements in CSS?

I am trying to create a button with text and an icon that change appearance when hovered over individually. How can I make it so that hovering over one element also affects the other, without using JavaScript?

Update: Here is my current attempt...

<a class="close"><div class="closebutt"></div></a>

a.close {
    float:right;
    font-size:12px;
    color: #a7dbe6;
    text-decoration:underline;
}
a.close:hover  {
    color: #fff;
}
a.vs_rewardClose:before {
    content:"Close "
}
.closebutt {
    background: url(images/close.gif) no-repeat;
    display:inline-block;
    width:14px;
    height:14px;
}
.closebutt:hover {
    background-position: 0px -14px;
}

Answer №1

When working with HTML, adjusting the background-position of a div is simple:

.close:hover > .closebutt {
  background-position: 0px -14px;
}

This ensures that the background-position changes only when its parent is hovered over.


Here is the original answer I provided prior to your question update: I typically structure my HTML like this:

<a href="#" class="button">
    <div class="glyph"></div>
    <div class="text">Button text</div>
</a>

EDIT: as @Paul D. Waite mentioned in the comments, this HTML format is invalid in HTML4 since an a can only contain inline elements. To address this issue, we can adjust the structure by using span elements as children of the a. The CSS code remains similar, but we may need to add display: block if necessary.

<a href="#" class="button">
    <span class="glyph"></span>
    <span class="text">Button text</span>
</a>

and modify your CSS accordingly:

.button {
  /* .. general style */
}

  .button > .glyph {
    /* .. general glyph style such as size or display: block */
    background-image: url('..');
    background-repeat: no-repeat;
    background-position: left top;
  }

  .button > .text {
    /* .. general text style like font-size or display: block */
    text-decoration: none;
  }

  .button:hover > .glyph {
    /* .. adjust glyph style upon button hover */
    background-position: left bottom;
  }

  .button:hover > .text {
    /* .. change text style on button hover */
    text-decoration: underline;
  }

This approach allows for easy styling modifications by adding a new class to the button:

<a href="#" class="button red">
    <div class="glyph"></div>
    <div class="text">Button text</div>
</a>
<a href="#" class="button gray">
    <div class="glyph"></div>
    <div class="text">Button text</div>
</a>

The corresponding CSS adjustments would be:

.button.red {
  background-color: red;
}

  .button.red > .text {
    color: black;
  }

.button.gray {
  background-color: darkgray;
}

  .button.gray > .text {
    color: white;
  }

Answer №2

Merge them together within a single element and apply the :hover effect to that element:

.container:hover > .content { your hover style}
.container:hover > .image { your hover style}

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

Updating a text input's placeholder using Bootstrap4

Bootstrap 4 provides classes like text-left and text-right for aligning text inside an input field. Is there a Bootstrap-friendly method to only align the placeholder text in an input without affecting the alignment of the actual text? ...

Changing the size of an input field in Bootstrap 4 by utilizing the input-group-append class

Currently, I am working on setting up a pay rate feature that includes two input boxes for numbers. However, I am facing an issue where the input boxes are stretching too much, causing the cents field to drop down to the next line. The Maxlength property ...

The navigation element in Foundation 4 is not showing up properly on an iPhone

After adding a <nav class="top-bar"> element to my webpage, I noticed it displayed correctly on desktop computers but appeared as a solid black bar with no menu items when viewed on an iPhone. This issue seems to be specific to the top section of m ...

Exploring JSON data with Angular

I am struggling with searching JSON data using Angular. After following various tutorials online, I am now facing an issue where the JavaScript debugger in Chrome shows that the script is running but nothing is being displayed on the page. As a beginner, ...

The issue of elements flickering while being hidden and shown with jQuery has been observed in both Chrome and

Having trouble with a simple animation using jQuery? While it may work smoothly in Firefox, you might be experiencing flickering in Chrome and Edge. Check out this jsfiddle for reference: HTML <div id="boxes-wrapper"> <div class="box"></ ...

Changing the font-family on the dropdown menu provided by Codrops seems to be difficult

Looking for help with the Tympanus Codrops Dropdown Menu - SimpleDropDownEffects. I'm having trouble changing the font-family on my website. I am using Icomoon for the icons, and while that works fine, I can't seem to properly format the text ho ...

Tips for sending parameters to XSLT using a Javascript function

Despite my efforts to find a solution in various online posts, I haven't been able to resolve the issue. The challenge lies in my HTML file that includes a JavaScript function for parsing XML and rendering XSLT. I have multiple 'records' in ...

Position Bootstrap Navbar in Location Dash

I am trying to center the links in my navbar, but currently they are aligned to the right using the 'ml-auto' classname. I have been unsuccessful in moving them to the center. Can anyone provide assistance? Below is the code snippet: nav_item = ...

What is causing the issue of my oversized images not showing up on GoDaddy?

I recently uploaded my website on GoDaddy and I am facing an issue where some of my small images (sizes: 872 × 546px) are displaying perfectly fine, but the large ones (banners with a size of 2700 × 900px) aren't showing up. Does anyone have ...

Issue with the responsive design in the paper-drawer-panel component not functioning as expected

I recently launched a new polymer website, but I'm encountering an issue with the responsive design when viewing it on my mobile phone. It's supposed to be using a paper-drawer-panel, so the responsive design should work seamlessly. Check it out ...

Trouble with highlighting the chosen menu item

I have been attempting to implement this code snippet from JSFiddle onto my website. Although I directly copied the HTML, used the CSS inline, and placed the Javascript in an external file, the functionality does not seem to be working properly. Feel free ...

Create a container with three rows (divs) each taking up 33.333333% of the container's height, designed to be responsive to changes in

Seeking a method to organize 3 divs that will collectively fill the entire height of the container. Sample code: <div class="container"> <div class="item-1"></div> <div class="item-2"></div> <div class="item-3">& ...

Ways to customize the size of a radio button with CSS

Is it possible to adjust the size of a radio button using CSS? ...

`At a loss: jQuery failing to retrieve JSON data`

I'm having trouble with a basic script that is supposed to fetch data from a JSON feed and display it in an alert. Despite having loaded jQuery correctly and checked for common issues, the code doesn't seem to be working. I am using jQuery and ca ...

Convert unprocessed JSON information into an HTML table within a ReactJS application

Seeking assistance on how to incorporate a table similar to the image below into my react app https://i.sstatic.net/sDdXt.png Here is the JSON data that I have converted and would like to display similarly. https://i.sstatic.net/l8STe.png I am facing cha ...

Send the data to be placed in the div to launch the window

I'm attempting to open a new window within the same tab and transfer some html code to be inserted into a specific div section of the newly opened window. However, I'm encountering difficulties in passing the data as illustrated below: JavaScrip ...

Issue encountered after updating to version 2.0 of Datatables.net: Conflict between custom CSS and BS5

Having utilized Datatables.net v1.13.11 in conjunction with custom BS5 templates from Themesbrand (), I recently upgraded to v2.0 and came across the following observations: A border box appeared when hovering over a column header. Extra top and bottom bo ...

Uh-oh! An unexpected type error occurred. It seems that the property 'paginator' cannot be set

I am developing a responsive table using Angular Material. To guide me, I found this helpful example here. Here is the progress I have made so far: HTML <mat-form-field> <input matInput (keyup)="applyFilter($event.target.value)" placeholder ...

Ways to enable users to showcase the files they have uploaded

I've successfully implemented code for file uploading. When saving the uploaded file in the database, I retrieve the uploader's username from another database. Additionally, I have a page that displays all uploaded files, functioning perfectly. N ...

What could be causing the color to not appear in the textarea depending on the time condition?

I am attempting to dynamically change the color of my calendar blocks based on the current time. I have created variables for various times in military format and compared them with the current time using a conditional statement. For instance, I have spec ...