CSS and Sass (SCSS) Development Guidelines

高洛峰
Release: 2017-02-28 13:46:02
Original
1904 people have browsed it

This article mainly introduces the front-end coding specifications (4) - CSS and Sass (SCSS) development specifications. Friends in need can refer to

ID and class naming

ID and class (class) names should always use names that reflect the purpose and use of the element, or other common names. Instead of appearances and obscure names.

Names that are specific and reflect the purpose of the element should be preferred because these are the most understandable and least likely to change.

Common names are just alternative names for multiple elements. They are the same among their sibling elements and have no special meaning.
Distinguish them so that they have a special meaning and are usually required as "helpers".

Although the semantics of class (class) names and IDs have no practical meaning for computer parsing,
Semantic names are usually the right choice because the information they represent does not contain Performance limitations.

Not recommended

.fw-800 {
  font-weight: 800;
}
 
.red {
  color: red;
}
Copy after login

Recommended

.heavy {
  font-weight: 800;
}
 
.important {
  color: red;
}
Copy after login

Reasonable avoidance of ID

In general IDs should not be applied to styles.
ID styles cannot be reused and you can only use ID once per page.
The only valid use of IDs is to determine position within a web page or entire site.
Nonetheless, you should always consider using class instead of id unless you are only using it once.

Not recommended

#content .title {
  font-size: 2em;
}
Copy after login

Recommended

.content .title {
  font-size: 2em;
}
Copy after login

Another objection The point of using ID is that the selector containing the ID has a high weight.
A selector containing only one ID has a higher weight than a selector containing 1000 class names, which makes it strange.

// 这个选择器权重高于下面的选择器
#content .title {
  color: red;
}
 
// than this selector!
html body p.content.news-content .title.content-title.important {
  color: blue;
}
Copy after login

Avoid tag names in CSS selectors

When building selectors you should use clear, precise and informative Semantic class name. Don't use tag selectors. It's easier to maintain if you only care about your class names
, rather than your code elements.

Considering from a separation perspective, html tags/semantics should not be allocated in the presentation layer.
It may be an ordered list that needs to be changed into an unordered list, or a p that will be converted into article.
If you only use meaningful class names,
and don't use element selectors, then you only need to change your html tags without changing your CSS.

Not recommended

p.content > header.content-header > h2.title {
  font-size: 2em;
}
Copy after login

Recommended

.content > .content-header > .title {
  font-size: 2em;
}
Copy after login

Be as precise as possible

Many front-end developers do not use direct sub-selectors when writing selector chains (note: the difference between direct sub-selectors and descendant selectors).
Sometimes, this can cause painful design problems and sometimes it can be very performance-intensive.
However, in any case, this is a very bad practice.
If you are not writing a very general selector that needs to match the end of the DOM, you should always consider direct subselectors.

Consider the following DOM:

<article class="content news-content">
  <span class="title">News event</span>
  <p class="content-body">
    <p class="title content-title">
      Check this out
    </p>
 
    <p>This is a news article content</p>
 
    <p class="teaser">
      <p class="title">Buy this</p>
      <p class="teaser-content">Yey!</p>
    </p>
  </p>
</article>
Copy after login

The following CSS will be applied to all three elements with the title class.
Then, to solve the different styles under the title class under the content class and the title class under the teaser class, this will require more precise selectors to rewrite their styles again.

Not recommended

.content .title {
  font-size: 2rem;
}
Copy after login

Recommended

.content > .title {
  font-size: 2rem;
}
 
.content > .content-body > .title {
  font-size: 1.5rem;
}
 
.content > .content-body > .teaser > .title {
  font-size: 1.2rem;
}
Copy after login

Abbreviation properties

CSS provides various abbreviation properties (such as font font) that should be used whenever possible, even when only one value is set.

Using abbreviated attributes is very 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;
Copy after login

Recommended

css code:

border-top: 0;
font: 100%/1.6 palatino, georgia, serif;
padding: 0 1em 2em;
Copy after login

0 and unit

Omit the unit after the "0" value. Do not use units after a 0 value unless there is a value.
Not recommended

css code:

padding-bottom: 0px;
margin: 0em;
Copy after login

Recommended

css code:

padding-bottom: 0;
margin: 0;
Copy after login

Hexadecimal notation

Use 3-character hexadecimal notation when possible.
Color values ​​are allowed to be represented this way,
The 3-character hexadecimal representation is shorter.

Always use lowercase hexadecimal numbers.

Not recommended

color: #FF33AA;
Copy after login

Recommended

color: #f3a;
Copy after login

Separators for ID and Class names

Use hyphens (dash lines) to separate words in ID and Class names. To enhance lesson understanding, do not use any characters (including none) other than hyphens (dashes) to connect words and abbreviations in the selector.

Also, as of the standard, the default attribute selector recognizes hyphens (dashes) as separators for words [attribute|=value],
so it's best to stick to hyphens as separator.

Not recommended

.demoimage {}
.error_status {}
Copy after login

Recommended

#video-id {}
.ads-sample {}
Copy after login

Hacks

避免用户代理检测以及CSS“hacks” – 首先尝试不同的方法。通过用户代理检测或特殊的CSS滤镜,变通的方法和 hacks 很容易解决样式差异。为了达到并保持一个有效的和可管理的代码库,这两种方法都应该被认为是最后的手段。换句话说,从长远来看,用户代理检测和hacks
会伤害项目,作为项目往往应该采取阻力最小的途径。也就是说,轻易允许使用用户代理检测和hacks 以后将过于频繁。

