当我看到网站的初始代码和示例时,CSS总是在一个单独的文件中,命名为“main.css”,“default.css”或“Site.css”。然而,当我编写一个页面时,我经常试图将CSS与DOM元素放在一起,例如在图像上设置“float: right”。我觉得这是“糟糕的编码”,因为在示例中很少这样做。

我明白,如果样式将应用于多个对象,明智的做法是遵循“不要重复自己”(Don't Repeat Yourself, DRY),并将其分配给每个元素引用的CSS类。然而,如果我不会在另一个元素上重复CSS,为什么不内联CSS,因为我写HTML?

问题是:使用内联CSS被认为是不好的,即使它只用于该元素?如果有,为什么?

例子(这样不好吗?)

<img src="myimage.gif" style="float:right" />

当前回答

Code how you like to code, but if you are passing it on to someone else it is best to use what everyone else does. There are reasons for CSS, then there are reasons for inline. I use both, because it is just easier for me. Using CSS is wonderful when you have a lot of the same repetition. However, when you have a bunch of different elements with different properties then that becomes a problem. One instance for me is when I am positioning elements on a page. Each element as a different top and left property. If I put that all in a CSS that would really annoy the mess out of me going between the html and css page. So CSS is great when you want everything to have the same font, color, hover effect, etc. But when everything has a different position adding a CSS instance for each element can really be a pain. That is just my opinion though. CSS really has great relevance in larger applications when your having to dig through code. Use Mozilla web developer plugin and it will help you find the elements IDs and Classes.

其他回答

页面内css是目前最流行的,因为谷歌认为它比从单独文件加载的css提供了更好的用户体验。一个可能的解决方案是将css放在一个文本文件中,用php动态加载它,并将它输出到文档头中。在<head>部分包括:

<head> ...

<?php
$codestring = file_get_contents("styles/style1.txt");
echo "<style>" . $codestring . "</style>";
?>

... </head>

将所需的css放在styles/style1.txt中,它将在文档的<head>部分中输出。这样,你就可以使用样式模板style1.txt,它可以被任何和所有页面共享,允许站点范围内的样式更改仅通过一个文件进行。此外,这种方法不需要浏览器从服务器请求单独的css文件(从而最小化检索/呈现时间),因为所有内容都是由php一次性交付的。

实现此功能后,可以在需要的地方手动编码单独的一次性样式。

除了其他答案....国际化。

根据内容语言的不同,通常需要调整元素的样式。

一个明显的例子就是从右向左的语言。

假设你使用你的代码:

<img src="myimage.gif" style="float:right" />

现在假设你想让你的网站支持rtl语言——你需要:

<img src="myimage.gif" style="float:left" />

现在,如果你想同时支持两种语言,没有办法用内联样式来赋值给float。

在CSS中,lang属性很容易解决这个问题

所以你可以这样做:

img {
  float:right;
}
html[lang="he"] img { /* Hebrew. or.. lang="ar" for Arabic etc */
  float:left;
}

Demo

即使你只在这个例子中使用了一次样式,你仍然混合了CONTENT和DESIGN。查找“关注点分离”。

尽管我完全同意上面给出的所有答案,在一个单独的文件中编写CSS从代码可重用性、可维护性和更好的关注点分离来看总是更好的,但在许多情况下,人们更喜欢在生产代码中使用内联CSS

The external CSS file causes one extra HTTP call to browser and thus additional latency. Instead if the CSS is inserted inline then browser can start parsing it right away. Especially over SSL HTTP calls are more costly and adds up additional latency to the page. There are many tools available that helps to generate static HTML pages (or page snippet) by inserting external CSS files as inline code. These tools are used at the Build and Release phase where the production binary is generated. This way we get all the advantages of external CSS and also the page becomes faster.

我们已经创建了一个模板驱动的工件生成器,它为任何类型的文本工件——HTML、XML、计算机语言、非结构化文本、DSV等——提供了包含文件的功能(例如,它非常适合处理普通Web或手动页面的页眉和页脚,而无需脚本)。

一旦你有了它,并使用它在你的“head”标签中提供“style”标签,“关注点分离”参数就消失了,取而代之的是“我们必须在对模板的每次更改后重新生成”和“我们必须从它生成的内容中调试模板”。自从第一种计算机语言获得预处理器(或者有人开始使用M4)以来,这些抱怨就一直存在。

总的来说,我们认为CSS文件或“style”标签的元化功能比元素级样式化更简洁,更不容易出错。但这确实需要一些专业判断,所以新手和散漫的人不必费心。