Home > Web Front-end > CSS Tutorial > How can I override a conflicting CSS rule from an external stylesheet when using inline styling?

How can I override a conflicting CSS rule from an external stylesheet when using inline styling?

Barbara Streisand
Release: 2024-10-30 03:39:28
Original
282 people have browsed it

How can I override a conflicting CSS rule from an external stylesheet when using inline styling?

CSS Precedence: Understanding Specificity

In this scenario, the inline style padding-left: 10px applied to table cells in the rightColoumn ID is being overridden by a conflicting rule from an external stylesheet. The referenced stylesheet contains a rule that sets margin and padding to 0 for all elements within the rightColumn class.

The Concept of CSS Specificity

To determine which CSS rule takes precedence, a concept called specificity comes into play. Specificity is a measure of how specific the selector of a CSS rule is. The more specific a selector, the higher its specificity.

Calculating Specificity

CSS 2.1 defines the following rules for calculating specificity:

  • Count 1 if the declaration is from a style attribute (inline styling) and 0 if it's not from a style attribute.
  • Count the number of ID attributes in the selector.
  • Count the number of other attributes and pseudo-classes in the selector.
  • Count the number of element names and pseudo-elements in the selector.

These four values are concatenated in the form a-b-c-d, where a is the value from the first rule, b from the second rule, and so on. The higher the numerical value, the higher the specificity.

Applying Specificity to the Given Case

In this case, the inline style has a specificity of 0001 (no ID attributes, no other attributes or pseudo-classes, one element name). The conflicting rule from the external stylesheet has a specificity of 0010 (no ID attributes, one other attribute or pseudo-class, no element names).

Since 0010 is higher than 0001, the rule from the external stylesheet takes precedence and overrides the inline style.

Two Solutions for Overcoming Specificity Issues

To override the conflicting rule, there are two main options:

  1. Use !important: Adding !important to the inline style would make it more important and override the conflicting rule. However, this should be used sparingly as it can introduce confusion.
  2. Use a Higher-Specificity Selector: Modify the inline style by adding a class or ID to the table cells in question. This will create a new rule with a higher specificity than the conflicting rule from the external stylesheet.

The above is the detailed content of How can I override a conflicting CSS rule from an external stylesheet when using inline styling?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template