adjust carousel height to image height (responsive) - html

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>

Related

how to allow my carousel slider image to be responsive

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%;
}

Bootstrap carousel images not same height in mobile view

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

Image won't fill complete width of container

My image slider isn't quite the way I want it.
The image won't fill the full width of the container unless my browser window is small. When my window is larger the right side of the image has a white space instead of the image filling the whole width.
Here is my code:
<div class="bs-example">
<div id="myCarousel" class="carousel slide" data-interval="6500" data-ride="carousel">
<!-- 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>
<div class="fill">
<div class="carousel-inner">
<div class="active item carousel-fade">
<img src="EvanHeathYellowGreen.jpg" alt="Evan&MEKiss" >
</div>
<div class="item carousel-fade">
<img src="EvanCrossStreet.jpg" alt="Evan&MEKiss" >
</div>
<div class="item carousel-fade">
<img src="EvanMeLook.jpg" alt="Evan&MEKiss" >
</div>
</div>
</div>
<!-- Carousel nav -->
<a class="carousel-control left" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="carousel-control right" href="#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>
</div>
and the CSS:
.fill {
overflow: hidden;
background-size: cover;
background-position: center;
background-image:"path/to/image.jpg";
}
.carousel-inner{
margin-left: auto;
}
.carousel{
margin-top: 20px;
border-top: 2px solid black;
border-bottom: 2px solid black;
}
Which is the image you are trying to fill?
If they are the ones with alt="Evan&MEKiss" then you can try Alberto Rubio's answer:
.item img {
width: 100%;
}
If you are trying to fill the background-image, you can try tweaking background-image to use url:
background-image:url("path/to/image.jpg");
Here's a codepen example with placeholder images: http://codepen.io/fawyna/pen/VaOmqX

Centering button over Bootstrap 3 carousel

I'm trying to center a button over a bootstrap 3 carousel, this button is hovering above the carousel.
I tried to wrap the button in a div class and center it with either text-align: center; or margin: 0 auto; without succes.
I can manually set margin-left of the .hover-class; this will work, but it's not perfectly centered. How to center my button?
This is my code:
HTML:
<!--The slider-->
<section>
<div class="hover-slide">
Let's go
</div>
<div id="slider-top" class="carousel slide" data-ride="carousel">
<!--Indicators-->
<ol class="carousel-indicators">
<li data-target="#slider-top" data-slide-to="0" class="active"></li>
<li data-target="#slider-top" data-slide-to="1"></li>
<!--<li data-target="#slider-top" data-slide-to="2"></li>-->
</ol>
<!--Wrapper for the the slide-->
<div class="carousel-inner">
<!--specify first slide-->
<div class="item active">
<img src="img/01.jpg" alt="Foto1">
<div class="carousel-caption">
<h3>TEST</h3>
</div>
</div>
<!--specify second slide-->
<div class="item">
<img src="img/02.jpg" alt="Foto2">
<div class="carousel-caption">
<!--Let's go-->
<h3>TEST SLIDE 2</h3>
</div>
</div>
</div>
<a class="carousel-control left" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="carousel-control right" href="#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>
My CSS:
.hover-slide {
position: absolute;
/*margin-left: 30%;*/
z-index: 10;
}
.carousel .item {
width: 100%;
height: 100%;
}
.carousel .item img {
width: 100%;
height: 100%;
}
.btn.start-me {margin: 0 auto;}
You need to wrap in column and add text-center class :
Fiddle : http://bootply.com/112692
Extract :
<section>
<div class="col-md-12 hover-slide text-center"> // <------ HERE
Let's go
</div>
<div id="slider-top" class="carousel slide col-md-12" data-ride="carousel"> // <------ HERE
<ol class="carousel-indicators">
<li data-target="#slider-top" data-slide-to="0" class="active"></li>
<li data-target="#slider-top" data-slide-to="1"></li>
<!--<li data-target="#slider-top" data-slide-to="2"></li>-->
</ol>

In Twitter bootstrap, how do I stop divs from being arranged vertically when I decrease the size of my browser?

I have three carousels of span4 and I don't want them to be arranged vertically when I shrink the webpage to a tablet size. I want that to happen when I shrink the web page to phone size instead.
Because, when my browser is in tablet size the carousels are vertically placed and are stretched (which I don't want). Here is my code:
div class="container">
<div class="row-fluid">
<div class="span4 w" >
<div id="myCarousel" class="carousel slide" style="overflow: hidden">
<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>
<!-- Carousel items -->
<div class="carousel-inner">
<div class="active item">
<img class="carouselImg" src="fall.jpg"/>
</div>
<div class="item"> <img class="carouselImg" src="clock8.jpg"/></div>
<div class="item"> <img class="carouselImg" src="color.jpg"/></div>
</div>
<!-- Carousel nav -->
<a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a>
<a class="carousel-control right" href="#myCarousel" data-slide="next">›</a>
</div>
</div>
</div>
<div class="span4 w">
<div id="myCarousel1" class="carousel slide" style="overflow: hidden">
<ol class="carousel-indicators">
<li data-target="#myCarousel1" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel1" data-slide-to="1"></li>
<li data-target="#myCarousel1" data-slide-to="2"></li>
</ol>
<!-- Carousel items -->
<div class="carousel-inner">
<div class="active item"> <img class="carouselImg" src="color.jpg"/></div>
<div class="item"> <img class="carouselImg" src="dots.jpg"/></div>
<div class="item"> <img class="carouselImg" src="light.jpg"/></div>
</div>
<!-- Carousel nav -->
</div>
</div>
<div class="span4 w">
<div id="myCarousel2" class="carousel slide " style="overflow: hidden">
<ol class="carousel-indicators">
<li data-target="#myCarousel2" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel2" data-slide-to="1"></li>
<li data-target="#myCarousel2" data-slide-to="2"></li>
</ol>
<!-- Carousel items -->
<div class="carousel-inner">
<div class="active item"> <img class="carouselImg" src="ball.jpg"/></div>
<div class="item"> <img class="carouselImg" src="fall.jpg"/></div>
<div class="item"> <img class="carouselImg" src="ball.jpg"/></div>
</div>
<!-- Carousel nav -->
<a class="carousel-control left" href="#myCarousel2" data-slide="prev">‹</a>
<a class="carousel-control right" href="#myCarousel2" data-slide="next">›</a>
</div>
</div>
</div>
You could use a #media query like this in your CSS...
#media (min-width: 481px) and (max-width: 767px) {
.row-fluid .span4 {
width:31%;
float:left;
margin:4px;
}
}
This will override the Bootstrap CSS for .row-fluid span4. You may need to tweak the width/margin of your .span4's accordingly.
Demo: http://www.bootply.com/64736
Don't use row-fluid class on the containing div. just use class="row".
Using row-fluid turns on the fluid/adaptive layout that will rearrange the columns based on viewport size.
UPDATE
Have a read here http://twitter.github.io/bootstrap/scaffolding.html#gridSystem this explains how the grid system works. Are you including the responsive CSS file? If so, simply removing it should make it stop that.