Ensure that all elements with the :hover event have a text color of #fff in CSS


 Troubleshooting problems with block's child elements during the :hover event. Specifically, I have a pricing block where I want all texts to be of color #fff when hovered over.

Currently, when hovering over the <h3> and <p> elements individually, they change color as expected. However, I need them to change color even when just hovering over the parent block. How can I achieve this effect without using JavaScript? Is it possible or did I overlook something?

This is the HTML code:

<section>
    <div id="Pricing">
        <div class="container">
            <h1>Pricing Plans</h1>
            <div class="price-group">
                <div class="price-block">
                    <h3>Students</h3>
                    <span class="price-main">$ 8</span>
                    <span class="price-additional">Per Month</span>
                    <p class="footnote">Personal License</p>
                    <button class="btn-price">Purchase</button>
                </div>
           </div>
       </div>
   </div>
</section>

And here is the CSS code:

.price-block {
    width: 270px;
    min-height: 450px;
    margin-left: 20px;
    text-align: center;
    background-color: #f4f4f4;
    transition: .4s ease-in-out;
}
.price-block:hover {
    background-color: #000;
    color: #fff;
    transform: scale(1.1);
}

.price-block h3 {
    margin-top: 40px;
    margin-bottom: 50px;
    font-size: 20px;
    color: #333;
    letter-spacing: 1px;
    font-weight: 700;
    text-transform: uppercase;
}

Here are some screenshots: https://i.sstatic.net/mcBYX.png https://i.sstatic.net/bF7Fc.png

Thank you for your attention!

Answer №1

To ensure consistent styling across all child elements, you need to incorporate a cascading style. Using * will apply the style to all descendants (and their descendants). In order to prevent styling the text within the button element, you can utilize :not(button), which will maintain the black color of the button text as depicted in your image.

Below is the styling directive that should be included:

.price-block:hover *:not(button) {
  color: #fff;
}

Here is a demonstration:

.price-block {
    width: 270px;
    min-height: 450px;
    margin-left: 20px;
    text-align: center;
    background-color: #f4f4f4;
    transition: .4s ease-in-out;
}

.price-block:hover {
    background-color: #000;
    color: #fff;
    transform: scale(1.1);
}

.price-block:hover *:not(button) {
  color: #fff;
}

.price-block h3 {
    margin-top: 40px;
    margin-bottom: 50px;
    font-size: 20px;
    color: #333;
    letter-spacing: 1px;
    font-weight: 700;
    text-transform: uppercase;
}
<section>
    <div id="Pricing">
        <div class="container">
            <h1>Pricing Plans</h1>
            <div class="price-group">
                <div class="price-block">
                    <h3>Students</h3>
                    <span class="price-main">$ 8</span>
                    <span class="price-additional">Per Month</span>
                    <p class="footnote">Personal License</p>
                    <button class="btn-price">Purchase</button>
                </div>
           </div>
       </div>
   </div>
</section>

Answer №2

To customize the appearance, you can modify the CSS style below.

.price-box {
    width: 300px;
    min-height: 500px;
    margin-left: 30px;
    text-align: center;
    background-color: #eaeaea;
    transition: .3s ease-in-out;
}
.price-box:hover *:not('.btn-price') {
    color: #000;
}

.price-box:hover {
    background-color: #333;
    color: #fff;
    transform: scale(1.2);
}

.price-box h4 {
    margin-top: 30px;
    margin-bottom: 40px;
    font-size: 24px;
    color: #444;
    letter-spacing: 2px;
    font-weight: 600;
    text-transform: uppercase;
}

Answer №3

If you want to apply styles to all child elements, you can use the * selector.

.price-block:hover *{
    background-color: #000;
    color: #fff;
    transform: scale(1.1);
}

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

having each individual div function on its own accord

Is there a more efficient way to make HTML and CSS function independently without duplicating the code multiple times and changing IDs or class names? Currently, only the top male and female button works when clicked. I am looking for a CSS-only solution t ...

Guide: Creating a delete button that removes a designated post

I am encountering an issue with the delete button and dialog in my application. The button does not delete the correct post; instead, it always starts deleting from the first post that results in deleting all posts. Furthermore, the template does not displ ...

Bringing data from HTML into Matlab via a table

I need assistance with importing data from an HTML webpage into Matlab. I've tried using the "getTableFromWeb" function, but it doesn't seem to be working as expected. When I view the data in a browser like Firefox, everything appears fine. Howev ...

