search
HomeWeb Front-endCSS TutorialCSS priority and inheritance issues

CSS priority and inheritance issues

★CSS conflict, that is, priority
The setting of CSS itself can apply multiple styles to the same element at the same time, and conflicts between styles may occur at this time. And cannot achieve the effect that users want.
★Priority rules for resolving CSS conflicts:
● Priority of CSS cascading style sheet introduction methods: Inline > Embedded > Linked > Imported
● In multiple external In the style, the priority of the style that appears later is higher than the style that appears first, which is commonly known as override
● In the style, the priority of the selector: ID style>class style>mark style (with weight metaphor : The weight of id is 100, the weight of class is 10, and the weight of tag name is 1)
● Add an !important after the style, for example: .abc {background:#fff !improtant;} The priority of this style The level will be raised to the top level by default, and global styles will not be able to affect it.
✪Note: !important should be written in front of the semicolon
Priority:
Proximity principle (the closer to the code, the higher the priority)
The smaller the selection range, the higher the priority. Refine the CSS, wrap it layer by layer by adding more selectors of the parent element, and narrow the selection range

★Style inheritance:
Inheritance means that we set the CSS style of the superior (parent), and the superior (parent) level) and the children below (subordinates) have this attribute. Generally, only text text has inherited properties, such as text size, text boldness, text color, font, etc. But note that some CSS styles are not inherited. For example, border: 1px solid red;

demo:

<p>如果你不知道自己<span>想做什么该做什么</span>,那你什么都做不好。</p>

Description: (1) I set the text of the superior (parent: p) The color is red, and the text color of the child (span) is not set, but the text has inheritance properties, so the child text content is still red.
(2) If after setting the parent text color style, its multiple children will be the same as the parent due to inheritance. If the color of some children does not want to be the same as the parent, then you only need to set the required color for the corresponding child. Can.
✪Note: There is another weight that is special - inheritance also has a weight but it is very low. Some literature suggests that it is only 0.1, so it can be understood that inheritance has the lowest weight.

Benefits of inheritance: You can only set the upper-level CSS style sheet properties, and the children (subordinates) do not need to set them. They all have this CSS property, which can reduce CSS code and facilitate maintenance.

★Attributes that can and cannot be inherited in CSS
1. Properties without inheritance

1. display: Specifies the type of box that the element should generate

2. Text attributes:

vertical-align: Vertical text alignment

text-decoration: Specifies the decoration added to the text

text-shadow: Text shadow effect

white-space: processing of whitespace characters

unicode-bidi: setting the direction of text

3. Box model attributes: width, height, margin, margin-top, margin- right, margin-bottom, margin-left, border, border-style, border-top-style, border-right-style, border-bottom-style, border-left-style, border-width, border-top-width, border-right-right, border-bottom-width, border-left-width, border-color, border-top-color, border-right-color, border-bottom-color, border-left-color, border-top, border-right, border-bottom, border-left, padding, padding-top, padding-right, padding-bottom, padding-left

4, background attributes: background, background-color, background-image, background-repeat, background-position, background-attachment

5, positioning attributes: float, clear, position, top, right, bottom, left, min-width, min-height, max-width, max- height, overflow, clip, z-index

6, generated content attributes: content, counter-reset, counter-increment

7, outline style attributes: outline-style, outline-width, outline-color, outline

8, page style attributes: size, page-break-before, page-break-after

9, sound style attributes: pause-before, pause-after, pause, cue-before, cue-after, cue, play-during

2. Inherited properties

1. Font series properties

font: combined font

font-family: Specify the font family of the element

font-weight: Set the thickness of the font

font-size: Set the size of the font

font- style: Defines the style of the font

font-variant: Sets the font in small caps to display the text, this means that all lowercase letters will be converted to uppercase, but all letters using small caps font will be different from the rest of the text Its font size is smaller compared to .

font-stretch: Stretch and transform the current font-family. Not supported by all major browsers.

font-size-adjust: Specify an aspect value for an element so that the x-height of the preferred font is maintained.

2. Text series attributes

text-indent: text indent

text-align: text horizontal alignment

line-height: line height

Word-spacing: Increase or decrease the space between words (i.e. word spacing)

letter-spacing: Increase or decrease the space between characters (character spacing)

text- transform: Control text case

direction: Specify the writing direction of text

color: Text color

3. Element visibility: visibility

4. Table layout attributes: caption-side, border-collapse, border-spacing, empty-cells, table-layout

5, list Layout attributes: list-style-type, list-style-image, list-style-position, list-style

6, generated content attributes: quotes

7, cursor attributes: cursor

8. Page style attributes: page, page-break-inside, windows, orphans

9. Sound style attributes: speak, speak-punctuation, speak-numeral, speak-header, speech- rate, volume, voice-family, pitch, pitch-range, stress, richness,, azimuth, elevation

3. Attributes that can be inherited by all elements

1. Element visibility: visibility

2. Cursor attribute: cursor

4. Attributes that can be inherited by inline elements

1. Font family attributes

2.Except text-indent. Text series attributes other than text-align

5. Attributes that can be inherited by block-level elements

1. text-indent, text-align

More CSS is preferred For related articles on level and inheritance issues, please pay attention to the PHP Chinese website!

Statement
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
How much specificity do @rules have, like @keyframes and @media?How much specificity do @rules have, like @keyframes and @media?Apr 18, 2025 am 11:34 AM

I got this question the other day. My first thought is: weird question! Specificity is about selectors, and at-rules are not selectors, so... irrelevant?

Can you nest @media and @support queries?Can you nest @media and @support queries?Apr 18, 2025 am 11:32 AM

Yes, you can, and it doesn't really matter in what order. A CSS preprocessor is not required. It works in regular CSS.

Quick Gulp Cache BustingQuick Gulp Cache BustingApr 18, 2025 am 11:23 AM

You should for sure be setting far-out cache headers on your assets like CSS and JavaScript (and images and fonts and whatever else). That tells the browser

In Search of a Stack That Monitors the Quality and Complexity of CSSIn Search of a Stack That Monitors the Quality and Complexity of CSSApr 18, 2025 am 11:22 AM

Many developers write about how to maintain a CSS codebase, yet not a lot of them write about how they measure the quality of that codebase. Sure, we have

Datalist is for suggesting values without enforcing valuesDatalist is for suggesting values without enforcing valuesApr 18, 2025 am 11:08 AM

Have you ever had a form that needed to accept a short, arbitrary bit of text? Like a name or whatever. That's exactly what is for. There are lots of

Front Conference in ZürichFront Conference in ZürichApr 18, 2025 am 11:03 AM

I'm so excited to be heading to Zürich, Switzerland for Front Conference (Love that name and URL!). I've never been to Switzerland before, so I'm excited

Building a Full-Stack Serverless Application with Cloudflare WorkersBuilding a Full-Stack Serverless Application with Cloudflare WorkersApr 18, 2025 am 10:58 AM

One of my favorite developments in software development has been the advent of serverless. As a developer who has a tendency to get bogged down in the details

Creating Dynamic Routes in a Nuxt ApplicationCreating Dynamic Routes in a Nuxt ApplicationApr 18, 2025 am 10:53 AM

In this post, we’ll be using an ecommerce store demo I built and deployed to Netlify to show how we can make dynamic routes for incoming data. It’s a fairly

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft