Issue with CSS: Hover effect causing unexpected additional white space

My main goal is to implement a hover effect that covers an entire section, but I'm facing some challenges. When I hover over my products, it doesn't behave as expected and adds extra white space below while not covering the section properly.

Things I have tried:

  • Adjusting the line height
  • Adding a new section/div
  • Setting padding and margin to 0

Here is a snippet of my CSS code:

div#cat{
/*line-height: 30%;
height: 50px;*/
width: 61%;
margin-left: 39%;
height: 15px;
line-height: 50%

}

/*product area rollover/hover*/
div#cat:hover{
background-color: #e86065;
opacity: 0.5;
filter: Alpha(opacity=50);
background-position: 100% 100%;
height: 80%;
}

And here is a part of the PHP code where I loop information out of a database:

 echo("<div id='cat$i' class='cat'>");
echo("<table>");
echo("<tr>");
echo("<td class='full'>");
echo("<a name='foodcat$iii' id='prods_nme'>$prod_name</a>");
echo("<td>");
echo("</td>");
echo("<p id='prods_desc'>$prod_desc</p>");
echo("</td>");
echo("<td class='price'>");
echo("<p id='prods_price'>£$prod_price</p>");
echo("</td>");
echo("<td class='add'>");
echo("<a href='Shopping_cart.php?add_item=$prod_id'>+</a>");
echo("</td>");
echo("</tr>");
echo("</table>");
echo("<br>");
echo("<br>");
echo("<hr id='hr'>");
echo("</div>");
echo("</div>");
echo("</div>");
echo("<br>");
echo("<br>");
$i++;

Answer №1

When your CSS transitions to the hover state, it seems to be adding unnecessary height. To avoid this issue, ensure that you only modify the specific properties you intend to change. For example, in this case, you should not alter the height.

A correct approach would be something like the following:

div#cat{
    width: 61%;
    margin-left: 39%;
    height: 15px;
    line-height: 50%
}

/*product area rollover/hover*/
div#cat:hover{
    background-color: #e86065;
    opacity: 0.5;
    filter: Alpha(opacity=50);
    background-position: 100% 100%;
    width: 61%;
    margin-left: 39%;
    height: 15px;
    line-height: 50%
}

Answer №2

ArtOfCode has provided a suitable solution to your issue.

Nevertheless, using tables for formatting is not recommended. The table padding becomes apparent when borders are applied to the cells, causing various formatting challenges. It would be more beneficial to utilize DIVs instead and become familiar with utilizing float:left and/or flexbox.

table{border-collapse:collapse;}
td{border:1px solid #ccc;padding:0;}

div#cat {
  /*line-height: 30%;
height: 50px;*/
  width: 61%;
  margin-left: 39%;
  height: 15px;
  line-height: 50%
}
/*product area rollover/hover*/

div#cat:hover {
  background-color: #e86065;
  opacity: 0.5;
  filter: Alpha(opacity=50);
  background-position: 100% 100%;
  height: 80%;
}
<div id='cat'>
  <table>
    <tr>
      <td class='full'>
        <a name='foodcat$iii' id='prods_nme'>$prod_name</a>
        <p id='prods_desc'>$prod_desc</p>
      </td>
      <td class='price'>
        <p id='prods_price'>£$prod_price</p>
      </td>
      <td class='add'>
        <a href='Shopping_cart.php?add_item=$prod_id'>+</a>
      </td>
    </tr>
  </table>
</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

Generating a hyperlink to a specific user ID within a data table

I'm facing an issue with the formatting of my simple table when trying to navigate to user.id. Is there a better approach to this or should I consider moving LinkToUser? All styling has been removed to avoid any conflicts. import styled from 'st ...

Position DIVs next to each other

After scouring through countless Stack Overflow threads in search of a solution to my issue, I still can't seem to get it right. Everyone suggests using float: left, float:right, overflow: hidden, display: block, etc., but none of them are working for ...

Unable to display HTML content on a page using the foreach callback in JavaScript

I have a function that iterates through each element of an array and generates HTML content on the page while updating some properties using elements from the array. I am utilizing forEach to iterate over the elements of the array and innerHTML to display ...

Executing the JavaScript function only a single time is what you're looking

