2016-06-21 133 views
0

当我有一个图像的属性widthheight我的CSS重写这些属性,但如果我有宽度和高度的内联样式我的外部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" />

实施例的代码,其中i n线风格:

.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" />

+0

我明白这一点。我的问题更多地针对外部CSS重写内联属性的原因。 – Anthony

+2

这很有趣。我希望这种行为,否则你永远无法调整图像的外部大小的CSS媒体查询等。有一个高度和宽度attr不会保留在浏览器中的空间,所以我想你可以让你的蛋糕也吃了。 –

+0

我的假设是它将属性写为非内联CSS,然后外部CSS出现并覆盖CSS属性选择器。 – Anthony

回答

0

占优的属性像这样的HTML标签直接使用“宽”“分区风格=” WIDTH:你把100" 是必要的后重要你的CSS代码,例如这样的事情:

.pic { 
    width: auto!important; 
    height: auto!important; 
} 
+1

感谢您的回复。我已经意识到这一点。除非绝对必要,否则也不建议这样做。我的问题更关注于:为什么外部CSS会覆盖内联属性。 – Anthony