简体   繁体   English

是否有必要拥有有效的CSS?

[英]Is it necessary to have valid CSS?

Is it necessary to have valid CSS? 是否有必要拥有有效的CSS? I am using Twitter Bootstrap for rapid web development and after running the two main Bootstrap style sheets through the W3C CSS Validator, I noticed about 600 errors. 我正在使用Twitter Bootstrap进行快速Web开发,在通过W3C CSS Validator运行两个主要的Bootstrap样式表之后,我发现了大约600个错误。

My question is, my site looks good so why is it so important for the CSS to be valid? 我的问题是,我的网站看起来不错,为什么CSS有效这么重要?

Yes, it is absolutely necessary to have valid CSS. 是的,拥有有效的CSS是绝对必要的。 CSS is a programming language and as such, you must follow its language rules. CSS是一种编程语言,因此,您必须遵循其语言规则。 Just because browsers tend to accept invalid HTML and try to render it, it doesn't make generating ill-formatted HTML a good practice. 仅仅因为浏览器倾向于接受无效的HTML并尝试渲染它,它不会使生成格式错误的HTML成为一种好习惯。 The same is true to CSS, although - fortunately - CSS rules are quite a bit stricter than HTML rules. CSS也是如此,但幸运的是,CSS规则比HTML规则要严格得多。

Another reason is that valid CSS has guaranteed behavior - if you write a rule, you can expect that rule to behave a certain way. 另一个原因是有效的CSS保证了行为 - 如果您编写规则,您可以期望该规则以某种方式运行。 (Buggy browsers, like all versions of IE aside.) If your CSS is invalid, any behavior you get is undefined and it may break when a patch release is issued for any of the browsers that you use for testing. (Bug浏览器,与所有版本的IE一样。)如果您的CSS无效,您获得的任何行为都是未定义的,并且当您为测试使用的任何浏览器发布补丁版本时,它可能会中断。 Your CSS won't necessarily break but when you write invalid CSS, you get no guarantees of any behavior - you simply get some behavior that may seem correct to you but that may change any time. 您的CSS不一定会破坏,但是当您编写无效的CSS时,您无法保证任何行为 - 您只是得到一些看似正确的行为,但这可能会随时改变。

If you have correct CSS mixed in with incorrect CSS, browsers tend to ignore the invalid parts (just how the specification tells them to) but each browser does it slightly differently. 如果你有正确的CSS混入不正确的CSS,浏览器往往会忽略无效的部分(只是规范告诉他们),但每个浏览器的做法略有不同。 Similarly, while many people advise to use CSS hacks, I'd say not to, for the above reasons. 同样地,虽然很多人建议使用CSS黑客,但出于上述原因我不会说。

The CSS doesn't have to be valid to work. CSS无需有效工作。 Browsers simply ignore the CSS that they don't understand. 浏览器只是忽略了他们不理解的CSS。

Validating the CSS is a method to test if it follows the specification. 验证CSS是一种测试它是否遵循规范的方法。 If it does, any browser that is up to date with the specification used will understand the CSS. 如果是这样,任何与所用规范一致的浏览器都将理解CSS。

It's somewhat of a debated topic really. 这真的是一个有争议的话题。 The W3C tools are certainly good to use, but they tend to not account for a lot of modern code. W3C工具当然很好用,但它们往往不会占用很多现代代码。 Naturally, it's difficult for them to not only advance standards, but also make sure the tools they offer are accountable to new and inventive code. 当然,他们很难不仅提高标准,还要确保他们提供的工具对新的和创造性的代码负责。

In order to get websites to look good in all browser and across all platforms requires people to maybe stretch outside of the norms that otherwise would be "valid". 为了使网站在所有浏览器和所有平台上看起来都很好,要求人们可能会超出规范,否则将是“有效的”。 It's tough to argue against a site that works perfect cross browser and platform even if the CSS isn't 100% spotless. 即使CSS不是100%一尘不染,也很难与一个完美的跨浏览器和平台的网站争论。 That's my two cents. 那是我的两分钱。

Your CSS doesn't need to be valid (depending on who you ask), but if it is invalid, you should have a reason for the invalidity: 您的CSS不需要有效(取决于您询问的人),但如果它无效,您应该有无效的理由:

audio,
canvas,
video {
  display: inline-block;
  *display: inline;
  *zoom: 1;
}

The validator has a parse error here because of the asterisk at the beginning of property. 验证器在这里有一个解析错误,因为属性开头有星号。 This is a obscure but recognized hack for targeting Internet Explorer. 这是一个模糊但被认可的针对Internet Explorer的黑客攻击。 Other browsers will ignore the properties that it won't recognize but IE6/7 will read properties with asterisks. 其他浏览器将忽略它无法识别的属性,但IE6 / 7将读取带星号的属性。


input:-moz-placeholder,
textarea:-moz-placeholder {
  color: #999999;
}

input:-ms-input-placeholder,
textarea:-ms-input-placeholder {
  color: #999999;
}

input::-webkit-input-placeholder,
textarea::-webkit-input-placeholder {
  color: #999999;
}

The validator error here is a result of vendor-specific pseudo-classes. 此处的验证器错误是特定于供应商的伪类的结果。 Note than unlike unrecognized properties, if a browser doesn't recognize the selector the entire rule will be ignored so the vendor placeholder extensions need to be separate rules. 请注意,与未识别的属性不同,如果浏览器无法识别选择器,则将忽略整个规则,因此供应商占位符扩展需要是单独的规则。 This happens even when using the comma operator so: 即使使用逗号运算符,也会发生这种情况:

input::-moz-placeholder,
input::-ms-input-placeholder,
input::-webkit-input-placeholder, {
  color: #999999;
}

would be ignored in all browsers unless they recognized all three vendor prefixes. 除非他们识别所有三个供应商前缀,否则将在所有浏览器中被忽略。


  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#fffbb450', endColorstr='#fff89406', GradientType=0);

This is the old-style IE extension for gradients. 这是渐变的旧式IE扩展。 It ripples and causes a number of errors in the validator even though IE follows it and other browsers will not quietly ignore it. 它涟漪并在验证器中引起许多错误,即使IE跟随它并且其他浏览器也不会悄悄地忽略它。


width: auto\9;

The \\9 is another IE hack that targets IE<=8 \\ 9是针对IE <= 8的另一个IE hack


The bottom line is that if you are doing something non-standard, make sure you know why you are doing it. 最重要的是,如果你做的是非标准的事情,请确保你知道为什么要这样做。

现在有一天,有不同数量的浏览器有不同版本的数量。有些支持很多,但有些不支持。所以当你包含样式时,总是不可能100%完美。如果你的风格没有任何问题可以正常工作。但是当它会进入不同的浏览器,如果你得到问题相关的CSS,你必须小心,否则没有问题。

yes its important, most of browsers follows the w3c standard when they load the html page. 是的,重要的是,大多数浏览器在加载html页面时都遵循w3c标准。 if your page don't have the valid css, in different browser it might appear different ways. 如果您的页面没有有效的css,则在不同的浏览器中可能会出现不同的方式。 Old internet explorers didn't followed the W3c standards which cost alot to the developers result in developer need always extra css for the IE to display page properly. 旧的Internet浏览器没有遵循W3c标准,这对开发人员的成本很高,导致开发人员需要总是额外的css,以便IE正确显示页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM