Hover effect not activating on image within modal

only img:hover is working, but after adding this to the CSS:

#user-profile #user-myModal .modal-body img:hover #user-profile #user-myModal .modal-body .change-pic i{
    display: block;
}

it's not working. I changed the class and id names, tried everything,

but it still won't work.

What I want is for the user to hover over an image and have this class/id display: block. Also, can I use multiple classes/ids on hover or should I stick to only one?

CSS :

#user-profile #user-myModal .modal-body img{
    border: 1px solid gray;
    height: 160px;
    width: 87%;
    margin-left: 9%;
    margin-top: 8%;
    position: relative;
}
#user-profile #user-myModal .modal-body .change-pic i {
    position: absolute;
    top: 43%;
    left: 39%;
    color: black;
    z-index: 999;
    height: 50px;
    width: 50px;
    background: white;
    border: 1px solid;
    border-radius: 50px;
    padding: 7% 0% 0% 8.4%;
    display: none;
}
#user-profile #user-myModal .modal-body img:hover #user-profile #user-myModal .modal-body .change-pic i{
    display: block;
}

HTML :

<section id="user-profile">
  <div id="user-myModal" class="modal fade" role="dialog">
    <div class="modal-dialog">
            <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <ul class="nav nav-tabs list-inline">
            <li role="presentation" class="active"><a id="profile-tab" href="#">Your Profile</a></li>
            <li role="presentation"><a id="edit-profile-tab" href="#edit-profile">Edit Profile</a></li>
          </ul>
        </div>
        <div class="modal-body">
          <div class="row" id="user-profile-tab">
            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12">
              <img src="img/jagdish.jpg" class="img-responsive">
              <div class="change-pic">
                <i class="fas fa-upload"></i>
              </div>
            </div>
            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-12 details">
                <h4>Mr. jagdish Panday</h4>
              <h6><strong>Occupation :</strong> Web Developer</h6>
              <h6><strong>Address :</strong> Kausaltar, Madhyapur Thimi, Nepal</h6>
              <h6><strong>Phone :</strong> 9863696718</h6>
              <h6><strong>Email :</strong> <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3c4c5d52585d4556557c5b5550125f5351">[email protected]</a></h6>
              <ul>
                <li class="facebook"><i class="fab fa-facebook"></i></li>
                <li class="twitter"><i class="fab fa-twitter-square"></i></li>
                <li class="instagram"><i class="fab fa-instagram"></i></li>
                <li class="google"><i class="fab fa-google-plus-square"></i></li>
              </ul>
            </div>
          </div>

          <div class="edit-profile text-center" id="edit-profile" style="display:none;">
            <form>
                            <input type="text" placeholder="Name"><br>
                            <input type="text" placeholder="Occupation"><br>
                            <input type="text" placeholder="Address"><br>
                            <input type="text" placeholder="Phone Number"><br>
                            <input type="text" placeholder="Email"><br>
                            <a class="btn btn-default">Save Changes</a>
            </form>
          </div>
        </div>
        </div>
    </div>
  </div>
</section>

Answer №1

why using

#user-profile #user-myModal .modal-body img:hover #user-profile #user-myModal .modal-body .change-pic i{
    display: block;
}

If you want to show your .change-pic i upon hovering your image, utilize the "+" selector to select the next sibling element after your image in the hierarchy. This way, you can target your icon correctly like so:

#user-profile #user-myModal .modal-body img:hover + .change-pic i {
    display: block;
}

Here's an example Snippet demonstrating this behavior:

#user-profile #user-myModal .modal-body img{
    border: 1px solid gray;
    height: 160px;
    width: 87%;
    margin-left: 9%;
    margin-top: 8%;
    position: relative;
}
#user-profile #user-myModal .modal-body .change-pic i {
    position: absolute;
    top: 43%;
    left: 39%;
    color: black;
    z-index: 999;
    height: 50px;
    width: 50px;
    background: white;
    border: 1px solid;
    border-radius: 50px;
    padding: 7% 0% 0% 8.4%;
    display: none;
}
#user-profile #user-myModal .modal-body img:hover + .change-pic i{
    display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<button type="button" data-toggle="modal" data-target="#user-myModal">Open Modal</button>
