The CSS Specificity Hierarchy

I am currently facing a dilemma with two CSS classes in my codebase. Despite having a stronger specificity, the second class is not being chosen over the first one. Can anyone shed some light on this issue?

The CSS class that is currently being applied is -

td, th, table {   border-collapse: collapse;   border: 1px solid #999; }

Whereas, the CSS that I actually intend to use is -

  table.cancellation {     border: none;    }

I assumed that since the second class has a class selector, it would have higher specificity. Why does this assumption seem to be incorrect in practice?

Answer №1

You have adjusted the styling for the table, but not for the cells.

In the initial table, all elements are enclosed within a red border.

For the next table, the table itself is outlined in blue, however, due to the use of border-collapse:collapse, the red borders of the td and th take precedence.

The third table employs border-collapse:separate, clearly showing the blue border surrounding the table.

Finally, in the last table, the styles for td and th have been overwritten, resulting in a table without any visible borders.

th,
td,
table {
  border-collapse: collapse;
  border: 1px solid red;
  margin-bottom:1rem;// just for looks
}
table.table {
  border-color: blue
}

table.separate{
border-collapse:separate;
}

table.none,
table.none th,
table.none td{
border:none
}
<table>
  <tr>
    <th>
      Head
    </th>
    <td>
      cell
    </td>
</table>

<table class = "table">
  <tr>
    <th>
      Head
    </th>
    <td>
      cell
    </td>
</table>

<table class = "table separate">
  <tr>
    <th>
      Head
    </th>
    <td>
      cell
    </td>
</table>

<table class = "table none">
  <tr>
    <th>
      Head
    </th>
    <td>
      cell
    </td>
</table>

Answer №2

When it comes to CSS, the order matters as it reads from top to bottom. If you have a class defined for elements like

td, th, table {   border-collapse: collapse;   border: 1px solid #999; }
, and then later on have another class for
table.cancellation {     border: none;    }
, the properties of the last one will be applied.

If you want to ensure that a certain property is always used, irrespective of its position in the code, you can use "border: none !important". This rule takes precedence over everything else.

If neither of these techniques solves your issue, it might be helpful to provide more context by sharing the relevant HTML and complete CSS for the specific div element.

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

How can I use jQuery to transform a div with its elements into a canvas?

Looking for a way to convert a HTML Div and its elements to canvas, and then the canvas to an image using Jquery. I've checked out a website that only converts the whole HTML page, but I need a solution for converting specific Div elements. Any help w ...

Invoking a function means calling another one simultaneously

There are two buttons in my code: The button on the right triggers a function called update(): <script> function update(buttonid){ document.getElementById(buttonid).disabled = true; event.stopPropagation(); var textboxid = buttonid.sli ...

Trouble with CSS positioned image rendering in Internet Explorer? Z-index not solving the issue?

I've been working on a timeline project where the completed sections should have a green background with a check mark image in the foreground. It appears correctly in Firefox, Chrome, and Safari. However, in IE 7 and 8 (and possibly 9), the layout is ...

MUI Grid - justify content to the right

I'm new to utilizing the MUI Grid system and I am currently trying to accomplish a simple task of aligning my 'Cancel' and 'Save' buttons all the way to the right on the screen. Despite browsing through various posts and documentat ...

Creating a responsive grid layout with HTML and CSS

Is there a way to adjust the grid layout so that the second row of blocks align just below the corresponding block above instead of creating an entirely new row? http://jsfiddle.net/AndyMP/wSvrN/ body { margin-left: 0px; margin-top: 0px; marg ...

"Verifying the dimensions of the input sizes with the i

It's possible that this question has been asked before on this platform. However, I haven't come across a satisfactory answer yet. How can I adjust the size of inputs in the iCheck library? The current size is too large for my website. Css: .ic ...

Having difficulty adjusting the padding of the Material UI table

I have been using the Table component provided by the material-ui library in my React project. However, I have encountered an issue where each row, including the header, has a padding of 24px on the top and bottom that I am unable to modify or overwrite. D ...

Text that disappears upon clicking on show/hide按钮

Could someone please help me figure out how to prevent the "See more" text from disappearing when clicked in the example below? I want it to stay visible. Thank you! ...

Images are suddenly encircled by a mysterious border

On my wordpress website, I encountered a strange issue with some photos. Specifically, an image of a cross in a circle is getting a weird border around it, but only on certain resolutions (such as width:1506). Despite inspecting the element, I couldn' ...

What is the best way to incorporate padding into an Angular mat tooltip?

I've been attempting to enhance the appearance of the tooltip dialog window by adding padding. While adjusting the width and background color was successful, I'm encountering difficulties when it comes to styling the padding: https://i.sstatic.ne ...

Troubleshooting HTML Label Problems

I am facing an issue with my custom labels. .label-ras{ padding:3px 10px; line-height:15px; color:#fff; font-weight:400; border-radius:5px; font-size:75%; } .label-primar{background-color:#5c4ac7} <span style="background- ...

Restrict printing loops within the designated division layer

Is there a way to limit the number of rows displayed horizontally when using foreach? I only want to display 7 rows. Can this be achieved with CSS or pagination? Here is an image illustrating the issue: https://i.sstatic.net/yJx3N.png Currently, it print ...

Floating CSS3 animations within HTML elements

I'm struggling to understand why the animated arrow on my website refuses to go behind the other elements. The animation code I've used for the arrow in CSS3 is as follows: -webkit-animation-fill-mode:both; -moz-animation-fill-mode:both; -ms-an ...

Tips for incorporating a Python script into a online project

I've been working on a Python code that detects faces and eyes using face recognition. When the code runs in PyCharm, it displays a camera window. Now I'm trying to figure out how to integrate this window into a webpage project written in HTML, C ...

The font fails to load

I've hit a roadblock with this issue. I'm attempting to add a custom font to my project. The CSS file can be found in the directory project/css/. The font is located at project/fonts/iconfont/. In that folder, I have the following font files (alt ...

The issue with hiding elements using .setAttribute in HTML/CSS is not occurring as expected

function showEditStudentProfile() { document.getElementById('editstudentprofile').setAttribute('class', 'unhide'); document.getElementById('profile_viewStudent').setAttribute('class', ' ...

Managing the scrolling direction horizontally with waypoints.js

As I work on creating a custom wizard form with waypoints, I've encountered an interesting issue that has left me puzzled. In my sample CODEPEN, you can see two pages of the wizard process to better understand the problem. Upon clicking the forward ...

The appearance of the mock button varies between the development and production environments due to CSS styling

I'm currently using CSS to style a hyperlink in order to give it the appearance of a button. This is for a .NET website. a.pretend { display:inline-block; border-top:1px solid #CCCCCC; border-left:1px solid #CCCCCC; border-bottom:1px solid #999999; b ...

Steps for displaying a post's image when hovering over its link in WordPress

One section of my website displays the latest 6 posts, and here is the HTML code for it: <div class="latest-posts"> <div id="latest-posts-title"> <p>Latest Posts</p> </div> <div id="latest-posts-pictures" ...

A step-by-step guide to implementing Inline Linear Gradient in Nuxt/Vue

How can I create a linear gradient overlay on top of my inline background image? I attempted to add a CSS style but the class seems to be positioned beneath the image. <div class="header__bkgd" v-bind:style="{'background-image': 'url(&ap ...