.dom0, .dom1 {
text-align: center;
}
.dom0 {
color: red;
font-size: 12px;
}
.dom1 {
color: blue;
font-size: 14px;
}
如果少量公用的样式 是不是分别写比较好 虽然这样会造成代码的重复
.dom0 {
color: red;
font-size: 12px;
text-align: center;
}
.dom1 {
color: blue;
font-size: 14px;
text-align: center;
}
The respondent wants to ask about the difference in performance between the two?
In fact, both methods are possible. CSS is not JS. It does not mean that if you write it separately, you will select the DOM twice. CSS is more like this process:
The respondent seems to understand that writing a
.dom1
in CSS means that the HTML interpreter will find the HTML elements ofclass=dom1
and change them all to the styles in{...}
. This understanding is wrong of.So, when writing CSS, some common things can be taken out separately for easy modification and later use. For example:
CSS:
HTML:
----------2017.03.05 Added----------
Will CSS be appended twice?
Each DOM has all the CSS properties that we can define. We define styles just to change their style properties.
For example:
<p>默认样式的p标签</p>
We did not specify its color, but in fact, the
color
attribute of this p tag is#000
(black).Then the repeatedly defined style will overwrite the previous definition, for example:
CSS:
HTML:
↑ The final color of this p tag will be
#333
. Because#222
was also recorded, but he was in front and was covered.If the HTML is as follows, it will also be overwritten:
HTML:
↑ The color of this p tag will be
#444
. Because the style definition that comes with the tag has the highest priority. (He is defined behind all style sheets.)There is a way not to be overridden, that is to add
!important
. (IE6 and below are not supported)For details, please refer to http://www.w3chtml.com/css3/r...
CSS:
HTML:
↑ This is the color of the p tag is
#222
. Because!important
is added, the styles given later will not cover this one.Isn’t this your style? What is selecting dom? It should be rendering dom.
You will render it twice. Obviously the first way of writing is better.
Depending on your application scenario, in fact, text-align:center can be extracted separately like bootstrap and used in parallel with multiple classes
In this way, not only the two of them can be used, but other elements can also share this class
(o´・ェ・`o) It doesn’t matter what style you choose, after all, it will be drawn to the public after packaging~
No matter how you write CSS, the CSS parser will calculate the final style applied to this DOM element according to priority rules before rendering the DOM element
It does not apply styles from top to next
If it is a selector like yours, there is no need to use the first one. Because the extracted ones are completely useless. Cannot be reused.
You can extract reusable functions, for example:
Both writing methods are acceptable, but it is recommended to write it in a public style, so that the code is simpler and easier to manage.
Your first way of writing will indeed render the same thing twice. In addition, to achieve the same effect, the less code, the better. You should learn about sass or less.