<section id="user-profile">
    <div id="user-myModal" class="modal fade" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <!--<h4 class="modal-title">Your Profile</h4>-->

                        <ul class="nav nav-tabs list-inline">
                            <li role="presentation" class="active"><a id="profile-tab" href="#">Your Profile</a></li>
                            <li role="presentation"><a id="edit-profile-tab" href="#edit-profile">Edit Profile</a></li>
                        </ul>
                </div>
                <div class="modal-body">
                    <div class="row" id="user-profile-tab">
                        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12">
                            <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/2d/Jagdish.jpg/450px-Jagdish.jpg" class="img-responsive">
                            <div class="change-pic">
                                <i class="fa fa-upload"></i>
                            </div>
                            <!--<a id="btn-edit-profile" class="btn btn-default">Edit Profile</a>-->
                        </div>
                        <div class="col-lg-8 col-md-8 col-sm-8 col-xs-12 details">
                            <h4>Mr. jagdish Panday</h4>
                            <h6><strong>Occupation :</strong> Web Developer</h6>
                            <h6><strong>Address :</strong> Kausaltar, Madhyapur Thimi, Nepal</h6>
                            <h6><strong>Phone :</strong> 9863696718</h6>
                            <h6><strong>Email :</strong> <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0c0d1ded4d1c9dad9f0d7ddd1d9dc9ed3dfdd">[email protected]</a></h6>
                            <ul>
                                <li class="facebook"><i class="fab fa-facebook"></i></li>
                                <li class="twitter"><i class="fab fa-twitter-square"></i></li>
                                <li class="instagram"><i class="fab fa-instagram"></i></li>
                                <li class="google"><i class="fab fa-google-plus-square"></i></li>
                            </ul>
                        </div>
                    </div>

                    <div class="edit-profile text-center" id="edit-profile" style="display:none;">
                        <form>
                             <input type="text" placeholder="Name"><br>
                             <input type="text" placeholder="Occupation"><br>
                             <input type="text" placeholder="Address"><br>
                             <input type="text" placeholder="Phone Number"><br>
                             <input type="text" placeholder="Email"><br>
                             <a class="btn btn-default">Save Changes</a>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

Answer №2

img:hover #user-profile-selector

The code snippet above attempts to target an element within the image tag on hover, which is not a valid CSS selector.

If you want to display a different element when hovering over the image, it's recommended to implement this functionality using JavaScript.

You can check out a demonstration I created on this website

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

How to utilize the reduce method with an array of objects in JavaScript

Every week, a number of objects are delivered to me. Each object contains information such as date, hours, and other fields. I need to arrange these objects in an array based on the total hours for each day. Here is an example of the objects: var anArray ...

Transform CSS into React.js styling techniques

My current setup involves using Elementor as a REST Api, which is providing me with a collection of strings in React that are formatted like this: selector a { width: 189px; line-height: 29px; } Is there a tool or library available that can conver ...

What is the proper way to mention JavaScript packages when including them as parameters in Angular elements within HTML?

I was looking to enhance the command to be more authoritative. <div (click)="lastCall(999)">click me</div> My attempt to utilize Number.MAX_SAFE_INTEGER resulted in an error from my computer stating that it wasn't recognized. As a result ...

Tips on fetching the ID of the selected option using JavaScript without relying on jQuery

Looking to print the selected option ID using pure Javascript (not JQuery) for multiple select tags. If we have more than one select tag, how do we achieve this? <select onchange="showOptions(this)" id="my_select1"> <option value="a1" id="ida ...

Is there a way to execute a JavaScript function by clicking on an anchor tag?

Many websites, such as StackOverflow, have attempted to address this particular issue. However, the explanations provided are complex and difficult for me to grasp. I am looking for someone who can help identify my mistakes and explain the solution in simp ...

