CSS and Sass (SCSS) style rules
ID and class naming
ID and class (class) names always use names that reflect the purpose and purpose of elements, or other generic names. Instead of appearances and obscure names.
Names that reflect the purpose of the element should be preferred because they are the most understandable and least likely to change.
Common names are just alternate names for multiple elements, and their sibling elements are the same, with no special significance.
To distinguish them and make them special, they usually need to be "helpers".
Although the semanticization of class names and ID s has little practical significance for computer parsing,
Semantic names are usually the right choice because they represent information meaning and do not contain performance constraints.
Not recommended
css Code:
- .fw-800 {
- font-weight: 800;
- }
- .red {
- color: red;
- }
Recommend
css Code:
- .heavy {
- font-weight: 800;
- }
- .important {
- color: red;
- }
Reasonable Avoidance of ID Use
In general, ID s should not be applied to styles.
The ID style cannot be reused and you can only use the ID once per page.
The only valid way to use ID is to locate the page or the entire site.
Nevertheless, you should always consider using class, not id, unless you use it only once.
Not recommended
css Code:
- #content .title {
- font-size: 2em;
- }
Recommend
css Code:
- .content .title {
- font-size: 2em;
- }
Another objection to the use of ID is the high weight of ID selectors.
A selector containing only one ID selector weighs more than a selector containing 1000 class (class) names, which makes it strange.
css Code:
- // This selector weights higher than the following selector
- #content .title {
- color: red;
- }
- // than this selector!
- html body div.content.news-content .title.content-title.important {
- color: blue;
- }
Avoid label names in CSS selectors
Clear, accurate and semantic class (class) names should be used when building selectors. Do not use label selectors. If you only care about your class name
Instead of your code elements, it's easier to maintain.
From the standpoint of separation, html tags/semantics should not be allocated in the presentation layer.
It may be that an ordered list needs to be changed to an unordered list, or that a div will be converted to article.
If you only use class (class) names that have practical meaning,
And without using element selectors, you just need to change your html tags, not your CSS.
Not recommended
css Code:
- div.content > header.content-header > h2.title {
- font-size: 2em;
- }
Recommend
css Code:
- .content > .content-header > .title {
- font-size: 2em;
- }
Be as accurate as possible
Many front-end developers write selector chains without using direct sub-selectors (note: the difference between direct sub-selectors and descendant selectors).
Sometimes, this can lead to painful design problems and sometimes can be performance-intensive.
However, in any case, this is a very bad practice.
If you don't write very generic selectors that need to match to the DOM end, you should always consider direct sub-selectors.
Consider the following DOM:
css Code:
- <article class="content news-content">
- <span class="title">News event</span>
- <div class="content-body">
- <div class="title content-title">
- Check this out
- </div>
- <p>This is a news article content</p>
- <div class="teaser">
- <div class="title">Buy this</div>
- <div class="teaser-content">Yey!</div>
- </div>
- </div>
- </article>
The following CSS will apply to all three elements of the title class.
Then, to resolve the different styles under the title class under the content class and the title class under the teaser class, this will require a more precise selector to rewrite their styles again.
Not recommended
css Code:
- .content .title {
- font-size: 2rem;
- }
Recommend
css Code:
- .content > .title {
- font-size: 2rem;
- }
- .content > .content-body > .title {
- font-size: 1.5rem;
- }
- .content > .content-body > .teaser > .title {
- font-size: 1.2rem;
- }
Abbreviated attribute
CSS provides various abbreviation attributes (such as font fonts) that should be used whenever possible, even if only one value is set.
Using abbreviated attributes is useful for code efficiency and readability.
Not recommended
css Code:
- border-top-style: none;
- font-family: palatino, georgia, serif;
- font-size: 100%;
- line-height: 1.6;
- padding-bottom: 2em;
- padding-left: 1em;
- padding-right: 1em;
- padding-top: 0;
Recommend
css Code:
- border-top: 0;
- font: 100%/1.6 palatino, georgia, serif;
- padding: 0 1em 2em;
0 And unit
The unit after the value of "0" is omitted. Do not use units after zero unless they have value.
Not recommended
css Code:
- padding-bottom: 0px;
- margin: 0em;
Recommend
css Code:
- padding-bottom: 0;
- margin: 0;
Hexadecimal representation
Where possible, use a hexadecimal representation of three characters.
The color values are allowed to be expressed in this way.
The hexadecimal representation of three characters is shorter.
Always use lowercase hexadecimal digits.
Not recommended
css Code:
- color: #FF33AA;
Recommend
css Code:
- color: #f3a;
ID Separator for Class Name and Class Name
Use hyphens to separate words in ID and Class names. In order to enhance the comprehension of the lesson, do not use any characters (including none) in the selector except hyphens (underlines) to connect words and abbreviations.
In addition, as a standard, the default attribute selector recognizes hyphens (medians) as separators for words [attribute|=value].
So it's best to stick to hyphens as separators.
Not recommended
css code:
- .demoimage {}
- .error_status {}
Recommend
css Code:
- #video-id {}
- .ads-sample {}
Hacks
Avoid user agent detection and CSS "hacks" - try different approaches first. Through user agent detection or special CSS filters, flexible methods and hacks can easily solve style differences. In order to achieve and maintain an effective and manageable code base, both methods should be considered as the last resort. In other words, in the long run, user agent detection and hacks
It will hurt the project. As a project, it should take the path of least resistance. That is to say, user agent detection and hacks will be too frequent in the future.
Declaration order
This is a rough outline of the order in which CSS attributes are written in the selector. This is to ensure better readability and scanneability is important.
As best practices, we should follow the following order (in the following order):
-
Structural attributes:
- display
- position, left, top, right etc.
- overflow, float, clear etc.
- margin, padding
-
Expressive attributes:
- background, border etc.
- font, text
Not recommended
css Code:
- .box {
- font-family: 'Arial', sans-serif;
- border: 3px solid #ddd;
- left: 30%;
- position: absolute;
- text-transform: uppercase;
- background-color: #eee;
- right: 30%;
- isplay: block;
- font-size: 1.5rem;
- overflow: hidden;
- padding: 1em;
- margin: 1em;
- }
Recommend
css Code:
- .box {
- display: block;
- position: absolute;
- left: 30%;
- right: 30%;
- overflow: hidden;
- margin: 1em;
- padding: 1em;
- background-color: #eee;
- border: 3px solid #ddd;
- font-family: 'Arial', sans-serif;
- font-size: 1.5rem;
- text-transform: uppercase;
- }
Statement ends
To ensure consistency and scalability, each declaration should end with a semicolon and each declaration should be newline.
Not recommended
css Code:
- .test {
- display: block; height: 100px
- }
Recommend
css Code:
- .test {
- display: block;
- height: 100px;
- }
End of property name
Use a space after the colon of the property name. For reasons of consistency,
A space is always used between attributes and values (but there is no space between attributes and colons).
Not recommended
css Code:
- h3 {
- font-weight:bold;
- }
Recommend
css Code:
- h3 {
- font-weight: bold;
- }
Separation of selector and declaration
Each selector and attribute declaration always uses a new line.
Not recommended
css Code:
- a:focus, a:active {
- position: relative; top: 1px;
- }
Recommend
css Code:
- h1,
- h2,
- h3 {
- font-weight: normal;
- line-height: 1.2;
- }
Rule separation
There is always a blank line (double newline character) separation between rules.
Recommend
css Code:
- html {
- background: #fff;
- }
- body {
- margin: auto;
- width: 50%;
- }
CSS quotation marks
Property selectors or attribute values are enclosed in double quotes ("), rather than single quotes (").
Do not use quotation marks for URI values (url()).
Not recommended
css Code:
- @import url('//cdn.com/foundation.css');
- html {
- font-family: 'open sans', arial, sans-serif;
- }
- body:after {
- content: 'pause';
- }
Recommend
css Code:
- @import url(//cdn.com/foundation.css);
- html {
- font-family: "open sans", arial, sans-serif;
- }
- body:after {
- content: "pause";
- }
Selector nesting (SCSS)
In SAS, you can nest selectors, which can make the code cleaner and more readable. Nested all selectors, but try to avoid nesting selectors without any content.
If you need to specify style attributes for some child elements, the parent element will have no style attributes.
Conventional CSS selector chains can be used.
This will prevent your script from looking too complex.
Not recommended
css Code:
- // Not a good example by not making use of nesting at all
- .content {
- display: block;
- }
- .content > .news-article > .title {
- font-size: 1.2em;
- }
Not recommended
css Code:
- // Using nesting is better but not in all cases
- // Avoid nesting when there is no attributes and use selector chains instead
- .content {
- display: block;
- > .news-article {
- > .title {
- font-size: 1.2em;
- }
- }
- }
Recommend
css Code:
- // This example takes the best approach while nesting but use selector chains where possible
- .content {
- display: block;
- > .news-article > .title {
- font-size: 1.2em;
- }
- }
Introducing into nesting Air Line (SCSS)
An empty line between nested selectors and CSS attributes.
Not recommended
css Code:
- .content {
- display: block;
- > .news-article {
- background-color: #eee;
- > .title {
- font-size: 1.2em;
- }
- > .article-footnote {
- font-size: 0.8em;
- }
- }
- }
Recommend
css Code:
- .content {
- display: block;
- > .news-article {
- background-color: #eee;
- > .title {
- font-size: 1.2em;
- }
- > .article-footnote {
- font-size: 0.8em;
- }
- }
- }
Context Media Query (SCSS)
In SAS, you can also use context media to query when you nest your selectors.
In SAS, you can use media queries at any given nested level.
The resulting CSS will be transformed, and such media queries will be presented in the form of package selectors.
This technology is very convenient.
Helps maintain the context in which media queries belong.
The first method allows you to write your phone style first and then anywhere you need it.
Use context media queries to provide desktop style.
Not recommended
css Code:
- // This mobile first example looks like plain CSS where the whole structure of SCSS is repeated
- // on the bottom in a media query. This is error prone and makes maintenance harder as it's not so easy to relate
- // the content in the media query to the content in the upper part (mobile style)
- .content-page {
- font-size: 1.2rem;
- > .main {
- background-color: whitesmoke;
- > .latest-news {
- padding: 1rem;
- > .news-article {
- padding: 1rem;
- > .title {
- font-size: 2rem;
- }
- }
- }
- > .content {
- margin-top: 2rem;
- padding: 1rem;
- }
- }
- > .page-footer {
- margin-top: 2rem;
- font-size: 1rem;
- }
- }
- @media screen and (min-width: 641px) {
- .content-page {
- font-size: 1rem;
- > .main > .latest-news > .news-article > .title {
- font-size: 3rem;
- }
- > .page-footer {
- font-size: 0.8rem;
- }
- }
- }
Recommend
css Code:
- // This is the same example as above but here we use contextual media queries in order to put the different styles
- // for different media into the right context.
- .content-page {
- font-size: 1.2rem;
- @media screen and (min-width: 641px) {
- font-size: 1rem;
- }
- > .main {
- background-color: whitesmoke;
- > .latest-news {
- padding: 1rem;
- > .news-article {
- padding: 1rem;
- > .title {
- font-size: 2rem;
- @media screen and (min-width: 641px) {
- font-size: 3rem;
- }
- }
- }
- }
- > .content {
- margin-top: 2rem;
- padding: 1rem;
- }
- }
- > .page-footer {
- margin-top: 2rem;
- font-size: 1rem;
- @media screen and (min-width: 641px) {
- font-size: 0.8rem;
- }
- }
- }
Nested Sequence and Parent Selector (SCSS)
When using Sass's nesting function,
It is important to have a clear nesting order.
The following is the order in which an SCSS block should have.
- Style properties of the current selector
- Pseudo-class selectors for parent selectors (: first-letter,: hover,: active etc)
- Pseudo-class elements (: before and: after)
- The declaration style of the parent selector (.selected,.active,.enlarged etc.)
- Query with Sass's Context Media
- Subselector as the last part
The following example should illustrate how this ordering will achieve a clear structure while making use of the Sass parent selector.
Recommended
css Code:
- .product-teaser {
- // 1. Style attributes
- display: inline-block;
- padding: 1rem;
- background-color: whitesmoke;
- color: grey;
- // 2. Pseudo selectors with parent selector
- &:hover {
- color: black;
- }
- // 3. Pseudo elements with parent selector
- &:before {
- content: "";
- display: block;
- border-top: 1px solid grey;
- }
- &:after {
- content: "";
- display: block;
- border-top: 1px solid grey;
- }
- // 4. State classes with parent selector
- &.active {
- background-color: pink;
- color: red;
- // 4.2. Pseuso selector in state class selector
- &:hover {
- color: darkred;
- }
- }
- // 5. Contextual media queries
- @media screen and (max-width: 640px) {
- display: block;
- font-size: 2em;
- }
- // 6. Sub selectors
- > .content > .title {
- font-size: 1.2em;
- // 6.5. Contextual media queries in sub selector
- @media screen and (max-width: 640px) {
- letter-spacing: 0.2em;
- text-transform: uppercase;
- }
- }
- }