[英]How to make a picture responsive in a slider
I have a slider from a bootstrap. 我有一个来自引导程序的滑块。 So I added some pictures in the slider but they are not responsive, How can i make them responsive? 所以我在滑块中添加了一些图片,但它们没有响应,如何使它们响应?
<div class="item">
<div class="carousel-caption">
<p>Kledingwinkel(Dronten)</p>
</div>
<img src="switch/2.jpg" alt="robot" width="300" height="200px" class="homeImg">
</div>
I have nothing in the class "homeIMG" but the width "300px" height="200px" is also not working... the image stays te same... what am I doing wrong? 我在“ homeIMG”类中没有任何内容,但是宽度“ 300px” height =“ 200px”也不起作用...图像保持不变...我在做什么错?
First of all, height
and width
attributes of the img
tag should not contain any unit, only a number. 首先, img
标签的height
和width
属性不应包含任何单位,而只能包含数字。 But don't use them if you want something responsive, and avoid using both in general. 但是,如果您想对某些内容做出响应,请不要使用它们,并且应避免同时使用两者。
Use css attributes with % such as : 将CSS属性与%一起使用,例如:
img {
max-width: 50%;
max-height: 50%;
}
Or 要么
img {
width: 100%;
}
Also, include a JSFiddle if you want to show your problem, so it's easier to understand what you want exactly 另外,如果您想显示问题,请包括一个JSFiddle,这样可以更轻松地理解您想要的内容
将img-resonsive
类添加到image标签。
<img src="switch/2.jpg" alt="robot" class="homeImg img-responsive">
You'll want to remove the width/height declarations from your tag. 您需要从标记中删除宽度/高度声明。 Instead you can set the width of the image to be 100%. 相反,您可以将图像的宽度设置为100%。
You'll want to set the parent item to be position:relative;
您需要将父项设置为position:relative;
so the images size is contained by the slider item (note if your item class already has position:absolute
then it wont be necessary to add position:relative
) 因此图像大小包含在滑块项目中(请注意,如果您的项目类已经具有position:absolute
则无需添加position:relative
)
If you dont want your image to appear any bigger than a certain size, you can set the max-width property. 如果您不希望图像显示大于特定大小的图像,则可以设置max-width属性。
.item{
position:relative;
}
.homeImg{
width:100%;
max-width:500px; /*this can also be percentages*/
max-height:300px;
}
You can't make the image responsive while you're specifying fixed width and height. 指定固定的宽度和高度时,无法使图像响应。
Use CSS for responsive layout. 使用CSS进行响应式布局。
.homeImg { width: 100%; }
<div class="item"> <div class="carousel-caption"> <p>Kledingwinkel(Dronten)</p> </div> <img src="http://www.w3schools.com/css/trolltunga.jpg" alt="robot" class="homeImg"> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.