The hover effect is being disrupted by the links inside the list item

I'm working on styling an unordered list where each list element contains a frame, background, and an image. I've set it up so that when you hover over a list element, the background changes to orange and the text turns white. However, I ran into an issue when I added links to external pages around the images within the li elements - the text no longer changes to white when hovering, even though the background still turns orange. Removing the <a href ""></a> parts fixes the text color but I need the links.

HTML

<ol id="selectable">
    <li class="ui-state-list">
        <b>Title</b>
        Text here should change to white when hovering over entire li element.
        </br>
        <h5 class="discProd">More text</h5>
        <a href="www.youtube.com">
            <img class="iconAlign" src="images/icons/videoIcon.png">
        </a>
    </li>
</ol>

CSS

h6 { 
    font-size: 12px; 
    color: #2D2D2D; 
    font-weight: 100; 
    line-height: 1.5em; 
}

.discProd { 
    font-size: 10px; 
    color: red; 
} 

#selectable { 
    list-style-type: none; 
    width: 900px; 
}

#selectable li:hover {
    background: orange; 
    color: white; 
}

.ui-state-list {
    border: 1px solid #d3d3d3;
    background: #e6e6e6 url(../images/Products/productBG5.png) 50% 50% repeat-x;
    /*searched bg gloss wave */
    font-weight: normal;
    color: #555555; 
}

.iconAlign {
    float: right;
    margin-left: 34px;
    margin-right: 8px;
    margin-top: -.2em;
}

Answer №1

To achieve this effect with your CSS styling

#selectable li:hover span {
    background: orange; 
    color: white; 
}

Make sure to include an additional span for the text that will change as intended ;)

<ol id="selectable">
      <li class="ui-state-list"><b>Title</b>
      <span>Text here should change to white when hovering over entire li element. </span>
      </br>
      <h5 class="discProd">More text</h5>
      <a href="www.youtube.com">
      <img class="iconAlign" src="images/icons/videoIcon.png">
      </a>
      </li>
</ol>

Answer №2

Initially, the code you provided does achieve the desired outcome.

However, a complication arises.

The issue lies in converting the <img> element into a block by floating it (the default is inline for images). Placing a block element within an inline element (such as the <a>) can lead to rendering issues beyond the bounds of the parent elements, as evident in this fiddle.

To remedy this, eliminate the float: right on .iconAlign and instead, transform the <a> element into a block and float it. Consider rearranging your HTML structure so that the <a><img></a> setup comes at the start of your <li> (assuming placement on the top right corner).

Furthermore, note that you are using an ORDERED list with <ol>, whereas UNORDERED lists should employ <ul>. The correct tag for a line break is <br /> in XHTML, not </br>. In modern browsers, both result in the same effect. Notably, the use of </br> is unnecessary since heading elements (

<h1>, <h2>...<h6>
) inherently function as block elements with existing line breaks before and after.

Explore a demonstration here

...showcasing these adjustments. I exemplify icon positioning utilizing an icon from my server.

PS - The same principle applies to <li>; it functions as a block element only upon applying display: block or float: left/right. Additional details about your objective would be beneficial for providing targeted guidance, like opting for a position: relative/absolute approach with offsets rather than using float altogether.

PPS - Within my demonstration, Firefox displays my icon slightly smaller compared to Chrome, underscoring differing rendering methods for a .ico file.

HTML:

<ul id="selectable">
    <li class="ui-state-list"> 
        <a href="www.youtube.com">
            <img class="iconAlign" src="http://1054.fleeceitout.com/sites/all/themes/jack/favicon.ico" />
        </a>  
        <b>Title</b>
Text here should change to white when hovering over entire li element.
         <h5 class="discProd">More text</h5>
    </li>
</ul>

CSS:

h5 {
    font-size: 12px;
    color: #2D2D2D;
    font-weight: 100;
    line-height: 1.5em;
}
.discProd {
    font-size: 10px;
    color: red;
}
#selectable {
    list-style-type: none;
    width: 900px;
}
#selectable li:hover {
    background: orange;
    color: white;
}
.ui-state-list {
    border: 1px solid #d3d3d3;
    background: #e6e6e6 url(../images/Products/productBG5.png) 50% 50% repeat-x;
    /*searched bg gloss wave */
    font-weight: normal;
    color: #555555;
}
a {
    float: right;
    margin-left: 34px;
    margin-right: 8px;
    margin-top: -.2em;
}
.iconAlign {
    /* no positioning done here since it's an inline element.  position using the block element it's inside of - the <a> */
}

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

Trigger the animation with a button click by utilizing ng-class in Angular.js

How can I make an animation run more than once when a button is clicked? I've tried using ng-class but it's not working as expected. Any help would be appreciated. Thank you! <div ng-controller="MyCtrl"> <div class='contendor_port ...

Establishing Fixed Positioning

I'm currently working on fixing the position of an image at the top of a mobile view page. .fixed { position: fixed; } This is specifically for use with Ionic. <div class="item item-image polygon"> <img src="http://urbanetradio ...

What are the steps to create a responsive form using CSS?

I have a screenshot below that needs to be recreated using HTML/CSS. This design should be consistent in both desktop and mobile views. https://i.stack.imgur.com/cnfHt.png Currently, I have managed to replicate this in a JSFiddle which looks good on desk ...

Utilize JavaScript/jQuery to check for upcoming deadlines in a SharePoint 2013 list

I need to check if an item is due based on its date. When a user creates an item with a date in the format MM/DD (e.g., 06/15), I want to determine if that date falls within the next 30 days, turning it red, within the next 60 days, turning it orange, or g ...

Storing a photo taken with a camera to a local directory on the computer

Currently, I am utilizing HTML5 inputs to capture a picture from the camera by using the code below: <input id="cameraInput" type="file" accept="image/*;capture=camera"></input> Subsequently, I am obtaining the image in blob format and manip ...

What is the best way to properly encapsulate a variable within single and double quotation marks in PHP?

Can someone help me with this HTML input issue? I'm struggling to include the $coins variable inside single and double quotes. echo '<form action="action.php" method="post"> <input type="number" max=& ...

Choosing images based on aspect ratio in CSS

I am looking to add a specific class to image elements, but only for those that are in landscape format. Is there a way to target images in my HTML with a width/height ratio greater than 1, similar to how media queries work with the "device-pixel-ratio" f ...

Determining the precise margin within a nested div structure

Hopefully, my description of a "nested div" was accurate. My current situation involves an image within a div structured like this: content -> content-inner -> column_left I'm struggling to start the image at 0 px (the extreme left side of th ...

Using jQuery, locate identical text and assign a class to the corresponding container

How can I check if any of the h2 elements match the text in h1, ignoring case sensitivity? If a match is found, I want to add a class to the container Product-div. <div id="PageHeader"> <h1>Same text</h1> </div> <div class= ...

Is it possible to apply a :before or :after pseudo-element to an input field element?

I'm having trouble incorporating the :after CSS pseudo-element on an input field. It seems to be working fine with a span, but not with the input field. <style type="text/css"> .mystyle:after {content:url(smiley.gif);} .mystyle {color:red;} < ...

overlapping an absolutely positioned div above a statically positioned div with unique overflow properties

I have a fixed div with a max-height, an overflow-y: auto and an overflow-x: hidden. <div id="overlay"> <!-- position:fixed; --> <div class="close" >&times;</div> <!-- position:absolute; NOT WORKING --> <div ...

jmpress: Scrolling through Absolutely Positioned Element (Slide) with dynamic height

Check out the Live Site I'm currently working on a website using jmpress, but I'm facing difficulties in implementing scrolling functionality for my content. While I can scroll in Chrome by selecting text and dragging down, I want to achieve thi ...

I am having issues with the accuracy of my JavaScript number validation function

function CheckIfNumeric() { var quantity = jQuery("#txtShippedQuantity").val(); quantity = quantity.toString(); for (i = 0; i < quantity.length; i++) { var character = quantity.charAt(i); if (isNaN(character)) { ...

What is the best way to place a background image at the top left and bottom right corners of an HTML element?

I have a paragraph and I would like to visually mark the beginning and end of the paragraph for the user. I have successfully displayed a background image in the upper left corner, but I am unsure how to position the background image in the lower right co ...

What is the best way to define the active <li> tab using JQuery?

Initially, my HTML code includes a set of <li> elements that I want to dynamically add to the existing <ul>. <ul class="tabs" id="modal_addquestion_ul"> <li class="tab-link current" data-tab="multipleChoice">Multiple Choice< ...

Is there a way to extract all the content from a webpage's body using BeautifulSoup?

Looking to extract text from a medical document webpage for a Natural Language Processing project using BeautifulSoup, but encountering challenges. The specific website in question is located here: The goal is to capture the entire text body by utilizing ...

Covering a secret link with a backdrop image

I have set a background image in a column on my WordPress site. However, I want to make this image clickable by placing a transparent box over it, as background images cannot be linked directly. #container { background-image: url(https://www.quanser.c ...

Unable to get click function to work with multiple div elements

Using four div elements, I have implemented a feature where clicking on the first div causes all other div elements to change opacity. This is working correctly. However, I would like the opacity of the first div to remain unchanged when moving to another ...

Which is the better option: replacing content with Ajax loading or appending content?

Currently, I am working on developing a predominantly ajax-driven website where content is fetched based on hashTag changes. However, I am facing a dilemma between two options: Option 1 In this option, the content is replaced with each request. Option 2 ...

How can I use TailwindCSS in NextJS to remove the "gap" className from a component?

Currently, I am working on button components in NextJS with Tailwindcss and I am encountering a problem with some variants. Everything works fine, except when I remove the children (button text), I notice something strange that I can't quite figure ou ...