I'm having trouble getting this script to work correctly on my website. It is supposed to trigger an alert when the specified id is in the viewport, but the issue I am encountering is that the alert keeps popping up repeatedly while the id is still vi ...

"Troubleshooting Issue: jQuery dataTables Filter Functionality Inoperative When Conjoined with

Incorporating the dataTables plugin and recycling code from another page to create a select for filtering a specific column, I encountered issues with the filtering functionality not working as expected. This was perplexing since it was code that had previ ...

JavaScript date formatting without using jQuery

Looking for help to apply a date mask (dd/mm/yyyy) on an ASP.net textbox. I have tried several JavaScript solutions found through Google search, but none seem to work seamlessly, especially with backspacing. Any suggestions for a reliable script would be ...

An issue with the font display

I'm having trouble with a specific font on my webpage and jsfiddle, even though it works fine on w3schools website. Here's an example on jsfiddle HTML <div> With CSS3, websites can finally use fonts other than the pre-selected "web-safe" ...

Center the image within the div by setting its position to absolute

<div class='img-box'> <img /> //position absolute <img /> //position absolute <img /> //position absolute <img /> //position absolute I am struggling to center the images within this div because of their absolute p ...

Displaying an alert on a webpage that shows a label input using JavaScript and

I'm currently working with HTML5 and JavaScript and I'm facing a challenge. I want to create a feature where users can input any word into a label, and when they click on a button, an alert is triggered with the given text. However, despite my ...

Looking to minimize the amount of HTML code in Angularjs by utilizing ng-repeat

Working with some HTML <tr class="matrix_row" ng-repeat="process in matrixCtrl.processes | filter : { park: parentIndex } track by $index"> <td class="properties" ng-click="dashboardCtrl.currParam=0; dashboardCtrl.currentProcess=process"> ...

How about incorporating AJAX into your HTML code?

I am currently in the process of developing an email system for a company that wishes to use it for sending emails. To achieve this, I have implemented a custom HTML editor for creating email content. My goal is to post the email contents to an external PH ...

Once the input is disabled, what is the best way to delete the previously inserted text?

Here's the scenario: I have two options that display different questions each. If a user decides to switch their option at the last minute, any inputs within option 1 will hide and show option 2. However, all input texts need to be removed and disabl ...

What is preventing images from displaying in my slider within baguetteBox?

Currently, I am in the process of incorporating a slider into my website using the baguetteBox library. After consulting the documentation, I successfully implemented an example that is functioning smoothly without any issues. In order to display a series ...

Manipulating and Parsing HTML files locally using Node.js

Hey there! I have a bit of an odd question that might be on the basic side. I'm diving into web development and front-end work, so I'm still fairly new to it all. The Scenario To give you some background, my experience with JS is mainly from usi ...

What is the most effective method for converting this flash application into a format that is compatible with

I have a unique flash animation that I want to make compatible with iPhones by integrating it into an iPhone app. Unfortunately, I cannot provide a direct link to the animation, but you can view a screenshot of the interface here. This interactive animat ...

"Is there a way to alter the background color of the second span within a Bootstrap row

I currently have 4 WordPress services in a loop, and I am trying to change the background color of the second service. However, when I try to apply the CSS background property, it ends up affecting all of the services. Here is my template code: <div ...

Differences in vertical font positioning between Mac and Windows systems

One element on my page is a quantity balloon for a basket, but I'm struggling to get it looking right. The font and vertical position don't seem to be cooperating for such a simple element: Despite trying solutions from stackoverflow (like setti ...

Connecting a CSS file with an HTML document

Having an issue with my CSS. For instance, here is a snippet from my CSS file .readmore { font-style: italic; text-decoration: none; color: #509743; padding-left: 15px; background: url(../images/more.png) no-repeat 0 50%; } .readmore: ...

Dropdown boxes that fetch data from a database in a cascading manner

Currently, I am working on creating two dropdown boxes that will be dynamically populated from a database using PHP and Ajax. The first dropdown menu will have only one option available, but selecting it should trigger the population of the second dropdown ...

Check out the page design for section one!

I have been attempting to create a link to a specific section on a one-page website. Sometimes it works oddly, but most of the time it gets stuck at the section I clicked on and then manual scrolling does not function properly. From what I've researc ...