最小宽度和最大宽度不适用于图像

min-width and max-width not working on image

详细信息:我正在使用 bootstrap,试图保持我的图片宽高比并根据屏幕尺寸使其响应。

问题:我已将图片的宽度设置为 18%,高度为自动,因此图片会根据屏幕宽度调整大小,但是当屏幕宽度较小时,图片变得太小太小,如果我尝试增加宽度 %,那么在正常屏幕尺寸上图片会变得太大。所以我尝试在图片上设置最大宽度和最小宽度,显然对图片没有任何影响!

html代码:

<div id="aboutcard" class="card" style="background-color:white;">
  <h1 class="cardheads">About</h1>
  <img id="mypic" class="img-responsive" src="mypicround.png" width="18%">
</div>

css代码:

#mypic
{
  margin-right : auto;
  margin-left : auto;
  min-width : 200px;
  max-width : 350px;
}
.card
{
 width : 100%;
 height : 830px;
 margin : 0 auto;
 background-color : #C00000;
}

bootstrap.css

.img-responsive,
.thumbnail > img,
.thumbnail a > img,
.carousel-inner > .item > img,
.carousel-inner > .item > a > img {
 display: block;
 max-width: 100%;
 height: auto; 
 }

如有任何帮助,我们将不胜感激!

尝试将 !important 添加到此规则:min-width: 200px !important; max-width: 320px !important;.

您可以通过提供 width:auto

来尝试
#mypic
{
  margin-right : auto;
  margin-left : auto;
  min-width : 200px;
  max-width : 350px;
  width:auto;
}

另一个替代 !important 的选择是删除 <img> 元素本身的 width 并将其移动到 CSS.

例如(我添加了一个工作图像,您可以调整 window 的大小进行测试):

#mypic {
  width: 18%;
  height: auto;
  margin-right : auto;
  margin-left : auto;
  min-width : 200px;
  max-width : 350px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div>
<img id="mypic" class="img-responsive" src="https://res.cloudinary.com/timolawl/image/upload/v1457416170/Paul-Walker-6.jpg" />
  </div>

使用

 .card img.img-responsive#mypic{
   margin-right : auto;
   margin-left : auto;
   min-width : 200px;
   max-width : 350px;
}