"Enhancing User Experience with Hover Effects in HTML using .common

I'm pretty new to coding and currently working on a Github repository. On the same page, I have 12 clickBoxes that each have a hover effect applied where a PNG appears upon hovering. However, I've run into an issue where either the hover effect works but the clickBox doesn't, or vice versa. I believe the problem lies within the HTML code.

The specific clickBox I am focusing on is .clickBox-Ministries. Here is the CSS for the hover effect:

.clickBox-Ministries,
.png-overlay {
    position: absolute;
    width: 6%;
    height: 14%;
    top: 12%;
    left: 11%;
}
.png-overlay{
    /*display: none;*/
    position: absolute;
    width: 12%;
    height: 13%;
    top: 12.5%;
    left: 8%;
    z-index: 1;
    opacity: 0;
}
.common-parent{
    position: relative;
}
img.png-overlay:hover{
    display: inline;
    opacity: 1;
    /*transition: opacity 0.1s ease-in-out;*/
    cursor: pointer;
}

Setting up the HTML in this manner causes both the clickBox and hover effect to not work correctly:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel = "stylesheet" type = "text/css" href = "style.css"/>
</head>
<body>
    <!-- Main Nav -->
        <div class=clickContainer>
            <div class="common-parent">
                <div class="clickBox-Ministries">

                </div>
                <img class="png-overlay" src="Ministries_Overlay.png">
            </div>

If I remove the closing tag from the .common-parent, the PNG hover effect works but the clickBox does not respond.

Conversely, removing the closing tag from .clickBox-Ministries allows the clickBox to function properly, but the PNG hover effect no longer works:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel = "stylesheet" type = "text/css" href = "style.css"/>
</head>
<body>
    <!-- Main Nav -->
        <div class=clickContainer>
            <div class="common-parent">
                <div class="clickBox-Ministries">
                <img class="png-overlay" src="Ministries_Overlay.png">
            </div>

Any insights on how to resolve this dilemma?

Answer №1

Essentially, the issue with your clickBox-Ministries div is that it lacks height due to having no content - unless you forget to close the div tag, in which case it treats the image within as its content and adopts the image's natural height.

By default, div elements function as block level elements. For more information on how they determine their default width and height, refer to https://developer.mozilla.org/en-US/docs/Web/HTML/Block-level_elements.

To understand what's happening, inspect the elements using your browser's developer tools. In this scenario, you'll notice that the div takes on the viewport's width (potentially subtracting a bit for default margin) and has a height of 0 unless it contains the img element.

In addition, for a smooth fade-in/fade-out effect on the image, apply the transition to the img class rather than just the hover state.

The provided snippet incorporates these adjustments. It may seem unusual because without any inherent height in the clickable div, only the img triggers an action (on hover). To make the div above the img clickable, it must have some content or explicit height and width settings.

When running this snippet, you will initially see a blank page and need to move the mouse around near the top left corner to reveal the img.

body {
  width: 100vw;
  height: 100vh;
}
.clickContainer {
  width: 100%;
  height: 100%;
}
.clickBox-Ministries,
.png-overlay {
    position: absolute;
    width: 6%;
    height: 14%;
    top: 12%;
    left: 11%;
}
.png-overlay{
    /*display: none;*/
    position: absolute;
    width: 12%;
    height: 13%;
    top: 12.5%;
    left: 8%;
    z-index: 1;
    opacity: 0;
    transition: opacity 0.1s ease-in-out;
}
.common-parent{
    position: relative;
    width: 100%;
    height: 100%;
}
img.png-overlay:hover{
    display: inline;
    opacity: 1;
    cursor: pointer;
}
    <!-- Main Nav -->
        <div class=clickContainer>
            <div class="common-parent">
                <div class="clickBox-Ministries">
                </div>
                <img class="png-overlay" src="https://picsum.photos/id/1015/200/300">
            </div>
         </div>

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

html elements correspond directly to individual json objects

Looking for a precise way in javascript/jquery to connect objects created in an external JSON file to populate their respective HTML divs. The code snippet and JSON data are provided below, along with an image for reference that shows the layout of the div ...

Interactive Radial Menu Using Raphael JS

Greetings and thank you for taking the time to consider my predicament. I am currently working on creating an SVG menu using raphael, but unfortunately, geometry has never been my strong suit. Below is a visual representation of what I have managed to cre ...

Press a button to navigate through a series of web pages

