CSS覆盖宽度和高度属性。为什么呢?

时间:2016-06-21 19:17:02

标签: html css image attributes inline-styles

当我有一个包含属性widthheight的图像时,我的css会覆盖这些属性,但如果我有宽度和高度的内联样式,我的外部css不会覆盖内联样式。为什么呢?

没有添加CSS的示例代码:

<img class="pic" src="http://cdn.cutestpaw.com/wp-content/uploads/2012/07/l-Wittle-puppy-yawning.jpg" width="100" height="100" />

添加了CSS的示例代码:

.pic {
  width: auto;
  height: auto;
}
<img class="pic" src="http://cdn.cutestpaw.com/wp-content/uploads/2012/07/l-Wittle-puppy-yawning.jpg" width="100" height="100" />

带内联样式的示例代码:

.pic {
  width: auto;
  height: auto;
}
<img class="pic" src="http://cdn.cutestpaw.com/wp-content/uploads/2012/07/l-Wittle-puppy-yawning.jpg" style="width: 100px; height: 100px" />

1 个答案:

答案 0 :(得分:1)

对于覆盖直接在html标记中使用的“width”属性,这样“div style =”width:100“是必要的,你把它放在你的css代码之后很重要,例如:

.pic {
   width: auto!important;
   height: auto!important;
}
相关问题