面试取经:css优先级之内联宽度覆盖

162 阅读1分钟

在不改变当前代码的情况下,使这张图片的宽度为300px

<img src = '1.jpg' style = 'width:480px!important'/>

1.css方法

<img src = '1.jpg' style = 'width:480px!important; max-width:300px'>
<img src = '1.jpg' style = 'width:480px!important;transform:scale(0.625,1) '>
<img src = '1.jpg' style = 'width:480px!important; width:300px!important'>

2.js方法

document.getElementsByTagName('img')[0].setAttribute('style','width:300px!important;')