内嵌CSS格式化最佳做法 - 两个问题 [英] Inline CSS formatting best practices - Two questions

查看:106
本文介绍了内嵌CSS格式化最佳做法 - 两个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题#1 - 在HTML元素中指定内联样式时,是否需要包含尾随分号?例如...

 < div style =padding:10px;> content< / div>问题#2  - 当指定内联样式时,应在冒号分隔属性名称之后插入一个空格。属性值?

 < div style =padding:10px;> content< / div> 

vs。

 code>< div style =padding:10px;> content< / div>           



只有之间声明才需要分号。


一个声明块(在下面的文本中也称为
{} -block)开始带有左大括号({)的
和带右匹配的
brace
(})。之间必须有一个列表
,零或多个分号分隔的
(;)声明。




<资料来源: http://www.w3.org/TR/css3-syntax/#rule-sets


style属性的值必须
匹配
CSS声明内容的语法块(不包括
分隔括号)


源:http://www.w3.org/TR/css-style-attr/#syntax





但是,CSS语法允许空声明

因为你只有一个声明,所以没有什么可以分开,所以不需要分号。 strong>,这意味着您可以根据需要添加前导和尾随分号。例如,这是有效的CSS:

  .foo {;;; display:none ;;; color:black ;;; } 

,相当于:

  .foo {display:none; color:black} 






回答#2:否。


空或
由一个属性组成,后跟一个
冒号(:),后跟一个值。


资源:http://www.w3.org/TR/css3-syntax/#declaration



您可以添加空格以提高可读性,但它们没有相关性。


Question #1 - When specifying an inline style in an HTML element, is it necessary to include a trailing semi-colon? For example ...

<div style="padding:10px;">content</div>

Question #2 - When specifying an inline style should a space be inserted after the colon separating attribute name from attribute value?

<div style="padding: 10px;">content</div>

vs.

<div style="padding:10px;">content</div>

解决方案

Answer #1: No.

Semi-colons are required only between declarations.

A declaration-block (also called a {}-block in the following text) starts with a left curly brace ({) and ends with the matching right curly brace (}). In between there must be a list of zero or more semicolon-separated (;) declarations.

Source: http://www.w3.org/TR/css3-syntax/#rule-sets

The value of the style attribute must match the syntax of the contents of a CSS declaration block (excluding the delimiting braces)

Source: http://www.w3.org/TR/css-style-attr/#syntax

Since you have only one declaration, there is nothing to separate, so no semicolons are needed.

However, the CSS syntax allows for empty declarations, which means that you can add leading and trailing semicolons as you like. For instance, this is valid CSS:

.foo { ;;;display:none;;;color:black;;; }

and is equivalent to this:

.foo { display:none;color:black }


Answer #2: No.

A declaration is either empty or consists of a property, followed by a colon (:), followed by a value. Around each of these there may be whitespace.

Source: http://www.w3.org/TR/css3-syntax/#declarations

You can add spaces in order to improve readability, but they have no relevance.

这篇关于内嵌CSS格式化最佳做法 - 两个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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