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

PHP中文网
Release: 2016-05-16 12:06:06
Original
1376 people have browsed it

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)!


Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template