Why does the position of the class in the style tag have varying effects on the webpage?

I need some help with CSS as I am not very proficient in it. I am trying to highlight rows in a table when the mouse hovers over them, but currently only the even rows are getting highlighted. Moving the class "highlight" below the class "stripe2" causes all rows to be highlighted correctly. Can someone explain why the positioning of the class definition affects the display?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Striping/Hover Highlighting a Table</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("#theList tr:even").addClass("stripe1");
        $("#theList tr:odd").addClass("stripe2");

        $("#theList tr").hover(
            function () {
                $(this).toggleClass("highlight");
            },
            function () {
                $(this).toggleClass("highlight");
            }
        );
    });
</script>
<style type="text/css">
th,td {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 18px;
    color: #000000;
}
tr {
    border: 1px solid gray;
}
td {
    width:200px;
    padding:3px;
}
th {
    background-color:#D2E0E8;
    color:#003366
}
table {
    border: 1pt solid gray;
}
.clickable {
    cursor:pointer;
}
.stripe1 {
    background-color:#0f0;
}
.highlight {
    background-color: #ffcc00;
    font-weight:bold;
}
.stripe2 {
    background-color:#afa;
}

</style>
</head>
<body>
<h1>Using jQuery to stripe and hover-highlight a table</h1>
<table id="theList">
    <thead>
    <tr>
      <th>Item</th>
      <th>Price</th>
  </tr>
  </thead>
  <tbody>
  <tr>
      <td>Milk</td>
      <td>1.99</td>
  </tr>
  <tr>
      <td>Eggs</td>
      <td>2.29</td>
  </tr>
  <tr>
      <td>Butter</td>
      <td>3.49</td>
  </tr>
  <tr>
      <td>Bread</td>
      <td>0.99</td>
  </tr>
  <tr>
      <td>Pasta</td>
      <td>1.19</td>
  </tr>
  <tr>
      <td>Honey</td>
      <td>4.39</td>
  </tr>
  <tr>
      <td>Cookies</td>
      <td>2.99</td>
  </tr>
  <tr>
      <td>Apples</td>
      <td>0.59</td>
  </tr>
  <tr>
      <td>Sugar</td>
      <td>1.78</td>
  </tr>
  <tr>
      <td>Pepper</td>
      <td>1.56</td>
  </tr>
  </tbody>
</table>
</body>
</html>

Answer №1

The problem arises from the fact that the classes .highlight and .stripe2 have equal specificity, causing the latter in your CSS to override the former. To resolve this issue, you can enhance the specificity of the highlight class to ensure its independence regardless of its position in the CSS hierarchy. One approach is by incorporating the tr tag.

.highlight, specificity = 10

tr.highlight, specificity = 11

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

Repositioning divs using HTML, CSS, and PHP

My divs are moving around when I resize the window. Can someone assist with this issue? I have HTML/PHP code for login and page retrieval. This is my HTML body section with PHP for logging in and managing the webpage content. <div class="menu"> ...

Ensures that two divs have the same dimensions

Currently, I have a line of sections set up like this: I am attempting to ensure that the second div matches the height of the first one, despite the fact that their heights are determined by the size of the pictures which may vary. Do you have any sugges ...

Switching Out Bootstrap Dropdown with Dropup (Varying functionality on two almost identical implementations)

In the midst of my project on Github Pages, I encountered an interesting issue involving replacing a bootstrap .dropdown with .dropup when the div's overflow-y: scroll causes the dropdown menu to be cut off or overflow. The functionality can be viewed ...

Update the 'selected' text with the JQuery fnGetColumnData Plugin

I have implemented the fnGetColumnData plugin for filtering data tables, and it is functioning correctly. However, the dropdowns that appear are labeled "Select," and there are a total of five dropdowns. I would like to know how to change the text "Select" ...

Unable to interact with "load more" button using Selenium

