I am trying to build a carousel which acts as a background image cover for my website kind of of like revolutionary slider in wordpress but not a plugin. Iam building it just for my static website. The problem is that some my images are different height and widths so the carousel doesn't adjust properly. for example img no. 1 is 2048*3072 px 2nd is 3072*2048 px and so on.I want to make my carousel height and width to be 100% like a cover image. when i do set min-width and height to be 100% and 100vh respectively the images gets cropped from the bottom. any help? Here is my code
我正在尝试构建一个旋转木马,作为我的网站的背景图像封面类似于wordpress中的革命性滑块,但不是插件。我只为我的静态网站建立它。问题是我的一些图像的高度和宽度不同,因此旋转木马不能正确调整。例如img no。 1是2048 * 3072 px 2nd是3072 * 2048 px等等。我想让我的旋转木马高度和宽度像封面图像一样100%。当我将min-width和height分别设置为100%和100vh时,图像从底部被裁剪。任何帮助?这是我的代码
<!--carousel-->
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
<li data-target="#myCarousel" data-slide-to="3"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item active">
<img class ="fill"src="img/msp_0409_3522.jpg" alt="Chania">
</div>
<div class="item">
<img class ="fill" src="img/msp_0406_1786.jpg" alt="Chania">
</div>
<div class="item">
<img class ="fill" src="img/msp_1608_9566.jpg" alt="Flower">
</div>
<div class="item">
<img class ="fill" src="img/msp_1605_6918.jpg" alt="Flower">
</div>
</div>
<!-- Left and right controls -->
<a class="left carousel-control" href="#myCarousel" role="button" data- slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data- slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"> </span>
<span class="sr-only">Next</span>
</a>
</div>
css
CSS
html,body{
height:100%;
}
.carousel,.item,.active{
height:100%;
}
.carousel-inner{
height:100%;
}
.fill{
width:100%;
height:100%;
background-position:center
;background-size:cover;
}
3 个解决方案
#1
1
In your code it looks like .carousel-inner>.item>img
is overriding your fill class. In order to override the bootstrap property add an !important to your class.
在您的代码中,它看起来像.carousel-inner> .item> img正在覆盖您的填充类。为了覆盖bootstrap属性,在类中添加!important。
.fill {
width: 100%;
height: 100% !important;
}
You also do not need the background-position
or background-size
as they only style background images
您也不需要背景位置或背景大小,因为它们只设置背景图像的样式
Code Pen链接
#2
0
you should use object-fit property. You can try something like that:
你应该使用object-fit属性。你可以试试这样的东西:
.carousel-inner img {
object-fit: contain;
}
https://css-tricks.com/almanac/properties/o/object-fit/
https://css-tricks.com/almanac/properties/o/object-fit/
#3
0
for background image, you can use:
对于背景图片,您可以使用:
background-image: url("image.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
#1
1
In your code it looks like .carousel-inner>.item>img
is overriding your fill class. In order to override the bootstrap property add an !important to your class.
在您的代码中,它看起来像.carousel-inner> .item> img正在覆盖您的填充类。为了覆盖bootstrap属性,在类中添加!important。
.fill {
width: 100%;
height: 100% !important;
}
You also do not need the background-position
or background-size
as they only style background images
您也不需要背景位置或背景大小,因为它们只设置背景图像的样式
Code Pen链接
#2
0
you should use object-fit property. You can try something like that:
你应该使用object-fit属性。你可以试试这样的东西:
.carousel-inner img {
object-fit: contain;
}
https://css-tricks.com/almanac/properties/o/object-fit/
https://css-tricks.com/almanac/properties/o/object-fit/
#3
0
for background image, you can use:
对于背景图片,您可以使用:
background-image: url("image.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;