Certainly! To highlight only the box (i.e., cell) under the cursor in a table using HTML and CSS (and optionally JavaScript), you can use a combination of CSS for basic hover effects and JavaScript for more advanced controls.
Here's a simple example using only CSS:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Highlight Cell</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<table border="1">
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
</tr>
<tr>
<td>Cell 4</td>
<td>Cell 5</td>
<td>Cell 6</td>
</tr>
<tr>
<td>Cell 7</td>
<td>Cell 8</td>
<td>Cell 9</td>
</tr>
</table>
</body>
</html>
CSS (styles.css)
/* Basic styling for the table */
table {
width: 50%;
border-collapse: collapse;
margin: 20px auto;
}
td {
padding: 20px;
text-align: center;
}
/* Highlight cell on hover */
td:hover {
background-color: yellow; /* change this to your desired color */
}
This basic CSS will highlight a cell when the mouse cursor hovers over it.
Advanced Example with JavaScript
If you need more control and want to use JavaScript (for example, to support older browsers that might not fully support CSS :hover), you can use the following approach:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Highlight Cell</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<table border="1">
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
</tr>
<tr>
<td>Cell 4</td>
<td>Cell 5</td>
<td>Cell 6</td>
</tr>
<tr>
<td>Cell 7</td>
<td>Cell 8</td>
<td>Cell 9</td>
</tr>
</table>
<script src="script.js"></script>
</body>
</html>
CSS (styles.css)
/* Basic styling for the table */
table {
width: 50%;
border-collapse: collapse;
margin: 20px auto;
}
td {
padding: 20px;
text-align: center;
}
.highlight {
background-color: yellow; /* change this to your desired color */
}
JavaScript (script.js)
// JavaScript to add hover effect to each cell
document.querySelectorAll('td').forEach(cell => {
cell.addEventListener('mouseover', () => {
cell.classList.add('highlight');
});
cell.addEventListener('mouseout', () => {
cell.classList.remove('highlight');
});
});
This method uses JavaScript to add a highlight class to a cell when it is hovered over and remove it when the mouse moves away.
Feel free to adjust the styles and colors to your preference!