css html 百分比调整图像大小

css html percentage resize image

我无法在 css/html 中调整图像大小。我可以以 px 为单位调整它的大小,但不能以 % 为单位。我已经尝试过最小高度、最小宽度。这是我的代码,我无法调整它的大小。

body{
    margin: 0;
}
#top{
    background-color: #53FF40;
    width: 100%;
    height: 50%;
}
#bottom{
    background-color: #FF5757;
    width: 100%;
    height: 50%;
}
.img-yes{
    width: 25%;
    height: 25%;
}
<!DOCTYPE html>
<html>
<head>
<title>DoYouLoveDogs</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<body>
<div id="top">
<div class="img-yes">
    <img class="img-yes" src="img/yes.png">
</div>
</div>
<div id="bottom">
</div>
<script src="js/jquery.js"></script>
<script src="js/button.js"></script>
<script>document.write('<script src="http://' + (location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=1"></' + 'script>')</script>
</body>
</html>

display: block?

.img-yes {
    display: block;
    width: 25%;
    height: 25%;
}

添加

.img-yes
{
width: 100%;
height: auto;
}

fiddle

首先,您需要为图像的外部 div 设置像素大小。然后用百分比调整图像大小。

.outerdiv{
  width:200px;
  height:200px;
}
.img-yes{
  width: 50%;
  height:30%;
}

但是如果你只想调整图片的大小,你只能以百分比的形式给出宽度,高度会根据宽度自动设置。

我只是将您的代码复制粘贴到记事本中(将内部 css 更改为内联 css 并更改图像路径)并执行它。它工作正常。当我增加宽度时,图像会根据宽度调整大小。

.img-yes {
    height: 40% !important;
    margin: 5px 28px 22px 82px;
    width: 50% !important;
}