Is there a way to click the "Load More" button multiple times using Selenium? I'm encountering an issue where I am unable to click on the button, even though it's not really a button. Every time I attempt to click on it, I receive the error messa ...

Text in a class button that is not skewed

I crafted a button for my website using a CSS class, aiming for a parallelogram shape by utilizing the skew function. The code worked like a charm, giving the button the desired shape. However, I encountered a dilemma where the text inside the button also ...

Ways to incorporate a focus event into a template

I'm currently working on implementing autocomplete functionality in the search box on my homepage. To achieve this, I have included an onfocus event for the template to execute a jQuery command targeting the source elements. The method I used to add j ...

Can you assign a particular symbol to a function for naming variables?

When incorporating a new function into jQuery: $.fn.boxLoader = function() { var FilterSelect = ($("[id^='filter_a_selector_']")); // perform actions on FilterSelect return this; }); Then, when a par ...

Creating a CSS hover effect for a menu popup

Need help with creating a sidebar on the site, positioned on the left, containing specific menu items and sub-items. The goal is to have a pop-up block appear when hovering over the main menu item that displays the sub-items. The main sidebar has its posit ...

What is the reason behind utilities.scss taking precedence over styles.scss and local scss files?

After defining the mt-5 class in both my component's .scss file and the root styles.scss, I noticed that they were both overwritten by _utilities.scss. This left me wondering why this was happening. https://i.sstatic.net/idnze.png https://i.sstatic.n ...

The swipe function of Hammer.js is unable to detect gestures on a rotated iframe

After creating a rotated iframe on the page using CSS transforms, I attempted to implement swipe events within the iframe. body.rotate iframe { transform: rotate(90deg); transform-origin: top right; position: absolute; t ...

Showing scheduled activities according to their dates in FullCalendar

I'm facing an issue with my recurring weekly events showing up before the current week. How can I make sure that only events from the current week and beyond are displayed? The code I am currently using doesn't seem to work. Here is an example o ...

Is it possible to achieve the full image when utilizing double buffering for drawing on canvas?

I have a code where I am using two canvas elements to draw an image in order to prevent flickering. However, I am facing an issue where I cannot get the full image to display when using this method. When I use just one canvas, the image displays fine. It ...

Can the tags beneath a div be scraped using xpath?

Consider the following: <div class="name" data-starred-src="A Full Url" data-non-starred-src="Some Other URL" data-reorderid="SomeID" data-fbid="SomeID" id="SomeID" style="position: absolute; margin-top: 0px; margin-left: 0px;"> This particular div ...

Styling and scripting with CSS and jQuery in an external JavaScript file

Is it possible to add the jquery library from "http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" into an external .js file? And how can a .css file be included in a .js file? ...

KnockOut.JS compares two arrays and removes the distinct values from the second array

I've been diving into Knockout.js and I have a question. var chosenFruit = ko.observableArray([]); var allFruits = ko.observableArray([]); allFruits = [ "Apple" , "Bananna" , "Grapes" , "Oranges"] chosenFruit = ["Apple" , "Ba ...

Exploring ways to access an element's background color through JavaScript

Is there a way to access an element's CSS properties that are set by a class name in JavaScript? In the case of the first div element, I have applied a "red" class which sets its background color to red. However, when I try to access the div's b ...

Integration of elFinder with TinyMCE 4

Has anyone attempted to integrate elFinder into the latest version (4b1) of TinyMCE? The previous implementation seems to be not working. If you have any snippets or tips, please share. Thank you very much. ...

Ways to achieve a blurred background effect on the body using CSS

I've been experimenting with setting a background color using linear gradient, but now I want to add a blur effect to it. I've tried various codes but haven't had any success so far. Here is the CSS code I have been working on: body{ ...

Blueprint CSS is causing a padding problem

This piece of code is perfectly styled with blueprintcss: <div class="container"> <div class="span-12" style="background-color : cyan"> Hello </div> <div class="span-12 last" style="background-color : green"> Bye </div& ...