Daily-Interview-Question icon indicating copy to clipboard operation
Daily-Interview-Question copied to clipboard

第 60 题:已知如下代码,如何修改才能让图片宽度为 300px ?注意下面代码不可修改。

Open zeroone001 opened this issue 5 years ago • 40 comments

<img src="1.jpg" style="width:480px!important;”>

zeroone001 avatar Apr 22 '19 23:04 zeroone001

max-width: 300px 不知道这样可不可以,大佬们请赐教

YouziXR avatar Apr 23 '19 00:04 YouziXR

max-width: 300px transform: scale(0.625,0.625) 暂时只想到这两种

frontEndJC avatar Apr 23 '19 00:04 frontEndJC

box-sizing: border-box;
padding: 0 90px;

这种也可以

TGuoW avatar Apr 23 '19 01:04 TGuoW

比较硬核的是再加个 width: 300px !important; 把题目里的覆盖掉

TGuoW avatar Apr 23 '19 01:04 TGuoW

padding:0 90px; box-sizing: border-box; max-width:300px

Mikerui avatar Apr 23 '19 01:04 Mikerui

来个有点局限的, 只适用于图片原始尺寸小于300的才行

<div style="display: flex;width:300px;height: 300px">
  <img src="./down.png" style="width:480px !important" />
</div>

leemotive avatar Apr 23 '19 01:04 leemotive

获取当前dom元素,更改css样式。css属性按楼上发的改都可以。 在图片外层加一个div。样式写成 div img{background-size:100%},也可以

y1324 avatar Apr 23 '19 01:04 y1324

一、

zoom: 0.625 
// 300 / 480 = 0.625

二、

transform: scale(0.625)

zhuzhh avatar Apr 23 '19 01:04 zhuzhh

1. <img src="1.jpg" style="width:480px!important; max-width = 300px">
2. <img src="1.jpg" style="width:480px!important; transform: scale(300/480)">

xbcc123 avatar Apr 23 '19 02:04 xbcc123

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

lzcdev avatar Apr 23 '19 02:04 lzcdev

max-width可以覆盖掉!important

moziming888 avatar Apr 23 '19 02:04 moziming888

max-width: 300px;

nobitas avatar Apr 23 '19 05:04 nobitas

总结一下吧: 1.css方法 max-width:300px;覆盖其样式; transform: scale(0.625);按比例缩放图片; 2.js方法 document.getElementsByTagName("img")[0].setAttribute("style","width:300px!important;")

zxcweb avatar Apr 23 '19 13:04 zxcweb

用max-width去覆盖挺好的。

fireairforce avatar Apr 25 '19 01:04 fireairforce

总结: img{ /* max-width:300px / / transform: scale(0.625); / / box-sizing:border-box; padding:90px; / / zoom:0.625; / / width:300px!important; 不可以 */

    }

除了最后的一行不可以,因为内联样式的优先级比内部样式优先级高

RuifangLong avatar Apr 25 '19 14:04 RuifangLong

1 max-width:300 2 transform:scale(0.625) 3 zoom:0625 4 ... 1\2\3 就已经足够了

nanfs avatar May 05 '19 09:05 nanfs

1.使important样式不生效,display: inline,position: absolute,left...right...,或者max-width:300px; 2.利用盒模型的padding,box-sizing: content-box; padding-left:90px; padding-right: 90px; 3.缩放

yzajoee avatar May 19 '19 17:05 yzajoee

max-width:300px; 最简单哒

zhdanny avatar Jul 11 '19 08:07 zhdanny

来个奇淫技巧

img {
      animation: test 0s forwards;
}
@keyframes test {
from {
width: 300px;
}
to {
width: 300px;
}
}

young122849 avatar Jul 13 '19 14:07 young122849

来个奇淫技巧

img {
      animation: test 0s forwards;
}
@keyframes test {
from {
width: 300px;
}
to {
width: 300px;
}
}

来个奇淫技巧

img {
      animation: test 0s forwards;
}
@keyframes test {
from {
width: 300px;
}
to {
width: 300px;
}
}

利用CSS动画的样式优先级高于!important的特性

young122849 avatar Jul 13 '19 14:07 young122849

很简单啊 img{ max-width:300px; }

catbea avatar Aug 14 '19 02:08 catbea

box-sizing: border-box

Rotten-Egg avatar Aug 18 '19 03:08 Rotten-Egg

document.querySelector('img').style.cssText='width:300px;'

为什么都没有用js的。。。

CC712 avatar Aug 28 '19 07:08 CC712

总结一下 1、给图片设置max-width:300px 2、给图片设置transform: scale(0.625,0.625),但是占据的位置还是原来的480px 3、给图片设置box-sizing: border-box;padding: 0 90px;,但图片左右会有90px的内边距 4、给图片设置zoom: 0.625 5、js获取元素使用imgs[0].setAttribute("style","width:300px!important;")或者imgs[0].style.cssText='width:300px;' 6、给图片设置动画,from{width:300px;}to{width:300px;},动画时间为0s,原理是CSS动画的样式优先级高于!important的特性

Shiryan avatar Sep 17 '19 04:09 Shiryan

来个奇淫技巧

img {
      animation: test 0s forwards;
}
@keyframes test {
from {
width: 300px;
}
to {
width: 300px;
}
}

Chrome可以,Firefox不支持

lzwdot avatar Sep 29 '19 07:09 lzwdot

外面套个盒子 然后overflow: hidden?

wugengliuli-web avatar Oct 14 '19 07:10 wugengliuli-web

同时设置max-width和min-width为300px即可

zhengwengang avatar Nov 14 '19 07:11 zhengwengang

<img src="1.jpg" style="width:480px!important;”>

yygmind avatar Dec 16 '19 03:12 yygmind

我只知道如果实际项目中如果发生了这种事,好的解决办法是把行内style改成class写法 本来行内style就已经够愚蠢了,现在你让我蠢上加蠢,我做不到

mask2012 avatar Jan 20 '20 02:01 mask2012

let img = document.getElementsByTagName('img')[0]
img.style.width = 300 + 'px'

litokele2018 avatar Mar 23 '20 11:03 litokele2018