微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

内嵌CSS格式化最佳做法 – 两个问题

问题#1 – 在HTML元素中指定内联样式时,是否需要包含尾随分号?例如 …
<div style="padding:10px;">content</div>

问题#2 – 指定内联样式时,应在冒号将属性名称属性值分隔开之后插入空格吗?

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

对比

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

解决方法

答案#1:不。

仅在声明之间需要分号。

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.

资料来源: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)

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

因为你只有一个声明,没有什么可分开,所以不需要分号。

但是,CSS语法允许空声明,这意味着您可以根据需要添加前导和尾随分号。例如,这是有效的CSS:

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

并相当于:

.foo { display:none;color:black }

答案2:不。

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.

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

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

原文地址:https://www.jb51.cc/css/220449.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。