Making carousel image fit for mobile device in bootstrap - html

I have created a basic website for a mates brewery, but when viewed on a mobile device, the carousel image is condensed and doesn't adjust to the size of the screen. Ive tried editing the CSS file, which in turn helped adjust the image when resizing on a PC, but didnt help with the mobile device..
the website is temporarily sitting here http://west.net16.net/
Thanks in advance
/* Carousel base class */
.carousel {
height: 100%;
margin-bottom: 60px;
}
/* Since positioning the image, we need to help out the caption */
.carousel-caption {
z-index: 10;
}
/* Declare heights because of positioning of img element */
.carousel .item {
height: 500px;
background-color: #777;
}
.carousel-inner > .item > img {
position: absolute;
top: 0;
left: 0;
min-width: 100%;
height: 100%;
}
HTML:
<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>
<div class="carousel-inner" role="listbox">
<div class="item active">
<img class="first-slide" src="IMGS/header1.gif" alt="west city brewing">
<div class="container">
<!--<div class="carousel-caption">
<h1>Example headline.</h1>
<p>Note: If you're viewing this page via a <code>file://</code> URL, the "next" and "previous" Glyphicon buttons on the left and right might not load/display properly due to web browser security rules.</p>
<p><a class="btn btn-lg btn-primary" href="#" role="button">Sign up today</a></p>
</div>-->
</div>
</div>

Your carousel is not cycling the elements. It is just a static image.
if you need a static image - try a jumbotron instead. It sizes nicely to mobile and desktop devices.
if you need to use a carousel, I suggest to modify the classic carousel html script using a background image and adjusting the size in your css file accordingly.
here you will find an html example:
<div class="container">
<!-- Header 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>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item active">
<div class="fill" style="background-image:url('https://placeimg.com/1440/810/any');"></div>
<div class="carousel-caption">
</div>
</div>
<div class="item">
<div class="fill" style="background-image:url('https://placeimg.com/1440/810/any');"></div>
<div class="carousel-caption">
</div>
</div>
<div class="item">
<div class="fill" style="background-image:url('https://placeimg.com/1440/810/any');"></div>
<div class="carousel-caption">
</div>
</div>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="icon-prev"></span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="icon-next"></span>
</a>
</div>
</div>
and your css example code:
/* CSS used here will be applied after bootstrap.css */
/* -------carousel slides----*/
.carousel-control.right, .carousel-control.left {
background-image: none;
}
.carousel-indicators {
bottom:-50px;
}
.carousel-indicators li {
border-color: #C0C0C0;
}
.carousel-indicators .active {
background-color: #c0c0c0;
}
.carousel-inner {
margin-bottom:50px;
}
.carousel .item{
height: 400px;
}
.item img {
position: absolute;
top: 0;
left: 0;
min-height: 400px;
}
.carousel .fill {
width: 100%;
height: 100%;
background-position: center;
background-size: cover;
}
bootply here
If you need you can remove the carousel indicators and its controls.

make the image a background to a div usuing url() in css file. Then use these styles.
background-repeat: no-repeat;
background-position: center 0px;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;

Related

Centering images (both vertically and horizontally) in Bootstrap 4 carousel

