i have an bootstrap carousel on my page. The images I use have all different heights. I want that all images have the same height. For that I used
.carousel{ max-height: 1000px; overflow: hidden; }
I know it's not perfect but it works on desktop view. But in mobile view it didn't work and the pictures have not the same height.
So how can I make it that the images have all the same height in mobile view?
I hope someone can help me :)
Thanks.
That's my carousel-code:
<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">
<div class="item active">
<img src="./img/index/seo.png" alt="Los Angeles" style="width:100%;">
</div>
<div class="item">
<img src="./img/index/security.jpg" alt="Chicago" style="width:100%;">
</div>
<div class="item">
<img src="./img/index/sport.jpg" alt="New york" style="width:100%;">
</div>
<div class="item">
<img src="./img/index/service.png" alt="New york" style="width:100%;">
</div>
</div>
<!-- Left and right controls -->
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="sr-only">Next</span>
</a>
</div>
I would suggest you to use height not max-height
Owl carousel is responsive so it adds width:100% to each image whatever the screen-width is. and probably your images won't get the 1000px height. so it would be better if you add this code.
#media only screen and (max-width: 576px) {
.carousel .carousel-inner .item{
height: 100px // feel free to add the height you want to use but it's going to change the ratio of your image
}
}
If images are not the same height I prefer if you add them to the div of class item as a background-image css property
Related
I was trying to recreate a navbar/header/slider like this https://matchthemes.com/demohtml/dina/index.html
However, I have an issue with my carousel slider image. It looks fine when it is in webpage form, but when I shrink it to a mobile form, my image does not resize to the same as my carousel slider. Below is an image of what I mean.
mobile form
website form
This is my HTML for the carousel slider
<section class="container-fluid carouselcontainer">
<section 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>
</ol>
<!-- Wrapper for slides -->
<section class="carousel-inner">
<section class="item active">
<img src="image/restaurant.jpg" alt="Los Angeles" style="width:100%; height:100%;">
</section>
<section class="item">
<img src="https://www.w3schools.com/bootstrap/chicago.jpg" alt="Chicago" style="width:100%; height:100%;">
</section>
<section class="item">
<img src="https://www.w3schools.com/bootstrap/ny.jpg" alt="New york" style="width:100%; height:100%;">
</section>
</section>
<!-- Left and right controls -->
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="sr-only">Next</span>
</a>
</section>
</section>
This is my CSS. It's only 1 line. It is the only way I know to resize the carousel and the image
.carousel-inner {
width:100%;
height:830px;
}
You can use image in background with "background-image:url('..');" like
<section class="item active" style="background-image:url('https://www.w3schools.com/bootstrap/chicago.jpg');">
</section>
You need to some CSS in item class to adjust images what you want.
Example
.item{
width: 100%;
height: 100%;
background-repeat: no-repeat ;
background-size: cover ;
background-position: center ;
}
How about letting a browser pic the best image?
img {
max-width: 100%;
}
and read the following link
https://github.com/mdn/learning-area/blob/master/html/multimedia-and-embedding/responsive-images/responsive.html
and use "img srcset"
This is Your Solution
/* Make the image fully responsive */
.carousel-inner img {
width: 100%;
height: 100%;
}
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<div id="demo" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ul class="carousel-indicators">
<li data-target="#demo" data-slide-to="0" class="active"></li>
<li data-target="#demo" data-slide-to="1"></li>
<li data-target="#demo" data-slide-to="2"></li>
</ul>
<!-- The slideshow -->
<div class="carousel-inner">
<div class="carousel-item active">
<img src="https://www.w3schools.com/bootstrap/la.jpg" alt="Los Angeles" width="1100" height="500">
</div>
<div class="carousel-item">
<img src="https://www.w3schools.com/bootstrap/chicago.jpg" alt="Chicago" width="1100" height="500">
</div>
<div class="carousel-item">
<img src="https://www.w3schools.com/bootstrap/ny.jpg" alt="New York" width="1100" height="500">
</div>
</div>
<!-- Left and right controls -->
<a class="carousel-control-prev" href="#demo" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a>
<a class="carousel-control-next" href="#demo" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
</div>
You Can Edit or Preview Code Here Also Code Link
Try to make the width of your carousel image 100%
.carousel-inner .item img {
width: 100%;
}
How to change from this
To this?
I want to shorten the width of the image in this carousel, but not the actual red box. How can i do it? I'm at my wits end. I tried every solution on this forum, it doesn't shrink it the way i show it in the picture.
<div class="container box">
<div class="row">
<!-- carousel -->
<div class="col-xs-8">
<div id="carousel-example-generic" class="carousel slide"
data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0"
class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item active">
<img
src=""
alt="..." />
</div>
<div class="item">
<img src="" alt="..." />
</div>
<div class="item">
<img src="" alt="..." />
</div>
...
</div>
<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-generic"
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="#carousel-example-generic"
role="button" data-slide="next"> <span
class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
<div class="col-xs-4"></div>
</div>
</div>
try:
.carousel-inner .item img {
width: 100%; /* or width: 100% !important; */
}
Try this:
.carousel-inner > .item > img {
width:100%; /* Or try to use important */
height:auto;
}
A second way to do this:
.carousel {
width:100%; /* Or try to use important */
height:auto;
}
If don't work let me know so I can help
You can use the following to resize an image in Bootstrap Carousel:
.carousel-inner .item img {
height: 100%;
width: 100% !important;
}
I had a similar problem and I solved it through some inline styling.
<div class="carousel-item">
<img
src="images/image.png"
alt="testImg"
style="height: 537px"
/>
</div>
I just used Google Chrome inspect elements to get the height of an image I was happy with and styled the problem images, as I only had some images in my carousel that didn't work properly.
-Note- styling the images like this did also adjust the positions of the indicators correctly in my testing so this shouldn't be a problem with changing the Width as well as the Height if you need.
I did try the other solutions posted here but none seemed to work. So hopefully this will help someone out.
I am a beginner and trying to implement Twitter Bootstrap Carousal, and I have two requirements.
Carousal/Image should be of the Height of the screen/browser (No Scrolling): No matter what the screen size is.
Text in the Carousal should be in the center irrespective of the screen size.
Below is standard Carousal for Bootstrap Carousal by Bootstrap:
<div class="container-fluid">
<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>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner">
<div class="item active">
<img src="~/Content/Images/15.jpeg" alt="Los Angeles" style="width:100%;">
<div class="carousel-caption">
<h1>Los Angeles</h1>
<p>LA is always so much fun!</p>
</div>
</div>
<div class="item">
<img src="~/Content/Images/20.jpeg" alt="Chicago" style="width:100%;">
<div class="carousel-caption">
<h3>Chicago</h3>
<p>Thank you, Chicago!</p>
</div>
</div>
<div class="item">
<img src="~/Content/Images/10.jpeg" alt="New York" style="width:100%;">
<div class="carousel-caption">
<h3>New York</h3>
<p>We love the Big Apple!</p>
</div>
</div>
</div>
<!-- Left and right controls -->
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
Issue with above code:
Text is shown at the bottom (Need to scroll for that).
To see the entire Image/Carousal user has to scroll. Not sure why this happens
Attempt made by me:
I set the height is set to 515 px;
Added CSS below:
.item{
position: relative;
}
.carousel-caption
{
top:35%;
position: absolute;
}
<div class="item active" style="height:515px">
<img src="~/Content/Images/15.jpeg" alt="Los Angeles" style="width:100%;">
<div class="carousel-caption">
<h1>Los Angeles</h1>
<p>LA is always so much fun!</p>
</div>
</div>
Output:
So far so Good
But when I decrease the size of browser this is what I get:
Expected Behaviour:
I have a bootstrap carousel which takes full width and height of the screen.
However when the screen-size is less than 600px,it looks like
clearly there is a gap at bottom,which i do not want.
at small size(<600px) i want the image to be like :
So that there is no bottom margin and only left part of the image is displayed.
Can it be achieved with css .My code is here. Thanks in Advance.
Relevant css and html are as follows:
<header>
<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 src="http://cdn.wallpapersafari.com/65/29/U5VN6e.jpg" alt="Chania" width="100%" height="100%">
</div>
<div class="item">
<img src="http://cdn.wallpapersafari.com/65/29/U5VN6e.jpg" alt="Chania" width="100%" height="100%">
</div>
<div class="item">
<img src="http://cdn.wallpapersafari.com/65/29/U5VN6e.jpg" alt="Flower" width="100%" height="100%">
</div>
<div class="item">
<img src="http://cdn.wallpapersafari.com/65/29/U5VN6e.jpg" alt="Flower" width="100%" height="100%">
</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>
</div>
css:
html
{height:100%;
margin:0;
overflow:hidden;
}
body
{ margin:0;
height:100%;
}
header{
height:100%;
}
You can try with background images instead of image tags (there is a problem with img tag and aspect ratio):
CSS:
.image {
background-image: url(http://cdn.wallpapersafari.com/65/29/U5VN6e.jpg);
background-size: cover;
background-repeat: no-repeat;
}
JS:
$(window).resize(function() {
$('.item > .image').css('height', $(window).height());
}).trigger('resize');
CODEPEN
I am using the bootstrap caroussel template.
There is 4 images, all four in 1330*500px. By default image stretches on window resizing. I managed to keep the original image ratio with a CSS width:100% , height:auto setting for the image. Problem is carousel container won't adjust (and I see a gray gap instead)... How can I adjust carrousel height to image height within (tried to adjust carousel height in % or using padding-bottom in % too, but doesn't work) ?
Thanks.
html :
<div id="myCarousel" class="carousel slide" data-ride="carousel" data-interval="false">
<!-- 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>
<div class="carousel-inner">
<div class="item active">
<img src="#" alt="First slide">
<div class="container">
<div class="carousel-caption">
<img src="#">
</div>
</div>
</div>
css:
/* Carousel base class */
.carousel
{
height: 500px;
margin-bottom: 10px;
}
/* Since positioning the image, we need to help out the caption */
.carousel-caption
{
}
/* Declare heights because of positioning of img element */
.carousel .item
{
height: 500px;
background-color: #777;
margin-top: 0px;
}
.carousel-inner > .item > img
{
position: relative;
top: 0;
left: 0;
min-width: 100%;
height: auto;
}
By overwriting bootstrap's css and adding height:500px to the carousel class, you seem to be be breaking the slider. I created a simple carousel in bootply and left out all of your Css. And now everything works as expected and the height adjust itself correctly without any grey gap. I also added Bootstrap's container, row and column divs. I don't know if you had them setup, but I recommend you do. I used images with the same dimensions you specified.
Here is the BOOTPLY EXAMPLE. Click on the desktop and mobile icon situed on the top-right to see how it will look on different resolution.
Hope this helps
Html:
<div class="container">
<div class="row">
<div class="col-lg-12">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
<li data-target="#carousel-example-generic" data-slide-to="3"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner">
<div class="item active">
<img src="images/img-1.jpg" alt="...">
<div class="carousel-caption">
...
</div>
</div>
<div class="item">
<img src="images/img-2.jpg" alt="...">
<div class="carousel-caption">
...
</div>
</div>
<div class="item">
<img src="images/img-3.jpg" alt="...">
<div class="carousel-caption">
...
</div>
</div>
<div class="item">
<img src="images/img-4.jpg" alt="...">
<div class="carousel-caption">
...
</div>
</div>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>
</div>
</div>
</div>