Home Web Front-end CSS Tutorial Understanding style priority in CSS: Explanation of CSS style priority order

Understanding style priority in CSS: Explanation of CSS style priority order

Nov 01, 2018 pm 03:29 PM

This article introduces to you the css style priority and gives you an in-depth understanding of the order of css style priority.

Understanding style priority in CSS: Explanation of CSS style priority order

Have you ever been in a situation where you tried to apply a css style to an element, but it had no effect? The page seems to ignore the CSS you defined, but you may not be able to figure out why. Maybe you will! important or add inline styles as a last resort. But actually the problem you are having is probably one of css priority. (Recommended course: css video tutorial)

A better understanding of which css styles to use first can lead to a clearer and more organized way to write css code, so let’s take a look at controlling a given html Three css rules for elements:

css priority calculation

css inheritance

css cascading

Learning these rules will make Take your CSS development to the next level.

Priority Calculation

Imagine that your html contains a paragraph that has a type of "creature" applied to it. You also have the following two css rules:

p { font-size :12px }
p.bio { font-size :14px }
Copy after login

Do you want the text size in the paragraph to be 12px or 14px? In this case you can guess it will be 14px. The second line of css (p.bio) is more specific than your class="bio" paragraph. However, sometimes priorities are not easy to see.

For example, consider the following html and css

<div id = “sidebar” >
<p class = “bio” >文字在这里</ p>
</ div>
Copy after login
div p.bio { font-size :14px }
#sidebar p { font-size :12px }
Copy after login

At first glance, the first line of css may seem more specific, but in fact the second line above is more consistent with the paragraph's font size. why is that?

To answer this question, we need to consider priority rules.

Specificity is calculated by counting the various components of css and expressing them in the form (a, b, c, d). This will be clearer with an example, but first the components.

Element, pseudo-element: d = 1 - (0,0,0,1)

Class, pseudo-class, attribute: c = 1 - (0,0,1,0)

Id: b = 1 - (0,1,0,0)

Inline style: a = 1 - (1,0,0,0)

The id is more specific than the class rather than the element.

You can calculate the priority by calculating each of the above and adding 1 to a, b, c or d. It's also important to note that 0,0,1,0 is more specific than 0,0,0,15. Let's look at some examples to make the calculation clearer.

p: 1 element - (0,0,0,1)

div: 1 element - (0,0,0,1)

#sidebar :1 element, 1 id - (0,1,0,0)

div#sidebar: 1 element, 1 id - (0,1,0,1)

div#sidebar p: 2 elements, 1 id - (0,1,0,2)

div#sidebar p.bio: 2 elements, 1 class, 1 id - (0,1,1 ,2)

Let’s look at the above example again

div p.bio { font-size :14px }  - (0,0,1,2)
#sidebar p { font-size :12px }  - (0,1,0,1)
Copy after login

The second one has higher priority and therefore takes precedence.

One final point before we move forward. Importance trumps priority when you use it! When you mark the css attribute important, you override the priority rules etc.

div p.bio { font-size :14px !important }    
#sidebar p { font-size :12px }
Copy after login

means that the first line of css above takes precedence over the second line instead of the second one. ! important is still the specificity surrounding the basic rules that you should never need if you understand how the rules work.

Inheritance

The idea behind inheritance is relatively easy to understand. Elements inherit styles from their parent container. If the body tag is set to use color:red, then the text of all elements in the body will also be red unless otherwise specified.

However, not all css properties are inherited. For example, margins and padding are non-inherited properties. If you set margins or padding on a div, the paragraphs inside the div will not inherit the margins and padding you set on the div. The paragraph will use default browser margins and padding until you declare otherwise.

However, you can explicitly set properties to inherit styles from its parent container. For example, you could declare

p { margin :inherit ; 填充:继承 }
Copy after login

and your paragraph would inherit from its containing element.

Cascading

At the highest level, cascading controls all CSS priorities and works as follows.

1. Find all CSS declarations that apply to related elements and attributes.

2. Sort by origin and weight. Origin refers to the source of the declaration (author style, user style, browser default value), and weight refers to the importance of the declaration. (The author's weight is greater than the user's weight is greater than the default value. !important is more important than the normal declaration)

3. Calculate the priority

4. If two rules among all the above rules are the same, Then the last rule wins. CSS embedded in html always comes after the external stylesheet, regardless of the order in the html.

#3 above is probably what you need to pay attention to the most. With #2 just understand that your styles will override how users set up their browsers unless they set the rule to important.

Also realize that your styles will override browser defaults, but these defaults do exist and often cause cross-browser issues. Using a reset file like Eric Meyer's CSS Reset or Yahoo's YUI Reset CSS helps take default styles out of the equation.

Summarize

Hope the above helps clarify some of your CSS priority issues. Most of the time, if your styles conflict, the issue will come down to priorities. Sometimes you haven't declared some CSS, but an element behaves in a way you don't expect it may have inherited some CSS from the parent container or the browser's default styles.

The general rule of thumb when declaring CSS is to declare the attribute with the lowest priority for styling an element. For example, use #sidebar instead of div#sidebar. I admit to breaking this general rule far more than I should, but by using the minimum required precedence it will make it easier for you to override styles by declaring more specific ones.

If you use highest priority, you may run into problems later and find yourself having to add unnecessary HTML to be able to add more priorities, or you may find yourself not using it anymore! important or declare the style inline. Start with the smallest priority and add more only when needed.

The above is the detailed content of Understanding style priority in CSS: Explanation of CSS style priority order. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1672
14
PHP Tutorial
1277
29
C# Tutorial
1257
24
A Comparison of Static Form Providers A Comparison of Static Form Providers Apr 16, 2025 am 11:20 AM

Let’s attempt to coin a term here: "Static Form Provider." You bring your HTML

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

Weekly Platform News: HTML Loading Attribute, the Main ARIA Specifications, and Moving from iFrame to Shadow DOM Weekly Platform News: HTML Loading Attribute, the Main ARIA Specifications, and Moving from iFrame to Shadow DOM Apr 17, 2025 am 10:55 AM

In this week&#039;s roundup of platform news, Chrome introduces a new attribute for loading, accessibility specifications for web developers, and the BBC moves

Some Hands-On with the HTML Dialog Element Some Hands-On with the HTML Dialog Element Apr 16, 2025 am 11:33 AM

This is me looking at the HTML element for the first time. I&#039;ve been aware of it for a while, but haven&#039;t taken it for a spin yet. It has some pretty cool and

Paperform Paperform Apr 16, 2025 am 11:24 AM

Buy or build is a classic debate in technology. Building things yourself might feel less expensive because there is no line item on your credit card bill, but

Weekly Platform News: Text Spacing Bookmarklet, Top-Level Await, New AMP Loading Indicator Weekly Platform News: Text Spacing Bookmarklet, Top-Level Await, New AMP Loading Indicator Apr 17, 2025 am 11:26 AM

In this week&#039;s roundup, a handy bookmarklet for inspecting typography, using await to tinker with how JavaScript modules import one another, plus Facebook&#039;s

Where should 'Subscribe to Podcast' link to? Where should 'Subscribe to Podcast' link to? Apr 16, 2025 pm 12:04 PM

For a while, iTunes was the big dog in podcasting, so if you linked "Subscribe to Podcast" to like:

Options for Hosting Your Own Non-JavaScript-Based Analytics Options for Hosting Your Own Non-JavaScript-Based Analytics Apr 15, 2025 am 11:09 AM

There are loads of analytics platforms to help you track visitor and usage data on your sites. Perhaps most notably Google Analytics, which is widely used

See all articles