The CSS hover effect on a <td> element is malfunctioning and not working as expected

My goal is to create a dropdown list embedded within each td cell of a table, similar to this setup:

<!DOCTYPE html>
<html>

  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
      .dropbtn {
        background-color: #04AA6D;
        color: black;
        padding: 16px;
        font-size: 16px;
        border: 2px solid #ccc;
      }

      ...

    </style>
  </head>

  <body>

    <div class="dropdown">
      <button class="dropbtn">+ operator:0.20.1</button>
      ...
      
      // Table structure with dropdown content

    </div> <br>

    <div class="dropdown">
      ...
      
      // Another table structure with dropdown content
      
    </div>

  </body>

</html>

This method has been effective so far. However, since I plan to automate the content generation for the first cell, I want the dropbtn buttons to maintain a consistent width. My initial attempt at placing them in another table caused issues with hover functionality:

<!DOCTYPE html>
<html>

  <head>
    [<meta tag and CSS styles here>]
  </head>

  <body>

    <div class="dropdown">

      <table>
        [<tr> and <td> elements with styled buttons inside<]

        // Embedded table for dropdown content

      </table>

    </div>


  </body>

</html>

If you have any insights on how to address this issue, it would be greatly appreciated! Thank you!

Answer №1

In your initial example, there are two dropdown elements, while in the second example, there is only one. As a result, both dropdown-content elements will appear on hover. I believe this is not the desired outcome.

  • I positioned the grey abspos table after the button in the first cell instead of the last cell to prevent it from appearing too far to the right.
  • Each row is now given the class "dropdown" so that hovering anywhere in the row will display the particular row's grey table.
  • The left-most buttons now have width:100% to ensure they are the same width regardless of content.
  • Edit: Buttons now have height:100%, and td has a definite height to resolve the buttons' percentage height.
  • Previously added script to match the abspos table width with the parent table. Refer to the comment below. Edit: The abspos table now derives its width from width:100%, with the tr acting as the relpos container.

.dropbtn {
  background-color: #04AA6D;
  color: black;
  padding: 16px;
  font-size: 16px;
  border: 2px solid #ccc;
  width: 100%;
}

.btncritical {
  background-color: #fc0303;
  color: black;
  padding: 16px;
  font-size: 16px;
  border: none;
  border: 2px solid #ccc;
}

.btnheigh {
  background-color: #fc7703;
  color: black;
  padding: 16px;
  font-size: 16px;
  border: 2px solid #ccc;
}

button {
  height: 100%;
}

td {
  /* Cell apparently has to have a definite height, even if it is ignored, to resolve a child's % height. */
  height: 10px;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
  /* This width: 100% resolves off the relpos containing block, which is the tr.dropdown. */
  width: 100%;
}

.dropdown {
  position: relative;
}

.dropdown-content a {
  color: black;


padding: 12px 16px;

text-decoration: none;
  
display: block;
}

.dropdown-content a:hover {
 
background-color: #ddd;
 
}

.dropdown:hover .dropdown-content {

 
display: block;
  

}

.dropdown:hover .dropbtn {

 
background-color: #3e8e41;
 

}
<table id="bigtable">
  <tr class="dropdown">
    <td><button class="dropbtn">+ clickhouse-operator:0.20.1</button>
      <div class="dropdown-content">


        <table style="width:100%">


          <tr>

            <td>Alfreds Futterkiste</td>

            <td>Maria Anders</td>

            <td>Germany</td>

          </tr>
          
<tr>

            <td>Centro comercial Moctezuma</td>

            <td>Francisco Chang</td>

            <td>some very very long country name</td>

          </tr>

        </table>

      </div>

    </td>

    <td><button class="btncritical">critical: 2</button></td>

    <td><button class="btnheigh">heigh: 1</button></td>


  </tr>
  
<tr class="dropdown">


    <td><button class="dropbtn">+ clickhouse-operator:0.20.1 longer</button>


      <div class="dropdown-content">


        <table style="width:100%">


          <tr>

            <td>Alfreds Futterkiste</td>

            <td>Maria Anders</td>

            <td>Germany</td>

          </tr>
          
