html - 在站点中使用内联 CSS 有多违法?

标签 html css conventions

<分区>


想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它.

关闭 9 年前

<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/

上一篇:css - 如何在 CSS 文件中保留 LESS 变量?

下一篇:html - DIV 高度不是 100%

相关文章:

java - 3 层架构 - 从较低层通知 gui

php - 如何让我的 PHP 代码打印到单独的 div?

javascript - 在 onclick 函数中传递 id

css - 框内居中 2 行文本

java - 正确的 Java 类结构

haskell - 良好的 Haskell 编码标准

CSS div交替颜色

javascript - 未关闭的 Accordion 菜单

jquery - 使用 jquery 动画时 IE 中的样式问题

javascript - 悬停后暂停视频