Dealing with content extending into the previous column within CSS columns

I used this code pen example to perfectly illustrate the concept. To demonstrate, I applied the following CSS code. Try hovering over the second card in the top row. .custom-hover:hover { transform: scale(1.5); } In essence, when the top element of a ...

Disabling buttons in a Fiori UI5 application using HTML: A step-by-step guide

I don't have much experience with scripting languages. I am attempting to hide buttons within a SAP Fiori app by using the "Inspect element" tool in the Mozilla browser. When I delete the HTML code, the button disappears, but I would like to accomplis ...

HTML code featuring multiple dropdown menus, each equipped with its own toggleable textarea

I have multiple HTML drop downs, each triggering a textarea based on the selection. Currently, I'm using show and hide JavaScript functions for each question individually. Is there a way to streamline this so that I don't have to write separate c ...

Enhancing your website's design with dynamic CSS variables using JavaScript

Is there a way to update CSS variables specifically scoped under a certain CSS class (or even other complex CSS selectors) that are predefined in a stylesheet? This question extends beyond just CSS variables and includes other CSS properties as well, quest ...

Expansive dropdown menu stretching across the entire width, yet the content within restricted to a width of

I have recently made some updates to our website's dropdown menu, expanding the results displayed. However, I would like the dropdown contents to fit within the width of our page. I have tried adjusting the CSS with limited success: .menu > ul > ...

It is not possible to place the cursor before a non-contenteditable div within a contenteditable div

Here is the layout I am working with: <div contenteditable=true> /// <div contenteditable=false>not editable</div> /// </div> One problem I'm facing is that when the non-contenteditable div is the first element on a ...

Tips for utilizing CSS flexbox to create a row of squares that can scale while maintaining their aspect ratios

Struggling with this issue has me on the verge of pulling my hair out. It seems like a simple task, yet I can't seem to achieve perfection. My goal is to create a row of squares that maintain their aspect ratio but scale down in size as their containe ...

The code is triggering IE 8 to switch to compatibility mode resembling IE7

I have created a custom javascript function that generates a pop-up, and I am invoking this function in my code. However, every time I click the button, the browser switches to IE 7 compatibility mode and the pop-up appears behind the button. Below is my ...

If a <section> element contains a <header>, must it also include a <footer>?

Here is the current state of my code: <section id="one"> <h2>Section Title</h2> <p>Lorem ipsum...</p> <p>Lorem ipsum...</p> <p>Lorem ipsum...</p> </section> <section id="two"&g ...

Discovering the differences between input values in HTML when using scripts

I have a code snippet here for an HTML project. The code includes an input field for username and password. I am looking to compare the user's input with a specific value using JavaScript. My question is, what code should be included in the button cli ...

What is the process of performing numerical calculations using jQuery?

I need to deduct certain input values from the total price. Here's the code snippet: $('.calculate-resterend').click(function(e) { e.preventDefault(); var contant = $('.checkout-contant').val(); var pin = $('.che ...

What is the process for embedding HTML and CSS content into an Outlook email?

Has anyone experienced trouble adding an HTML and CSS web page to an Outlook email? I attempted to use the "Insert as text" option, but the CSS file is not loading correctly in Outlook. Any suggestions on how to fix this? The error message states that the ...

Web GUI for insert, update, and delete functionalities

After repeatedly creating Add/Edit/Delete/List GUI's, I have grown weary and frustrated with the process. Surely, there must be a free package available that can simplify this tedious task. I envision something like the following code snippet: { ...

Utilizing CSS to Rearrange Columns Depending on Screen Size

Check out the progress on this page: If you scroll to the bottom, you'll find a list of links - displayed in 4 columns in the regular view. When you resize the screen, they shift into a single column. I want them to show as a single column on an iPh ...

Developing a perpetually scrolling container within a webpage

I am attempting to implement a scrollable div on my webpage that can continuously load content. I am currently using the following code snippet for this --> http://jsfiddle.net/cyrus2013/Qq85d/ $(document).ready(function(){ function loadMoreContent() ...