在线CSS有什么坏? [英] What's so bad about in-line CSS?

查看:119
本文介绍了在线CSS有什么坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我看到网站启动器代码和示例时,CSS总是在一个单独的文件,命名为像main.css,default.css或Site.css。然而,当我编写一个页面时,我经常试图将CSS与DOM元素在一起,例如通过在图像上设置float:right。我感觉这是坏的编码,因为它很少在例子中做。

When I see website starter code and examples, the CSS is always in a separate file, named something like "main.css", "default.css", or "Site.css". However, when I'm coding up a page, I'm often tempted to throw the CSS in-line with a DOM element, such as by setting "float: right" on an image. I get the feeling that this is "bad coding", since it's so rarely done in examples.

我理解,如果样式将应用于多个对象,遵循不要重复自己(DRY)并将其分配给每个元素引用的CSS类。但是,如果我不会在另一个元素上重复CSS,为什么不编写CSS,因为我写的HTML?

I understand that if the style will be applied to multiple objects, it's wise to follow "Don't Repeat Yourself" (DRY) and assign it to a CSS class to be referenced by each element. However, if I won't be repeating the CSS on another element, why not in-line the CSS as I write the HTML?

问题:行CSS被认为是坏的,即使它只会在该元素上使用?如果是,为什么?

The question: Is using in-line CSS considered bad, even if it will only be used on that element? If so, why?

示例(这是不好?):

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


推荐答案

使网站看起来不同。这可能不适用于您的示例,但如果您对

Having to change 100 lines of code when you want to make the site look different. That may not apply in your example, but if you're using inline css for things like

<div style ="font-size:larger; text-align:center; font-weight:bold">

在每页上表示一个页眉,这将更容易维护为

on each page to denote a page header, it would be a lot easier to maintain as

<div class="pageheader">  

如果页眉在一个样式表中定义,那么如果你想改变页眉的样式整个网站,你改变css在一个地方。

if the pageheader is defined in a single stylesheet so that if you want to change how a page header looks across the entire site, you change the css in one place.

但是,我会是一个异端,并说,在你的例子,我没有看到问题。你的目标是一个单一的图像的行为,这可能需要在一个单一的页面上查看,所以把实际的css在一个样式表可能是过度。

However, I'll be a heretic and say that in your example, I see no problem. You're targeting the behavior of a single image, which probably has to look right on a single page, so putting the actual css in a stylesheet would probably be overkill.

这篇关于在线CSS有什么坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