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

Video Face Swap

Video Face Swap

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

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)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

See all articles