html - HTML 和 CSS 宽度属性有什么区别?

标签 html css

代码可以在这里找到:

http://www.w3schools.com/css/tryit.asp?filename=trycss_float_elements

<!DOCTYPE html>
<html>
<head>
<style>
.thumbnail 
{
float:left;
width:110px;
height:90px;
margin:5px;
}
</style>
</head>

<body>
<h3>Image Gallery</h3>
<p>Try resizing the window to see what happens when the images do not have enough room.</p>
<img class="thumbnail" src="klematis_small.jpg" width="107" height="90">
<img class="thumbnail" src="klematis2_small.jpg" width="107" height="80">
<img class="thumbnail" src="klematis3_small.jpg" width="116" height="90">
<img class="thumbnail" src="klematis4_small.jpg" width="120" height="90">
<img class="thumbnail" src="klematis_small.jpg" width="107" height="90">
<img class="thumbnail" src="klematis2_small.jpg" width="107" height="80">
<img class="thumbnail" src="klematis3_small.jpg" width="116" height="90">
<img class="thumbnail" src="klematis4_small.jpg" width="120" height="90">
</body>
</html>

我发现style标签内有width:110px;内也有width="107" img 标签。两者都是必需的吗?(在这种情况下,CSS 似乎覆盖了 HTML 属性)它们之间的区别有任何解释吗?

最佳答案

I checked the documents, but they don't make clear what will happen if both of them are set..

那隐藏在某处here :

The UA may choose to honor presentational attributes in an HTML source document. If so, these attributes are translated to the corresponding CSS rules with specificity equal to 0, and are treated as if they were inserted at the start of the author style sheet. They may therefore be overridden by subsequent style sheet rules. In a transition phase, this policy will make it easier for stylistic attributes to coexist with style sheets.

现在,正如我在幽默评论中暗示的那样,我不确定为什么他们会为 HTML 宽度 img 元素上的属性,并为所有这些元素的 CSS width 属性设置一个不同的值。也许他们正在考虑何时缺少 .thumbnail 类,或者其他原因。 一如既往,Alohci is in a better position to explain why the width and height attributes are specified anyway ,即使它们出于某种原因不同于 CSS 中指定的尺寸。

但是,我可以告诉您的是,这基本上意味着 CSS 确实优先,即使两者都已设置也是如此。

关于html - HTML 和 CSS 宽度属性有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20742675/

相关文章:

javascript - 当密码正确时,我的 js 没有重定向

html - Firefox 中表格单元格显示对象内的 CSS 绝对定位对象

filter - IE9滤镜渐变和border-radius冲突

css - 响应 View 中的 Bootstrap 3 面板

html - 仅列表元素全高 CSS 的内容

javascript - jQuery 在一段时间后更改文本?

javascript - 带百分比颜色划分的空心圆

jQuery - css 在更高的 Z-index 上禁用 div

html - 未应用主题样式

html - 在高层次上,我将如何为图像构建轮播?