Home Web Front-end CSS Tutorial Application of CSS rule cascade Several points that must be paid attention to in CSS_Experience exchange

Application of CSS rule cascade Several points that must be paid attention to in CSS_Experience exchange

May 16, 2016 pm 12:06 PM

theoretical foundation is necessary, and practice is a way to improve and understand theory. both are equally important. any designer and developer who leaves the balance of the two, no matter which side they prefer, will make mistakes either left or right. but what is the practical use of this method i am talking about. obviously, it cannot create a rounded rectangle for us, nor can it create a column layout for us, nor can it give us a navigation bar fixed at the head of the document. the practical application of this algorithm lies in: good css programming practices. this is a macro practice that is just as important as the micro practices i just mentioned, but is much more difficult to master.

this article does not intend to include all good css programming specifications, nor is it possible. all i can provide is a summary of my personal specifications for writing css. i want these specifications to be as close as possible to the operation of css. intrinsical.

don’t use inline css
user style is not under your control
don’t use important rules
write css from low to high according to specificity
before the three points are not the focus of what i am talking about, i will just mention them one by one.

the particularity of inline css is the highest. if the attributes in your css file conflict with inline css, then the attributes in your css file will be invalid. this is different from the fact that we only debug the style in the css file. habits do not match. inline css is also ugly, it inserts styles into the html document, so it should be abandoned.

if the user sets user style to important, no matter how you write css rules, the user's important statement cannot be overridden, so trying to override all user styles is futile. happily we don't have to think about this anymore.

the important rule is an outlier. it does not conform to our usual way of thinking. no matter how we increase the specificity and order of css rules, the important rule will overwrite the rules that compete with it, which will also disrupting the customary rules of css rules brings trouble to debugging. if you want to use the important rule to hack the browser, then you should apply the rule in a selector that points to a unique element. (ps. hack should be the last one considered in the solution, because it is too ugly.)

last point, how to write css from low to high specificity?

the key is modular css.

add global css
add unified css for the current page
divide the page into several modules
use id hooks on each different module, and use class hooks on the same module
add unified css for each module
divide each module into several sub-modules, and return to step 4 to start looping until the style addition is completed.
writing good css is a design problem, not an implementation problem. we should first write global css content with very low specificity, which is our commonly used reset.css. it is the default style for all pages throughout our website.

if any page has a unique unified style, such as a page with a different background than other pages, then we can add an id to a page and then write the unified css of the current page under the id.

body#special{ 
    background-color:black; 
}
Copy after login

after the unified style is written, we divide the page into several modules. if these modules have basically the same style, then use class hooks. if the styles are not the same, then use id hooks, and you should follow this principle every time you divide into modules in the future. because class is not very specific, so if it is not a module that looks obviously similar, class should not be used. the name of the id can usually be used as the name of the module. such as head, bottom, etc. the id selector plays a key role in cascading, because id is exclusive and has high specificity, which can prevent css rules from being inadvertently overridden.

in a certain module we may have some unified css, then we need to use the id selector to write the unified style under the current module.

#head p{ 
    color:red; 
}
Copy after login

when adding a class hook, i recommend using the id of the parent module (or the page itself) as the first part of the class name. if i add an independent page (body#special), then after dividing the module into a module for the page, i should name the module of the page special_head, special_bottom, etc.

or when using class hooks in some page modules, you should use head_col etc. the advantage of this is that we don't need to use

#head .col{ 
    /* 头部中每一列的样式 */ 
}
Copy after login

but can directly use

.head_col{ 
    /* 头部中每一列的样式 */ 
}
Copy after login

so we don't have to worry about naming conflicts.

for elements with id selector added directly under the module, we can use the selector directly and add the id of the parent module before its name.

#head_navigator{ 
    /* 头部中导航栏的样式 */ 
}
Copy after login

to achieve modular css, we should try to prevent cross-module css from appearing. i think a good principle is: if the cross-module feature of a certain style is not your it's obvious at a glance, then don't use cross-module css. one exception is global css or unified css within a module. for those that are not clear at a glance and do not have a unified style, it is recommended to define them separately under each sub-module. this is just like the object-oriented design in writing java programs. we need to reduce the interdependence between modules so that the css rules of the same module are together and those that are slightly different are completely separated. this is not only easy to maintain, but also ensures write css from low to high specificity to prevent css rules from being accidentally overwritten

the above are the points that must be paid attention to when applying css with cascading css rules_experience exchange content, more for related content, please pay attention to the php chinese website (www.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

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 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)

What does placeholder mean in vue What does placeholder mean in vue May 07, 2024 am 09:57 AM

In Vue.js, the placeholder attribute specifies the placeholder text of the input element, which is displayed when the user has not entered content, provides input tips or examples, and improves form accessibility. Its usage is to set the placeholder attribute on the input element and customize the appearance using CSS. Best practices include being relevant to the input, being short and clear, avoiding default text, and considering accessibility.

What does span mean in js What does span mean in js May 06, 2024 am 11:42 AM

The span tag can add styles, attributes, or behaviors to text. It is used to: add styles, such as color and font size. Set attributes such as id, class, etc. Associated behaviors such as clicks, hovers, etc. Mark text for further processing or citation.

What does rem mean in js What does rem mean in js May 06, 2024 am 11:30 AM

REM in CSS is a relative unit relative to the font size of the root element (html). It has the following characteristics: relative to the root element font size, not affected by the parent element. When the root element's font size changes, elements using REM will adjust accordingly. Can be used with any CSS property. Advantages of using REM include: Responsiveness: Keep text readable on different devices and screen sizes. Consistency: Make sure font sizes are consistent throughout your website. Scalability: Easily change the global font size by adjusting the root element font size.

How to introduce images into vue How to introduce images into vue May 02, 2024 pm 10:48 PM

There are five ways to introduce images in Vue: through URL, require function, static file, v-bind directive and CSS background image. Dynamic images can be handled in Vue's computed properties or listeners, and bundled tools can be used to optimize image loading. Make sure the path is correct otherwise a loading error will appear.

What is node in js What is node in js May 07, 2024 pm 09:06 PM

Nodes are entities in the JavaScript DOM that represent HTML elements. They represent a specific element in the page and can be used to access and manipulate that element. Common node types include element nodes, text nodes, comment nodes, and document nodes. Through DOM methods such as getElementById(), you can access nodes and operate on them, including modifying properties, adding/removing child nodes, inserting/replacing nodes, and cloning nodes. Node traversal helps navigate within the DOM structure. Nodes are useful for dynamically creating page content, event handling, animation, and data binding.

What language is the browser plug-in written in? What language is the browser plug-in written in? May 08, 2024 pm 09:36 PM

Browser plug-ins are usually written in the following languages: Front-end languages: JavaScript, HTML, CSS Back-end languages: C++, Rust, WebAssembly Other languages: Python, Java

How to set unknown attributes in vscode vscode method to set unknown attributes How to set unknown attributes in vscode vscode method to set unknown attributes May 09, 2024 pm 02:43 PM

1. First, open the settings icon in the lower left corner and click the settings option. 2. Then, find the CSS column in the jumped window. 3. Finally, change the drop-down option in the unknownproperties menu to the error button.

What do ref and id in vue do? What do ref and id in vue do? May 02, 2024 pm 08:42 PM

In Vue.js, ref is used in JavaScript to reference a DOM element (accessible to subcomponents and the DOM element itself), while id is used to set the HTML id attribute (can be used for CSS styling, HTML markup, and JavaScript lookup).

See all articles