首页 文章

为什么内联样式不起作用?

提问于
浏览
0

我有这个代码适用于CSS:

.fancy_image img {
    width: <?=$width;?>px;
    height: <?=$height;?>px;
  }

<div class='fancy_image'>
    <a href="<?=$content;?>" rel="prettyPhoto[mixed]">
      <img class='fade' src="<?=$content;?>" alt='' />
    </a>    
</div>

这按预期工作,但当我删除.fancy_image img css样式并将img设置为:

<img class='fade' src="<?=$content;?>" width="<?=$width;?>px" height="<?=$height;?>px" alt='' />

它显示不同的结果(图像实际上是正确的大小,但div帧占据页面宽度的100% .

知道为什么会发生这种情况吗?

编辑:我想我可能刚才找到了原因 . 我有 :

.fade {
    background: url('images/hover.png');
}

对此负责(如果删除则有效) . 我会尝试解决它 . 看来这个背景图像会导致拉伸 .

2 回答

  • 0

    您需要在不使用“px”的情况下指定width和height属性中的值 .

  • 0

    这是你可以使用它的方式 .

    <img class='fade' src="<?=$content;?>" style="width:<?=$width?>px;height:<?=$height;?>px" alt='' />
    

    对于属性,您不能使用像 px 这样的单位

    <img class='fade' src="<?=$content;?>" width="<?=$width;?>px" height="<?=$height;?>px" alt='' />
    

相关问题