The CSS property for restricting white-space to nowrap is not functioning as

Having a div with multiple child divs floating left can be tricky. To prevent them from breaking, setting them to display:inline-block and white-space:nowrap seems like the solution. However, in some cases this may not work as expected. If your goal is to ...

Loop a background image around a header that is centered in the design

I'm having an issue with creating a header with fixed dimensions and aligning it center on my webpage. I want to have small 1 px images repeat along the remaining width of the header from each edge of the main image. However, when I try to repeat the ...

Error encountered when attempting to save a video using PHP due to an undefined index

Seeking assistance with my php code dilemma. I am attempting to insert data into mysql using a form that includes a video upload option which stores the video locally and the video URL in the database. However, when testing the form, I encounter multiple e ...

Stylish Dropdown Menu with Regular Buttons

Currently, all buttons have a hovering effect, but I only want one button to behave this way. I need two buttons to immediately redirect to another page upon clicking, without any hover effect. The CSS styling code has been provided below: #wrapper { ...

Django static files reference

sample code snippet tooltip reference navigating to the correct file path FILES: settings.py STATICFILES_DIRS = [ BASE_DIR / "static" , ] base.html {% load static %} How can I properly link my static files in the html line below?... &l ...

Having Trouble with Font Awesome Icon Loading in Search Box

Currently, I am working on a website located at . However, I am facing an issue where the font awesome icon is not loading properly and is only displaying a square. I have thoroughly reviewed the CSS and cannot identify any conflicting elements. Any assis ...

Is there a method in bootstrap that reveals the elements with the hidden class?

Currently, I am loading data into a bootstrap table on my JSP. The table class is hidden at the moment. After successfully uploading the data into the bootstrap table, I have implemented the following code: $(function() { var table = $('#Table') ...

What are some ways I can efficiently load large background images on my website, either through lazy loading or pre

Just dipping my toes into the world of javascript. I'm currently tackling the challenge of lazy loading some large background images on my website. My goal is to have a "loading" gif displayed while the image is being loaded, similar to how it works ...

What is the best way to swap out a div element with a text area when I press a button, all

I recently used a Fiddle found at http://jsfiddle.net/GeJkU/ function divClicked() { var divHtml = $(this).html(); var editableText = $("<textarea />"); editableText.val(divHtml); $(this).replaceWith(editableText) ...

Troubleshooting z-index problem with background image and parent tag of image

I seem to be facing an issue with the z-index property. Here is the code snippet in question: <div id="sidebarCont"> <div id="avatarCont" class="mask"> <img id="" class="" src="img.jpg" alt=""/> </div> And here is the correspo ...

Tips on containing text within a div box without exceeding its boundaries

I've been struggling with containing the text in my display area. No matter what I try, the text keeps overflowing and it's driving me crazy. I've researched various solutions like using word-wrap: break-word;, adjusting width manually, but ...

The use of absolute positioning in conjunction with overflow:hidden

<div id="container" style="overflow:hidden; position:relative;"> <div id="content" style="position:absolute;"> </div> </div> Is it possible to display the content element which is larger than its parent container, while still k ...

Incorporating RFID reader functionality into a website

I am currently facing an issue with integrating an RFID card reader into a web page. After some research, it seems that the solution involves creating an ActiveX component and using JavaScript. My question is, how can we go about building an ActiveX compo ...

When you press the submit button, the screen automatically scrolls down but no action is taken

I'm currently utilizing Selenium WebDriver (Java) with the Firefox driver. Upon trying to click the 'Submit' button on a particular page, it only results in scrolling down the screen slightly. The button itself does not register the click, c ...

Tips on implementing vertical-align within an inline-table

Can someone help me with vertically centering text inside links without losing the 100% height of "a" elements? I've tried to tackle this issue without success, and I am using Twitter Bootstrap 3. Any suggestions would be greatly appreciated. ...

React state update not triggering a component re-render

I've been attempting to toggle the visibility of a div by clicking on another div, but I'm encountering an issue. The div becomes invisible on the first click only if it was visible initially. After that, it remains invisible and does not update. ...

Display the dropdown selection

I am trying to customize my dropdown menu so that when I hover over the main menu item (Sample Page), only the About submenu is displayed, not all of the dropdown menus. li.menu-item.dropdown .dropdown-menu { position: absolute; top: 70px; visibil ...