声明顺序

这是一个选择器内书写CSS属性顺序的大致轮廓。这是为了保证更好的可读性和可扫描重要。

作为最佳实践,我们应该遵循以下顺序(应该按照下表的顺序):

结构性属性:
display
position, left, top, right etc.
overflow, float, clear etc.
margin, padding
表现性属性:
background, border etc.
font, text
不推荐

.box {
  font-family: &#39;Arial&#39;, 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;
}
Copy after login

推荐

.box {
  display: block;
  position: absolute;
  left: 30%;
  right: 30%;
  overflow: hidden;
  margin: 1em;
  padding: 1em;
  background-color: #eee;
  border: 3px solid #ddd;
  font-family: &#39;Arial&#39;, sans-serif;
  font-size: 1.5rem;
  text-transform: uppercase;
}
Copy after login

声明结束

为了保证一致性和可扩展性,每个声明应该用分号结束,每个声明换行。

不推荐

css 代码:
.test {
  display: block; height: 100px
}
Copy after login

推荐

css 代码:

.test {
  display: block;
  height: 100px;
}
Copy after login

属性名结束

属性名的冒号后使用一个空格。出于一致性的原因,
属性和值(但属性和冒号之间没有空格)的之间始终使用一个空格。

不推荐

css 代码:

h3 {
  font-weight:bold;
}
Copy after login

推荐

css 代码:
h3 {
  font-weight: bold;
}
Copy after login

选择器和声明分离

每个选择器和属性声明总是使用新的一行。

不推荐

css 代码:

a:focus, a:active {
  position: relative; top: 1px;
}
Copy after login

推荐

css 代码:

h1,
h2,
h3 {
  font-weight: normal;
  line-height: 1.2;
}
Copy after login

规则分隔

规则之间始终有一个空行(双换行符)分隔。

推荐

css 代码:

html {
  background: #fff;
}
 
body {
  margin: auto;
  width: 50%;
}
Copy after login

CSS引号

属性选择器或属性值用双引号(””),而不是单引号(”)括起来。
URI值(url())不要使用引号。

不推荐

css 代码:

@import url(&#39;//cdn.com/foundation.css&#39;);
 
html {
  font-family: &#39;open sans&#39;, arial, sans-serif;
}
 
body:after {
  content: &#39;pause&#39;;
}
Copy after login

推荐

css 代码:

@import url(//cdn.com/foundation.css);
 
html {
  font-family: "open sans", arial, sans-serif;
}
 
body:after {
  content: "pause";
}
Copy after login

选择器嵌套 (SCSS)

在Sass中你可以嵌套选择器,这可以使代码变得更清洁和可读。嵌套所有的选择器,但尽量避免嵌套没有任何内容的选择器。
如果你需要指定一些子元素的样式属性,而父元素将不什么样式属性,
可以使用常规的CSS选择器链。
这将防止您的脚本看起来过于复杂。

不推荐

css 代码:

// Not a good example by not making use of nesting at all
.content {
  display: block;
}
 
.content > .news-article > .title {
  font-size: 1.2em;
}
Copy after login

不推荐

css 代码:

// 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;
    }
  }
}
Copy after login

推荐

css 代码:

// This example takes the best approach while nesting but use selector chains where possible
.content {
  display: block;
 
  > .news-article > .title {
    font-size: 1.2em;
  }
}
Copy after login

嵌套中引入 空行 (SCSS)

嵌套选择器和CSS属性之间空一行。

不推荐

css 代码:

.content {
  display: block;
  > .news-article {
    background-color: #eee;
    > .title {
      font-size: 1.2em;
    }
    > .article-footnote {
      font-size: 0.8em;
    }
  }
}
Copy after login

推荐

css 代码:

.content {
  display: block;
 
  > .news-article {
    background-color: #eee;
 
    > .title {
      font-size: 1.2em;
    }
 
    > .article-footnote {
      font-size: 0.8em;
    }
  }
}
Copy after login

上下文媒体查询(SCSS)

在Sass中,当你嵌套你的选择器时也可以使用上下文媒体查询。
在Sass中,你可以在任何给定的嵌套层次中使用媒体查询。
由此生成的CSS将被转换,这样的媒体查询将包裹选择器的形式呈现。

这技术非常方便,
有助于保持媒体查询属于的上下文。

第一种方法这可以让你先写你的手机样式,然后在任何你需要的地方
用上下文媒体查询以提供桌面样式。

不推荐

css 代码:

// 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&#39;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;
    }
  }
}
Copy after login


推荐

css 代码:

// 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;
    }
  }
}
Copy after login

嵌套顺序和父级选择器(SCSS)

当使用Sass的嵌套功能的时候,
重要的是有一个明确的嵌套顺序,
以下内容是一个SCSS块应具有的顺序。

当前选择器的样式属性
父级选择器的伪类选择器 (:first-letter, :hover, :active etc)
伪类元素 (:before and :after)
父级选择器的声明样式 (.selected, .active, .enlarged etc.)
用Sass的上下文媒体查询
子选择器作为最后的部分
The following example should illustrate how this ordering will achieve a clear structure while making use of the Sass parent selector.

Recommended

css 代码:

.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;
    }
  }
}
Copy after login


更多CSS 和 Sass (SCSS) 开发规范相关文章请关注PHP中文网!


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!