CSS code skills and Maintenance★Mozilla Hacks-the Web developer blog, hacksdeveloper
Link: https://hacks.mozilla.org/2016/05/css-coding-techniques/
Http://www.zcfy.cc/article/css-coding-techniques-x2605-mozilla-hacks-8211-the-web-developer-blog-1244.html.
Recently, I found that many people have been stumped by CSS, both novice and experienced developers. Naturally, they hope to have a better language to replace it. CSS preprocessors were born from this idea. Some people want to use the CSS framework to write less code (we have seen in the previous article why this is not a good solution ). Some people have begun to abandon CSS and use JavaScript to apply styles.
But you do not need to always use CSS preprocessors in your workflow. You do not need to use a bloated framework as the default start for each of your projects. Any use of JavaScript to do something CSS should have done is a terrible idea.
In this article, we will see some tips and suggestions for writing better CSS and CSS code that is easier to maintain. Therefore, your style sheet will become shorter with fewer rules. CSS becomes a convenient tool rather than a burden.
Select the minimum available Selector
##
CSS is a declarative language that allows you to specify styles for DOM elements. In this language, some rules take precedence over other rules, just as the intra-row style will rewrite some previous rules.
For example, if we have the following HTML and CSS code:
<button class="button-warning">
.button-warning { background: red;}button, input[type=submit] { background: gray;}
Although.button-warningInbutton, input[type=submit]Previously defined, but it will still overwrite the latter'sbackgroundAttribute. Why? What principles are used to determine which rule will rewrite another style?
Precision.
Some selectors are considered more accurate: for example,#idThe selector will override.classSelector. What happens if we apply a selector that is more precise than it actually needs? If we want to rewrite these styles later, we want to rewrite this selector. We need a more accurate one... that's right, just as a snowball rolls bigger and bigger, it will eventually become difficult to maintain.
Therefore, when you write the selector yourself, first ask yourself: Is this the most suitable selector?
All selector precision rules have been officially defined in W3C CSS specifications. Here you can find the details of each selector. If you want more simple articles to help you understand them, read this article.
Do not add more code for bugs
Let's consider this common situation: There is a bug in your CSS and you have found out which DOM element has an incorrect style. In addition, you also find that it has an attribute that is not supposed to exist.
You may want to add more CSS to it. If you do this, your code library will become larger and it will be more difficult to find bugs in the future.
As an alternative, you can look back at bugs and use your browser's development tools to view the links between elements and all layers. Determine which rule is applying the style you don't want. Modify existing rules so that they do not have unwanted results.
In FireFox, you can right-click a page element and selectCheck ElementTo debug the style sheet.
Look at that cascade in all its glory ). Here you can see all the rules applied to elements in the order they are applied. The top rules are more accurate and can be rewritten. You can see that some of the attributes of some rules have been deleted: this means that a more precise rule has been rewritten.
In addition, you can not only view these rules, but also choose whether to apply them or modify them to observe the results, which is helpful for fixing bugs.
The repair method may be a rule change, or a rule change in other locations of the layer link. This may require a new rule. At least you should know that this is the correct requirement and is also required by your code library.
This is also a good time to refactor the code. Although CSS is not a programming language, you should also consider it as JavaScript or Python: it should be clean and readable. Therefore, it should be reconstructed when necessary.
Do not use it! Important
I have already hinted at it in the previous suggestion, but due to its importance, I want to emphasize it again: Do not use it.!importantIn your code
!importantIs a feature in CSS that allows you to break the cascade rules. CSS stands for "stacked Style Sheets", which is also a prompt.
!importantIt is often used when you are worried about fixing bugs, because you do not have enough time or do not want to fix this cascade relationship.
When you give a property application!importantThe browser will ignore the precision rules. When you!importantOne rule to override the other with the same!importantYou are in serious trouble.
In fact, there is also a suitable use!importantWhen you use development tools to debug something. Sometimes, you need to find a value to fix your bug. Apply in your development tools!importantTo modify CSS rules, which helps you find the values you need without having to manage the cascade feature.
Once you know which CSS can work, you can go back to your code and check which layer of CSS you should put in the cascading relationship.
Not onlypxAnd%
Usepx(Pixels) and%(Percentages) units are intuitive, so here we will focus on those little-known units.
EmAndrem
The most famous relative unit is em. 1em is equal to the font size of that element.
Let's consider the following HTML code:
<article>
Add the following rule:
article { font-size: 1.25em;}
By default, most browsers apply a 16 PX font size to the root element (by the way, this feature is easily overwritten ). Therefore, the above article element will have a font size of 20 PX (16*1.25).
Soh1What about elements? To better understand what will happen next, let's add another CSS rule to the style sheet:
h1 { font-size: 1.25em;}
Even if it is1.25em, AndarticleElements are the same, but we must consideremCompound ). What does it mean? In other words,h1As a direct sub-element of the body, there will be a 20 PX font size (16*1.25). However, ourh1Is located in a font size different from the root element (ourarticleElement. In this case,1.25The reference is the font size given by the cascade relationship, soh1The font size of the element will be 25 PX (16 * 1.25 * 1.25).
By the way, you can useInspectorIn the panelComputedTab, which displays the actual, final pixel value.
emThe Unit is actually very practical. You can use it to dynamically change the page size (not only the font size, but also other attributes such as line spacing and width ).
If you like the feature relative to the basic size in em, but do not like its combination. You can useremUnit.remUnit andemIt is very similar, but only the size of the root element is used to remove its composite property.
Therefore, if we modify the CSSh1PartialemUnit:rem
article { font-size: 1.25em; }h1 { font-size: 1.25rem; }
Vw and VL
vwAndvhIs the unit of view.1vwIt is 1% of the port width.1vhThat is, 1% of the height of the view. When you need a UI element to occupy the entire screen (such as a traditional translucent mask layer), they are very useful because they are not always the same size as the body.
Other units
Other units may not be as common or useful as the above units, but you will encounter them one day. You can understand