img 或 css 宽度的内联宽度?
我想知道以下哪一个更好用,或者通常更好:
<img src = "foo.png" height = 32 width = 32 />
或者
<img src = "foo.png" style = "height: 32px; width: 32px;" />
I would like to know which of the following is better to use, or is generally better:
<img src = "foo.png" height = 32 width = 32 />
OR
<img src = "foo.png" style = "height: 32px; width: 32px;" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
后者更好(而且更更新)。
the latter is better (and more up2date).
如果您想保持简洁,切勿在任何 HTML 标记上放置样式属性。始终使用外部 CSS 文件。使用 ID 或使用类,因为它们就是为此目的而设计的。如果您突然需要更改 50 个 HTML 元素上存在的某些内容,那么您会诅咒有一天您决定将它们全部内联,而不是为它们全部提供一个类并在外部 css 文件中定义该类的值。因为编辑 CSS 文件中的一行比在 HTML 文件中查找所有 50 行要容易得多。
另外,请始终将属性用单引号或双引号引起来。这是最佳实践。
另外,请务必关闭标签。我真的不敢相信,在当今这个时代,有人会随口说你不需要在 HTML 中关闭标签。
你不必这样做,这是事实。但我真的认为我们都已经超越了懒惰的地步。
所以,我的建议是你使用这个:
此外,无需在 = 符号之前或之后添加空格。
If you want to keep it clean, never put a style attribute on any HTML tag. Always use an external CSS file. Use an ID or use a class, as they are intended for this. If you suddenly need to change something that's present on 50 HTML elements, you'll curse the day you decided to do them all inline instead of giving them all a class and defining the values for that class in an external css file. Because editing one line in a CSS file is a lot easier than finding all 50 of them in the HTML file.
Also, always enclose your attributes in either single or double quotation marks. This is a best-practice.
Also, always close your tags. I seriously cannot believe someone would, in this day and age, casually say you don't need to close your tags in HTML.
You don't have to, it's true. But I really thought we had all moved past that point of laziness.
So, my recommendation is you use this:
<img src="foo.png" height="32" width="32" />
Also, there is no need whatsoever to put spaces before or after you = sign.