I would like to make a dropdown list within every td of a table, something like this:
<!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;
}
.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;
}
.dropdown {
position: relative;
display: inline-block;
}
.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;
}
.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;
}
</style>
</head>
<body>
<div class="dropdown">
<button class="dropbtn">+ operator:0.20.1</button>
<button class="btncritical">critical: 2</button>
<button class="btnheigh">heigh: 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>Mexico</td>
</tr>
</table>
</div>
</div> <br>
<div class="dropdown">
<button class="dropbtn">+ operator:0.20.1</button>
<button class="btncritical">critical: 2</button>
<button class="btnheigh">heigh: 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>Mexico</td>
</tr>
</table>
</div>
</div>
</body>
</html>
Which works perfectly. However, because I will generate the content automatically (the first cell) I would like the dropbtn
buttons to have constant width. I thought I could put it in another table, however, it seems that hovering stopped working:
<!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;
}
.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;
}
.dropdown {
position: relative;
display: inline-block;
}
.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;
}
.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;
}
</style>
</head>
<body>
<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>
<td>
<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>
<td>
<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>
</table>
</body>
</html>
How to solve this? Thanks!
Your first example has two dropdown
elements. Your second only has one. So of course both dropdown-content
elements will appear on a hover. I think you don't want that.
class="dropdown"
so that hovering anywhere in the row will trigger display:block
for that particular row's grey table.width:100%
so that they'd be the same width even with different content.height:100%
and gave td
a definite height so that the buttons' % height can be resolved.width:100%
and making tr
be 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>