留出CSS块的最后一个分号 [英] Leaving out the last semicolon of a CSS block

查看:185
本文介绍了留出CSS块的最后一个分号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关这方面的几个问题:




  • 这是好习惯吗?


  • 对于最后一个功能,这是一样的吗?
  • 在Javascript(/ jQuery)?


我的意思是这样的东西:

  #myElement {
position:absolute;
top:0;
left:0
}


解决方案


是否是好的习惯?


分号。这是纯粹是因为在添加更多样式时很容易忽略,特别是如果你在一个团队中工作:



想象一下你开始:

  .foo {
background-color:#F00;
color:#000< - 缺少分号
}

然后有人添加了一些样式:

  .foo {
background-color:#F00;
color:#000< - 缺少分号
width:30px;
z-index:100;
}

突然,其​​他开发人员浪费时间知道为什么他们 width 声明不工作(或更糟的是,没有注意到它不工作)。


在大规模上,会产生更好的加载时间吗? strong>


最明确的是,对于每个块,你会保存几个字节。这些加起来,特别是对于大样式表。不要担心这些性能增益本身,最好使用CSS Compressor,例如 YUI Compressor 来自动删除


不,它是安全的,因为浏览器正确实现这部分规范。 CSS2规范定义了一个声明:


一个声明是空的,或者是一个属性名称,后跟一个冒号(:),后跟一个属性值。


更重要的是:


...多个声明可以组织成分号(;)分隔的组。


这意味着; 用于分隔多个声明,但不需要它们来终止它们。



Javascript?


JavaScript是一个完全不同的规格的完全不同的野兽。这个特定问题已经深入地回答了很多次堆栈溢出


Couple of questions concerning this:

  • Is it good practice?
  • Will it, on a large scale, result in better load times?
  • Can it result in browsers 'breaking'?
  • Is the same true for the last function in Javascript (/jQuery)?

What I mean is stuff like this:

#myElement {
  position: absolute;
  top: 0;
  left: 0
}

解决方案

Is it good practice?

It's not good practice to manually exclude semi-colons. This is purely because it's easy to overlook when adding more styles, especially if you're working in a team:

Imagine you start with:

.foo {
    background-color: #F00;
    color: #000             <-- missing semi-colon
}

And then someone adds some styles:

.foo {
    background-color: #F00;
    color: #000             <-- missing semi-colon
    width: 30px;
    z-index: 100;
}

Suddenly the other developer is wasting time figuring out why their width declaration isn't working (or worse yet, doesn't notice that it's not working). It's safer to leave the semi-colons in

Will it, on a large scale, result in better load times?

Most definitely, for every block, you'd save a couple bytes. These add up, especially for large style sheets. Instead of worrying about these performance gains yourself, it's better to use a CSS Compressor, such as the YUI Compressor to automatically remove the ending semi-colons for you.

Can it result in browsers 'breaking'?

No, it's safe, as browsers implement this part of the specification correctly. The CSS2 specification defines a declaration thusly:

A declaration is either empty or consists of a property name, followed by a colon (:), followed by a property value.

More importantly:

...multiple declarations for the same selector may be organized into semicolon (;) separated groups.

This means that ; is used to separate multiple declarations, but are not needed to terminate them.

Is the same true for the last function in Javascript?

JavaScript is a whole different beast with a completely different specification. This particular question has been answered in depth many times before on Stack Overflow.

这篇关于留出CSS块的最后一个分号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