<tr>

            <td>Centro comercial Moctezuma</td>

            <td>Francisco Chang</td>

            <td>Mexico</td>

          </tr>

        </table>

      </div>

    </td>

    <td><button class="btncritical">critical: 2</button></td>

    <td><button class="btnheigh">heigh: 1</button></td>

  </tr>

</table>

Answer №2

Even without modifying your CSS, I was able to achieve the desired outcome by adjusting the placement of the dropdown element in this fiddle. Essentially, I created a new tr element and housed the dropdown div within a td element with a colspan of 3 (to maintain consistency with the previous tr's dimensions). You can see the working example below by making changes only to your HTML while retaining the same CSS.

Check out the live demonstration on JSFiddle: https://jsfiddle.net/ilanpatao/5zytep0b/12/

Revised HTML:

<div class="dropdown">

  <table>
    <tr>
      <td style="width: 100%"><button class="dropbtn">+ clickhouse-operator:0.20.1</button></td>
      <td style="width: 100%"><button class="btncritical">critical: 2</button></td>
      <td style="width: 100%"><button class="btnheigh">heigh: 1</button></td>
    </tr>
    <tr>
      <td colspan=3>
        <div class="dropdown-content">
          <table style="width:100%">
            <tr>
              <td>Alfreds Futterkiste</td>
              <td>Maria Anders</td>
              <td>Germany</td>
            </tr>
            <tr>
              <td>Centro comercial Moctezuma</td>
              <td>Francisco Chang</td>
              <td>Mexico</td>
            </tr>
          </table>
        </div>
      </td>
    </tr>
    <tr>
      <td style="width: 100%"><button class="dropbtn">+ clickhouse-operator:0.20.1</button></td>
      <td style="width: 100%"><button class="btncritical">critical: 2</button></td>
      <td style="width: 100%"><button class="btnheigh">heigh: 1</button></td>
    </tr>
    <tr>
      <td colspan=3>
        <div class="dropdown-content">
          <table style="width:100%">
            <tr>
              <td>Alfreds Futterkiste</td>
              <td>Maria Anders</td>
              <td>Germany</td>
            </tr>
            <tr>
              <td>Centro comercial Moctezuma</td>
              <td>Francisco Chang</td>
              <td>Mexico</td>
            </tr>
          </table>
        </div>
      </td>
    </tr>
  </table>
</div>

This strategy may provide some helpful guidance for achieving your desired layout with a bit of adjustment and fine-tuning;

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

Tips for creating responsive designs with specific media queries for various device sizes

I am looking to create a responsive website using media queries and have already created different media queries for mobile, tablets, and desktops. However, I am unsure if I should be writing the same CSS code multiple times for various device sizes, such ...

Is there a way to specify both a fixed width and a custom resizable length for a Spring <form:textarea /> element?

Despite extensive research, I have yet to find an answer to my specific problem. Within a model window, I have a textarea element: <div class="form-group"> <label for="groupDescription" class="col-sm-2 control-label"> Descripti ...

The inner DIV cannot be accessed using document.getElementById due to its limited scope

Here is the HTML code below: <Div id="one"> <Div id="two"> </Div> </Div> When trying to access div "two" using "document.getElementById("two")", it returns null. ...

I am having trouble with the $lookup in MongoDB and Node.js as it is not returning any results

I've been struggling to find a way to search for data with a foreign key in my model. Here is the structure of my model: const mongoose = require('mongoose'); const UserCGN = mongoose.Schema({ username: String, cours: String }) mod ...

Expo BarCodeScanner becomes unresponsive (specifically, the camera) upon exiting the application

I am using Expo's BarCodeScanner component within a tab: return ( <View style={{ flex: 1, flexDirection: "column", justifyContent: "flex-end", }} > <BarCodeScanner onBarCodeScanned={s ...

Choose from the dropdown menu, not from the text

I'm having trouble aligning a select element in Bootstrap without centering the text inside it. Can anyone provide a solution for this issue? I've attempted to center the containing div and then set the text-align property to left for the select ...

"Encountering issues with toggle effect in nested divs when utilizing an Angular Directive -

I have included a link to the Plunker. Unfortunately, I am facing issues with accessing the elements .gc and .mc. On the other hand, the class .bc is functioning properly. My goal is to implement a toggle effect on them based on hierarchy: BroadCategory ...

I am unsuccessful in transferring the "side-panel content" to the side panel located on the main menu page

I am facing an issue where I want to pass My left and right Panel to the main menu page (dashboard), but it is not working as expected. The problem arises because the first page that needs to be declared is the login page (/ root) in my case. If I pass it ...

What is the procedure for adding a data table to an HTML table with JSON?

I'm trying to display a table fixture layout in an HTML table using the Ajax method, but it's not working. I'm not sure what the problem is. Can someone please help me out with my code? Controller JsonResult public JsonResult FixturesVal( ...

Could someone review my coding syntax in JavaScript for utilizing indexOf, split, and looping through multiple inputs to paste the splits?

As someone who is self-taught and codes part-time as a hobby, I am currently working on building a JavaScript/jQuery tool. This tool will allow users to copy rows or columns from Excel and paste them into an online HTML form consisting of a grid of <tex ...

Ensuring a DIV remains fixed in place for a specific number of scrolls

I'm looking to create a 'Page section' that remains in place while scrolling for a specific distance and then smoothly transitions to the next section. I've attempted to implement this within the child theme without success... Any sugge ...

Building a TTL based schema in NestJs with MongooseIn this guide, we will explore

In my NestJs(TypeScript) project, I am attempting to create a self-destructing schema using the mangoose and @nestjs/mongoose libraries. Unfortunately, I have been unable to find a clear way to implement this feature. While I know how to do it in an expres ...

Adjusting the opacity of images in a Bootstrap 4 Jumbotron without affecting the text

Is there a way to apply an opacity of 0.5 to only the background image in my jumbotron, without affecting the text? Currently, when I set the opacity for the background, it also affects the text. How can this be resolved? Below is the custom CSS file: .j ...

Tips for improving the readability of my code

This code snippet pertains to handling a POST request in Express.js with MongoDB integration. router.post('/', function(req, res){ var data = req.body; Tag.find({name: data.name}).limit(1).exec( function(err, result){ if(err) { // ...

showcasing products from database with the help of Angular 12

Here are the files related to the item: Item file And here is the component file: Component file Lastly, this is the data service file: Data Service file However, issues arise when testing the code with console log statements as it indicates that the ...

Discover the basics of incorporating libraries using npm

As a beginner in JavaScript, I am looking to incorporate moment.js or another library into my project. However, I am unsure of how to properly set up my project so that I can import from the library. Here is how I have structured my HTML: <!DOCTYPE html ...

Browsing a webpage within a tab of another webpage via a Windows Phone 8 application

Currently engaged in C# programming for Windows Phone 8, I am faced with the challenge of accessing a specific HTML page containing students' subject grades (let's call it page 2). This particular HTML page is linked through an href tag located ...

Adding additional rows to an Array Object in JavaScript based on a certain condition

I am faced with a scenario where I have an array object structured as follows [ { id: 123, startdate: '2022-06-05', enddate: '2023-04-05' },{ id: 123, startdate: '2021-06-05', enddate: '2021-04-05' } ] The task at h ...

I am having issues with the external CSS and JS files not functioning properly in my project

I'm currently working on a project that has the following file structure: https://i.sstatic.net/ixd9M.png However, I am facing issues with loading my CSS and JS files... <link rel="stylesheet" href="./css/main.css"> <sc ...

How can you trigger the activation of a component in Angular 2 without needing to navigate to the actual

Currently, I am developing an application that includes a cart feature as a separate module component. When clicking the button to add items to the cart, I have implemented a subscription event in the cart component: ngOnInit(): void { this._cartSer ...