简体   繁体   English

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

[英]min-width and max-width not working on image

Details : I'm using bootstrap, trying to maintain my pictures aspect ratio and making it responsive depending on screen size. 详细信息 :我正在使用引导程序,试图保持图片的纵横比并根据屏幕尺寸使其响应。

Problem : I have set my picture's width to 18% and height is auto so the picture does resize depending on the screen width but when the width of the screen is small, the picture becomes too small and minute, if i try to increase width % then on normal screen sizes the picture becomes too big. 问题 :我将图片的宽度设置为18%,高度是自动设置的,因此图片的确会根据屏幕的宽度进行调整,但是如果屏幕的宽度很小,则如果我尝试增加宽度%,图片就会变得太小和太细那么在正常的屏幕尺寸下,图片会变得太大。 So i tried to set max-width and min-width on the picture which apparently had no effect at all on the picture! 所以我试图在图片上设置最大宽度和最小宽度,这显然对图片完全没有影响!

html code : 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 code : 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 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; 
 }

Any help received would be appreciated! 任何收到的帮助将不胜感激!

try adding !important to this rule: min-width: 200px !important; max-width: 320px !important; 尝试在此规则中添加!importantmin-width: 200px !important; max-width: 320px !important; min-width: 200px !important; max-width: 320px !important; .

Another option instead of resorting to !important is to remove the width on the <img> element itself and move it to CSS. 除了求助于!important另一种选择是删除<img>元素本身的width并将其移动到CSS。

For example (I added a working image that you can resize the window to test): 例如(我添加了一个工作图像,您可以调整窗口大小以进行测试):

 #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> 

You can try this by giving width:auto 您可以通过给出width:auto来尝试

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

use 采用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM