<分区>
标签 html css conventions
<h1 style="margin: -20px 0 40px 0">Heading</h1>
对比
.intro h1 { margin: -20px 0 40px 0; }
这是我的困境,我有一个页面使用与其余页面不同的独特样式作为单个标题。我很难决定是必须在我的外部样式表中声明这些属性,还是直接将其硬编码到该元素。
我必须更改的属性只是 margin 。它只会纠正间距问题。
我看到使用以下任一方法的优缺点:
如果我在外部样式表中声明它,这可能是操纵元素外观的常规方式,但对于单个元素,我觉得它太多了。
如果我将它声明为内嵌到元素中,以后更新会很方便,而不是通过一堆 css 规则只是为了找到特定的规则声明。
我需要有人支持我,而不是凭直觉来决定。如果我必须选择一种方法,我会使用内联。
最佳答案
来自 Google 的优秀人员:
Requirements
There is a tradeoff here between requests and cacheability: including the CSS directly in the HTML avoids making an additional request to the external CSS resource, but if the CSS file is large (and doesn't change often), it may be better to keep it separate from the HTML so that it can be cached by the browser. Thus, the Inline CSS filter will only inline CSS files below a certain size threshold, which can be adjusted using the CssInlineMaxBytes directive.
It is possible for CSS files to contain small snippets of JavaScript code, at least for certain browsers. To avoid opening up cross-domain scripting vulnerabilities, the Inline CSS filter will only inline an external CSS file if it is being served from the same domain as the HTML file into which it is to be inlined.
Risks
The 'Inline CSS' filter is low to moderate risk. It should be safe for most pages, but it could potentially break scripts that walk the DOM looking for and examining or tags.
https://developers.google.com/speed/pagespeed/module/filter-css-inline
关于html - 在站点中使用内联 CSS 有多违法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19942233/