img 样式 css
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5887504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
img styling css
提问by user544079
Is it a good practice to keep adding
继续添加是一个好习惯吗
style="padding: 0; margin: 0; display: block;"
to each and every image?
每个图像?
回答by David says reinstate Monica
No, it's better practice to use external stylesheets to do the same thing:
不,最好使用外部样式表来做同样的事情:
img {
padding: 0;
margin: 0;
display: block;
}
This will target all images in which the stylesheet is loaded. This can be overridden by the use of more specific selectors, such as id
-based (img#theImageID
or, more simply, #theImageID
will target an <img id="theImageID" src="path/to/image.png" />
), or class
-based (img.imageClass
or, again more simply, .imageClass
. The former will select: <img class="imageClass" src="path/to/image.png" />
the latter will select the same element, but alsoany other element that has the same class-name).
这将针对加载样式表的所有图像。这可以通过使用更具体的选择器来覆盖,例如id
-based (img#theImageID
或者,更简单地说,#theImageID
将定位 an <img id="theImageID" src="path/to/image.png" />
),或class
-based (img.imageClass
或者,更简单地说,.imageClass
。前者将选择:<img class="imageClass" src="path/to/image.png" />
后者将选择相同的元素,但还有任何其他具有相同类名的元素)。
Editeddue to response/question from OP:
由于来自 OP 的回复/问题而编辑:
[Even] in case of html emails?
[甚至] 在 html 电子邮件的情况下?
HTML emails are the one special case for this rule, HTML emails typically don't load, or can't load, external stylesheets. And seem to have trouble with style
blocks, so in that case, yes. You still have to use in-line styles. Unfortunately.
HTML 电子邮件是此规则的一个特例,HTML 电子邮件通常不会加载或无法加载外部样式表。并且似乎对style
块有问题,所以在这种情况下,是的。您仍然必须使用内联样式。很遗憾。
Further reading:
进一步阅读:
回答by cycero
It's surely better to create a class with the style properties, like the following:
使用样式属性创建一个类肯定会更好,如下所示:
img.imageclass {
border: none;
margin: 20px;
padding: 10px;
}
Why it is not recommended to use inline styles. Because if you use an in-line style, you'll not be able to affect on that element from browser specific style-sheets. For example, if you create an element and see that it needs some "tweak" to look good in IE6, for example, the IE6 specific style-sheet will not work if you explicitly put an inline style for that element since the in-line style will be "lower" and thus will have higher priority.
为什么不推荐使用内联样式。因为如果您使用内联样式,您将无法从浏览器特定的样式表中影响该元素。例如,如果您创建一个元素并发现它需要一些“调整”才能在 IE6 中看起来不错,那么如果您显式地为该元素放置内联样式,则 IE6 特定样式表将不起作用,因为内联样式将“较低”,因此具有更高的优先级。
回答by robx
it is good practice, but like @David Thomas said, it's better to do it in an external CSS file to keep from inline clutters.
这是一个很好的做法,但就像@David Thomas 所说的那样,最好在外部 CSS 文件中执行此操作以防止内联混乱。