Yes, you can highlight a table row in an HTML table based on the content of one of its columns using CSS, but this requires a combination of techniques since CSS alone cannot directly select elements based on their text content. Here are two common approaches to achieve this:
Approach 1: Using JavaScript to Add a Class
Since CSS cannot directly detect text content, you can use JavaScript to check for the string in a column and add a CSS class to the corresponding row. Then, you can style that class using CSS.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Highlight Table Row</title>
<style>
/* CSS to highlight the row */
.highlight {
background-color: yellow;
font-weight: bold;
}
</style>
</head>
<body>
<table border="1">
<thead>
<tr>
<th>Name</th>
<th>Status</th>
<th>Age</th>
</tr>
</thead>
<tbody id="tableBody">
<tr>
<td>John</td>
<td>Active</td>
<td>25</td>
</tr>
<tr>
<td>Jane</td>
<td>Inactive</td>
<td>30</td>
</tr>
<tr>
<td>Bob</td>
<td>Active</td>
<td>22</td>
</tr>
</tbody>
</table>
<script>
// JavaScript to highlight rows based on column content
const rows = document.querySelectorAll('#tableBody tr');
rows.forEach(row => {
const statusCell = row.cells[1]; // Index 1 is the "Status" column
if (statusCell.textContent === 'Active') {
row.classList.add('highlight');
}
});
</script>
</body>
</html>
Explanation:
- HTML: A simple table with columns for "Name", "Status", and "Age".
- CSS: The
.highlight class defines the styling for highlighted rows (e.g., yellow background).
- JavaScript:
- Selects all rows in the table body.
- Checks the text content of the "Status" column (index 1) in each row.
- Adds the
highlight class to the row if the text matches "Active".
Approach 2: Using CSS Attribute Selectors (Limited Use Case)
If the string you want to match is part of an attribute (e.g., a data-* attribute) rather than plain text, you can use CSS attribute selectors to highlight the row. This approach avoids JavaScript but requires modifying the HTML structure to include attributes.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Highlight Table Row with CSS</title>
<style>
/* CSS to highlight the row based on data attribute */
tr[data-status="Active"] {
background-color: yellow;
font-weight: bold;
}
</style>
</head>
<body>
<table border="1">
<thead>
<tr>
<th>Name</th>
<th>Status</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr data-status="Active">
<td>John</td>
<td>Active</td>
<td>25</td>
</tr>
<tr data-status="Inactive">
<td>Jane</td>
<td>Inactive</td>
<td>30</td>
</tr>
<tr data-status="Active">
<td>Bob</td>
<td>Active</td>
<td>22</td>
</tr>
</tbody>
</table>
</body>
</html>
Explanation:
- HTML: Each
<tr> element has a data-status attribute that mirrors the content of the "Status" column.
- CSS: The
tr[data-status="Active"] selector targets rows where the data-status attribute equals "Active" and applies the desired styling.
Limitations of Approach 2:
- This approach requires modifying the HTML to include
data-* attributes, which may not be practical if the table is dynamically generated or if you cannot control the HTML structure.
- CSS attribute selectors cannot match plain text content inside elements; they only work with attributes.
Which Approach to Use?
- Use Approach 1 (JavaScript) if you need to match plain text content in a column and cannot modify the HTML structure. This is the most flexible and commonly used method.
- Use Approach 2 (CSS Attribute Selectors) if you can modify the HTML to include
data-* attributes and prefer a pure CSS solution.
Notes:
- If you're working with a large dataset or dynamic content, consider using a JavaScript framework or library (e.g., jQuery, React, Vue) to simplify the logic.
- For case-insensitive matching in JavaScript, you can use
.toLowerCase() or .includes() for partial matches, depending on your needs.