Inline CSS formatting best practices - Two questions

后端 未结 4 1970
遥遥无期
遥遥无期 2020-12-08 07:28

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

4条回答
  •  既然无缘
    2020-12-08 07:37

    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.

提交回复
热议问题