Currently, I am working on a website focused on profiles. The challenge I am facing is implementing fixed side buttons (SKIP and ADD) that will cycle through the list of profiles. I understand that I may need to create an array to store all the profiles, ...

When attempting to change an image using JavaScript, the image fails to load

I am having trouble understanding what is wrong with my code. <html> <body> <script type="text/javascript"> function changeImage() { var image = document.getElementById('myImage'); if (image.src.match("bulbon")) { ...

Link-defying button

I'm developing a website with a homepage that serves as an entry point to the rest of the site (it welcomes the user and allows them to click anywhere to access the main website). The following code accomplishes this: <template> <div onc ...

Unexpected changes in styles can occur on the page at any moment

I have loaded this CSS: #ue_email_new{ border: 1px solid #000000; } When the page loads, it displays correctly. However, when I click on inspect element (using FF 17) and view the CSS, I notice something strange in the debugger: element { border ...

What is the best method for exporting HTML tables to multiple excel worksheets using PHP?

I am working with PHP and I need to export two HTML tables into an Excel file with two separate sheets, each containing one table. I have followed the instructions in the documentation, but it only generates one sheet with one table on it. <?php use ...

div container overflowing with numerous unordered list elements

I am having an issue with a div and ul in my HTML code. The height of the div is smaller than the combined height of all the li elements within it, so I have set overflow to auto in the CSS for the div. Here is an example of the HTML code: <div class= ...

What is the best way to stop HTML from treating user input text as an entity?

My website allows users to input text. One user entered the text "I worked on the #3&#4 valves" into an <input>. This text is stored in a database and displayed elsewhere on the screen. The issue I'm facing is that the "&#4" is being int ...

Tips on how to ensure the navigation menu is the same size as its child elements in the navigation menu

I'm currently designing a navigation menu that includes a child navigation menu. My goal is to have the size of the main navigation menu match that of the child navigation menu. To achieve this, I've created three hyperlink tags followed by a di ...

How can I align those 3 divs in the center of the body?

Can someone help me with centering those 3 divs in the body? I have tried creating 2 versions of the row, one with an extra outer wrap around the column and one without. Unfortunately, since this HTML was created with a page builder (WP visual composer), I ...

How to align text to the right with ellipsis and button in a ReactJS component

In a specific scenario, I need to display a button on the right corner of the header. The selected filter value should appear next to the filter button like this: | Between 01/23/2018 and 07/30/2018 {button}| As the user changes the screen size, I want to ...

Variety of perspectives on Magento products

Is there a way to configure Magento 1.7 to display view.phtml for bundled products and view.phtml for simple products, or the other way around? I am looking to customize the views for different product types - what is the best approach to achieve this? ...

Displaying a document file on a webpage hosted by a raspberry pi

My Raspberry Pi hosts a local website that does not require an internet connection. I want the webpage to display text from a large file line by line as users access it. For example, if the text file contains: Hello How are You doing Today? The brow ...

Tips for creating improved CSS ID rules

Currently, I am in the process of learning HTML and CSS and have a question regarding writing rules for multiple IDs. Is it possible to write rules that apply to several IDs in order to achieve my desired outcome? <!-- language: lang-css--> #menu- ...

Centering an Image of Unspecified Dimensions Both Vertically and Horizontally with Hover Effect on Anchor

To achieve both horizontal and vertical centering of an image with unknown dimensions, I need to use max-width and max-height to constrain it within a container size of 101x84. Additionally, I want the image to display an icon in the middle when hovered ov ...

Issue with nested grid layouts within the 960 grid system

I recently started working on a personal hobby website and decided to utilize the 960 css grid system for organizing my HTML elements on the page. After grasping the basic concept, I went ahead and created a preliminary version of the website, which is ho ...

Creating a unique paragraph style for my blog in SharePoint 2013 - step by step guide!

Managing a Share Point 2013 web site with a blog presents the challenge of incorporating special text content in posts. To ensure consistency, it's essential to add additional styles that can be applied to all posts. Normally, these styles are accesse ...

Enhance your product listings in Opencart 2.0 by adding supplementary images alongside the main product image

Can someone assist me with moving additional product images next to the main product image in the default opencart 2.0.1.1 theme? Currently, they are appearing below the product image and I would like them to be displayed alongside it instead. ...

Trouble with FilterBy Feature in Bootstrap-Table

I am attempting to use filtering functionality with a table that is populated via JSON, utilizing bootstrap-table by wenzhixin. HTML snippet: <button class="btn btn-primary" id="filterBtn">Filter</button> <div class="container"> &l ...