I am using Bootstrap 4 carousel and I am trying to center an image both vertically and horizontally. The following is my code together with the styling:
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<ul 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>
</ul>
<div class="carousel-inner">
<div class="carousel-item active">
<img src="https://placehold.it/200x200/c0392b/000">
</div>
</div>
<a class="carousel-control-prev" href="#myCarousel" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#myCarousel" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
#myCarousel {
height: 100%;
width: 100%;
}
.carousel-indicators {
margin-bottom: 8px;
}
.carousel-indicators>li {
width: 10px;
height: 10px;
border-radius: 100%;
}
.carousel-inner {
height: 100%;
background-color: #213B74;
}
.carousel-item {
text-align: center;
}
.carousel-item img {
display: block;
margin: auto;
}
For now, I have only added one carousel item for testing. The image is being centered horizontally. However, I have tried to set the display attribute for the image to block and its margin to auto but it's still not being centered vertically. Can someone tell me what am I doing wrong, please?
I have tried looking into other questions related to this, but I still didn't find the answer that I'm looking for. In case this question is a duplicate, I would gladly appreciate if you could tell me which answer should I be looking into.
Thanks
Here we go, you will need to add a <div> within the .carousel-item, and within that div your image will be placed. We are taking this mediator div to apply the flex centering effect.
If you try to apply flex classes such as d-flex on .carousel-item the carousel will not work as expected.
here I have added the <div class="parent d-flex justify-content-center"> which places the image within it to exact center of the div. Also you will have to give the div min-height equivalent to the .carousel-item OR some standard height for you carousel, so that it don't collapse if the inner image is smaller in size.
you can get to know more about flex here, also if you would like to try out with some examples go here.
#myCarousel {
height: 100%;
width: 100%;
}
.carousel-indicators {
margin-bottom: 8px;
}
.carousel-indicators>li {
width: 10px;
height: 10px;
border-radius: 100%;
}
.carousel-inner {
height: 100%;
background-color: #213B74;
}
.carousel-item {
text-align: center;
}
.parent {
min-height: 200px;
}
.carousel-item img {
display: block;
margin: auto;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<ul 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>
</ul>
<div class="carousel-inner">
<div class="carousel-item active">
<div class="parent d-flex justify-content-center">
<img src="https://placehold.it/100x200/c0392b/000">
</div>
</div>
<div class="carousel-item">
<div class="parent d-flex justify-content-center">
<img src="https://placehold.it/200x100/c0392b/000">
</div>
</div>
<div class="carousel-item">
<div class="parent d-flex justify-content-center">
<img src="https://placehold.it/150x150/c0392b/000">
</div>
</div>
</div>
<a class="carousel-control-prev" href="#myCarousel" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#myCarousel" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
I have fixed your problem using the following code for the css:
#myCarousel {
height: 100vh;
width: 100%;
}
.carousel-indicators {
margin-bottom: 8px;
}
.carousel-indicators>li {
width: 10px;
height: 10px;
border-radius: 100%;
}
.carousel-inner {
height: 100%;
background-color: #213B74;
}
.carousel-item {
height: 100%;
}
.carousel-item img {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%)
}
So first of all, I gave the carousel a height of 100vh to fill the total height of screen (You can give it a fixed height as well). I then made the carousel-item height: 100% in order to make it the full height of carousel and then applied the carousel-item img code as above in order to move the image to the center both horizontally and vertically.
You can achieve this with the align-items-center & align-content-center Bootstrap 4 flex classes. See example below :
.main-wrapper {
width: 100%;
height: 100vh;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="main-wrapper">
<div class="h-100 w-100 d-flex flex-wrap align-items-center align-content-center">
<div class="mx-auto">
Content
</div>
</div>
</div>

all image content appear in the div

I have a slide show for the image, but the image does not appear entirely.
How can I solve this? I want to show the image no matter its height.
This image shows my problem. This is part of the image that appears in my slide show
html,
body {
height: 85%;
}
.carousel,
.item,
.active {
height: 100%;
}
.carousel-inner {
height: 100%;
}
/* Background images are set within the HTML using inline CSS, not here */
.fill {
width: 100%;
height: 100%;
background-position: center;
-webkit-background-size: cover;
-moz-background-size: cover;
background-size: cover;
-o-background-size: cover;
}
<!-- Full Page Image Background Carousel Header -->
<header id="myCarousel" class="carousel slide">
<!-- 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">
<!-- Set the first background image using inline CSS below. -->
<div class="fill" style="background-image:url('img/slide1.jpg');"></div>
<div class="carousel-caption">
</div>
</div>
<div class="item">
<!-- Set the second background image using inline CSS below. -->
<div class="fill" style="background-image:url('img/slide2.jpg');"></div>
<div class="carousel-caption">
</div>
</div>
<div class="item">
<!-- Set the third background image using inline CSS below. -->
<div class="fill" style="background-image:url('img/slide3.jpg');"></div>
<div class="carousel-caption">
</div>
</div>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="icon-prev"></span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="icon-next"></span>
</a>
</header>
THANKS YOU
i solve the problem like this
width: 100%;
height:100%;
background-position:center ;
-webkit-background-size: contain;
-moz-background-size: contain;
background-size: contain;
-o-background-size:contain;
background-repeat: no-repeat; background-size: 100% 100%;background-origin: content-box;

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

How to set carousel in the middle of the screen?

I have this in carousel.css file (bootstrap), I set width of the image to 900px, but I want to have this image in the middle of the screen, how to do it? (I tried to edit left variable, but nothing happened, and I want it not fixed, something like <center></center>)
.carousel-inner > .item > img {
position: absolute;
top: 0;
left: 0;
width: 900px;
height: 500px;
}
index.html:
<div id="myCarousel" class="carousel slide" data-ride="carousel" >
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
</ol>
<div class="carousel-inner">
<div class="item active">
<img data-src="holder.js/900x500/auto/#777:#7a7a7a/text:blabla" alt="First slide">
<div class="container">
<div class="carousel-caption">
<h1>aaa</h1>
<p>xxxx</p>
</div>
</div>
</div>
</div>
<a class="left carousel-control" href="#myCarousel" data-slide="prev"><span class="glyphicon glyphicon-chevron-left"></span></a>
<a class="right carousel-control" href="#myCarousel" data-slide="next"><span class="glyphicon glyphicon-chevron-right"></span></a>
</div>
If you just want to center horizontally you can do margin: 0 auto; and get rid of your absolute positioning. If you want to center vertically and horizontally you can do
.carousel-inner{
position: relative;
}
.carousel-inner > .item > img {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
width: 900px;
height: 500px;
}
You can set the #myCarousel div's width to 900px too and margin: 0 auto.

adjust carousel height to image height (responsive)

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>