How can I make my carousel caption responsive? - html

I have been taking a few online classes and tutorials to better understand how to create websites using html/css and bootstrap in particular.
However one problem I have been unable to overcome is - I’m unable to make a carousel caption responsive - it just will not react to the resizing of a window.
HTML:
<div id="slides" class="carousel slide" data-ride="carousel">
<ul class="carousel-indicators">
<li data-target="#slides" data-slide-to="0" class="active"></li>
<li data-target="#slides" data-slide-to="0"></li>
<li data-target="#slides" data-slide-to="0"></li>
</ul>
<div class="carousel-inner">
<div class="carousel-item active">
<img src="IMG/TEAL ORIGAMI.jpeg">
<div class="carousel-caption">
<h1 class="display-2">ICE3DESIGN</h1>
<h3>Bespoke Web Design</h3>
<button type="button" class="btn btn-outline-light btn-lg">VIEW PORTFOLIO</button>
<button type="button" class="btn btn-primary btn-lg">ENQUIRE NOW</button>
</div>
</div>
<div class="carousel-item">
<img src="IMG/background2.png">
</div>
<div class="carousel-item">
<img src="IMG/background3.png">
</div>
</div>
CSS:
.carousel-inner img {
width: 100%;
height: 100%;
}
.carousel-caption {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
.carousel-caption h1 {
font-size: 500%;
text-transform: uppercase;
text-shadow: 1px 1px 10px #000;
}
.carousel-caption h3 {
font-size: 200%;
font-weight: 500;
text-shadow: 1px 1px 10px #000;
padding-bottom: 1rem;
}
I expect that the caption resize accordingly and appear in the centre of the carousel image, however what actually happens is when the browser is resized the caption stays the same size and is far too large and not centered.

So far, you haven't provided a link to the live site, which makes things a bit more difficult. However, I did notice a few things for testing your code.
Consider adding a rule for to make carousel items flex containers:
.carousel-item {
display: flex;
flex-direction: column;
justify-content: center;
}
Remove translation for carousel-captions. Because the sibling image is absolutely positioned (not shown), you'll have to leave absolute positioning here as well. Maybe a background-image would make life easier, though.
.carousel-caption {
position: absolute;
/* top: 50%; */
/* transform: translateY(-50%); */
}
With these rules in place, you'll at least achieve vertical centering of the .carousel-caption. Then you could add the appropriate #media queries to deal with the font overflowing the box (because the container is absolutely positioned).
jsFiddle

Related

How to always have slide image centered?

current state of my carousel
I'm trying to code the slide carousel of my website, so no matter what image I upload for the slides, images will always be centered no matter what(currently only centers image if it is a specific resolution).
I have tried calling the class in CSS and centering the margin that way, but its still not working. Here is my code so far:
I called specific slide item class in CSS, updated margin, image still not centered.
.carousel-control.left,
.carousel-control.right {
background-image: none;
}
.carousel-control>.fa {
font-size: 40px;
position: absolute;
top: 50%;
z-index: 5;
display: inline-block;
margin-top: -20px;
}
.item {
margin: 0px auto; // center
width: 1100px;
height: 500px;
text-align: center;
}
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<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" class=""></li>
<li data-target="#carousel-example-generic" data-slide-to="2" class=""></li>
</ol>
<div class="carousel-inner">
<div class="item active">
<img src="https://thumbs.gfycat.com/ShallowRawCockatoo-size_restricted.gif" alt="First slide" text-align="center">
</div>
<div class="item">
<img src="https://thumbs.gfycat.com/ShallowRawCockatoo-size_restricted.gif" alt="Second slide" text-align="center">
</div>
<div class="item">
<img src="https://thumbs.gfycat.com/ShallowRawCockatoo-size_restricted.gif" alt="Third slide" text-align="center">
</div>
</div>
<a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
<span class="fa fa-angle-left"></span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" data-slide="next">
<span class="fa fa-angle-right"></span>
</a>
</div>
First of all, you have an invalid CSS comment, that might cause some trouble as well, the right way to comment in CSS is using /* comment */
Next, lets take a look at your attemp of centering things
.item {
margin: 0px auto; /* center */
width: 1100px;
height: 500px;
text-align: center;
}
In theory, this would work, but we need to know what we are centering, which is in this case the whole slide, not its contents (which you probably want to center)
So the quickest solutions here might be the following
.item img {
margin: 0 auto;
}
This is the same way as you tried, but applied on the correct element that needs to be centered -> the content of your slide.
More 'robust' way of centering would be setting your slide to display: flex and using flexbox to center your stuff.
Try it this way
.item {
width: 1100px;
height: 500px;
display: flex;
justify-content: center; /* horizontal centering */
align-items: center; /* vertical centering */
}
Try those out and let me know!

How to fit fit carousel images? (HTML/CSS/Bootstrap4)

I'm new in HTML, CSS, and Bootstrap and I'm trying to create a carousel for my page.
The carousel is working, but the images are not fitting to the page. The size is too big (height and width) and I'm trying to fit but it's not working properly.
My codes are:
.carousel-inner img{
width:100%;
height: 40%;
}
.carousel-caption {
position: absolute;
top: 60%;
transform: translateY(-60%);
}
.carousel-caption h1{
font-size:470%;
text-transform: uppercase;
text-shadow:3px 3px 15px #000;
}
.carousel-caption h3{
color: #ffc107;
font-size: 200%;
font-weight: 500;
text-shadow: 3px 3px 10px #000;
padding-botton: 1rem;
}
<!-- Image slider -->
<div id="slides" class="carousel slide" data-ride="carousel">
<ul class= "carousel-indicators">
<li data-target= "#slides" data-slide-to= "0" class="active"></li>
<li data-target= "#slides" data-slide-to= "1"></li>
<li data-target= "#slides" data-slide-to= "2"></li>
</ul>
<div class="carousel-inner">
<div class="carousel-item active">
<img src="/home/susy/Desktop/GIT/vegan-/imagens/feijuca6.jpg">
<div class="carousel-caption">
<h1 class="display-2"> Have you ever tried?</h1>
<h3> Discover the authentic flavour</h3>
<br>
<button type="button" class= "btn btn-outline-light btn-lg"> Click Here</button>
</div>
</div>
<div class="carousel-item">
<img src="/home/susy/Desktop/GIT/vegan-/imagens/pudim.jpg">
</div>
<div class="carousel-item">
<img src="/home/susy/Desktop/GIT/vegan-/imagens/meal.jpg">
</div>
</div>
The first is the CSS (I'm using CSS 3) and the second is the HTML. I'm also using Bootstrap 4.
Does anyone know what am I missing or doing wrong? Or how can I change the sizes to fit the carousel? Please.
As per my knowledge use
.carousel-inner img{
max-width:100%;
height: auto;
display: block;
}
instead of using
.carousel-inner img{
width:100%;
height: 40%;
}
if you want absolute height you can specify height : 300px
.carousel-inner img{
max-width:100%;
height: 300px;
object-fit: cover;
object-position: center;
}
What I think you are searching for is adding class="d-block w-100" in your image tag.

adding margin-left creates horizontal scroll bar to white space

I'm writing the CSS for my site. I have text that I am putting on top of my background image. My HTML and CSS is below:
HTML
<header class="site-header">
<div class="site-header__menu-icon">
<div class="site-header__menu-icon__middle"></div>
</div>
<div class="site-header__menu-content">
<div class="site-header__btn-container">
Request A Demo
</div>
<nav class="primary-nav primary-nav--pull-right">
<ul>
<li>Home</li>
<li>Services</li>
<li>Why Us</li>
</ul>
</nav>
</div>
</div>
</header>
<div class="section">
<picture>
<img src="assets\images\pepper.jpg">
</picture>
<div>
<div class="section__text-content">
<h1 class="section__title">Company</h1>
<h3 class="section__sub-title">Company Slogan</h3>
<div class="btn-container">
<a class="btn" href="#">Talk To A Specialist</a>
<a class ="btn btn__white btn__pepper-white" href="#">Get A Quote</a>
</div>
</div>
</div>
</div>
CSS
.section {
position: relative;
max-width: 100%;
&__text-content {
position: absolute;
top: 30%;
width: 100%;
margin-left:
}
&__title {
font-size: 7rem;
font-weight: 300;
color: #ffffff;
margin-bottom: 0;
}
&__sub-title {
font-size: 2.5rem;
font-weight: 300;
margin-top: 3%;
margin-bottom: 2%;
color: #ffffff;
}
}
The problem arises when I try to add a margin-left to the .section class in my CSS, because then a blank which space to the right of my screen appears with a horizontal scroll bar in direct proportion to the amount of margin I specified to move to the left.
I know that I could use a simple "background-image" for my css, but I'd prefer to do it this way for responsive imaging (it's how I learned to do it and I'm on a bit of a time crunch).
Any suggestions?
FIXED
My problem was I was writing my margin in relative terms by doing:
.section__text-content {
position: absolute;
margin-left: 10%
}
Which is wrong, because the &__text-content is set to position: absolute
Percentage is relative position, with an absolutely positioned image you need absolute margins (i.e. pixels)

Center Bootstrap Carousel images when window is resized

I have seen a lot of similar questions, but the solutions did not apply.
I am trying to make a Bootstrap carousel such that when the window is resized, the image remains centered while also maintaining the required image height and minimum 100% width properties.
This is a little difficult to explain, but you can see a similar concept on http://www.lyft.com
I have replicated a similar issue on codepen which you can see, that if you shrink the window down to the size of a mobile device the image remains fixed and ultimately you're looking at the top left corner of the image (usually clouds or some unrecognizable blur).
I'm trying to have this image move left and remain centered as the horizontal view shrinks so that the image remains relevant.
My additional carousel styles:
/* CUSTOMIZE THE CAROUSEL
-------------------------------------------------- */
/* Carousel base class */
.carousel {
height: 500px;
margin-bottom: 60px;
overflow: hidden;
}
/* 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: relative;
top: 0;
left: 0;
min-width: 100%;
max-width: none;
height: auto;
}
.carousel-inner img{
margin: auto;
}
#mycarousel{
position: relative;
top: 0;
}
And the simple 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>
<li data-target="#mycarousel" data-slide-to="4"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item active">
<img src="https://unsplash.it/2000/1250?image=397" data-color="lightblue" alt="First Image">
</div>
<div class="item">
<img src="https://unsplash.it/2000/1250?image=689" data-color="firebrick" alt="Second Image">
</div>
<div class="item">
<img src="https://unsplash.it/2000/1250?image=675" data-color="violet" alt="Third Image">
</div>
<div class="item">
<img src="https://unsplash.it/2000/1250?image=658" data-color="lightgreen" alt="Fourth Image">
</div>
<div class="item">
<img src="https://unsplash.it/2000/1250?image=638" data-color="tomato" alt="Fifth Image">
</div>
</div>
</div>
I made a solution based on the lyft site you linked in your post. I turned your img into div and used a background image then styled appropriately.
<div class="carousel-inner" role="listbox">
<div class="item active">
<div class="item-child" style="background-image: url('https://unsplash.it/2000/1250?image=397');" data-color="lightblue" alt="First Image"></div>
</div>
<div class="item">
<div class="item-child" style="background-image: url('https://unsplash.it/2000/1250?image=689');" data-color="firebrick" alt="Second Image"></div>
</div>
<div class="item">
<div class="item-child" style="background-image: url('https://unsplash.it/2000/1250?image=675');" data-color="violet" alt="Third Image"></div>
</div>
<div class="item">
<div class="item-child" style="background-image: url('https://unsplash.it/2000/1250?image=658');" data-color="lightgreen" alt="Fourth Image"></div>
</div>
<div class="item">
<div class="item-child" style="background-image: url('https://unsplash.it/2000/1250?image=638');" data-color="tomato" alt="Fifth Image"></div>
</div>
Then removed this .carousel-inner > .item > img and added this CSS:
.carousel-inner .item .item-child {
background-position: top center;
background-size: cover;
position: absolute;
bottom:0;
top: 0;
left: 0;
right:0;
width: 100%;
height: auto;
}
You can see the js.fiddle here if you want to play around with it. Hope that helps.
There you go, Easiest fix
instead of min-width use width on images
.carousel-inner > .item > img {
position: relative;
top: 0;
left: 0;
width: 100%;
max-width: none;
height: auto;
}
also throw in a height adjustment for carousel like this
.carousel .item {
height: auto;
background-color: #777;
}
http://codepen.io/Rohithzr/pen/WwEpgp?editors=1100

twitter-bootstrap center carousel image reguardless of screen size

I have the following desires for a twitter bootstrap carousel:
When screen is bigger then carousel, I want the image centered.
When the screen is smaller than the carousel, I want to see just PART of the image. This part of the image should be centered.
At no point should the images be resized or scaled
The transitions should work.
My example below fixed problems 1-3.. but it breaks 4. I can easily fix 4 if I am willing to break 3, but that is no good.
Bootply link to example: http://bootply.com/88542
HTML:
<!-- Carousel
================================================== -->
<div 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>
<div class="carousel-inner">
<div class="item active">
<img src="http://lorempixel.com/1200/600/abstract/1" height="600px" width="1200px">
<div class="container">
<div class="carousel-caption">
<h1>Bootstrap 3 Carousel Layout</h1>
<pthis is="" an="" example="" layout="" with="" carousel="" that="" uses="" the="" bootstrap="" 3="" styles.<="" small=""><p></p>
<p><a class="btn btn-lg btn-primary" href="http://getbootstrap.com">Learn More</a>
</p></pthis></div>
</div>
</div>
<div class="item">
<img src="http://lorempixel.com/1200/600/abstract/2" height="600px" width="1200px">
<div class="container">
<div class="carousel-caption">
<h1>Changes to the Grid</h1>
<p>Bootstrap 3 still features a 12-column grid, but many of the CSS class names have completely changed.</p>
<p><a class="btn btn-large btn-primary" href="#">Learn more</a></p>
</div>
</div>
</div>
<div class="item">
<img src="http://placehold.it/1200X600" height="600px" width="1200px">
<div class="container">
<div class="carousel-caption">
<h1>Percentage-based sizing</h1>
<p>With "mobile-first" there is now only one percentage-based grid.</p>
<p><a class="btn btn-large btn-primary" href="#">Browse gallery</a></p>
</div>
</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>
<!-- /.carousel -->
CSS
/* BOOTSTRAP 3.x GLOBAL STYLES
-------------------------------------------------- */
body {
padding-bottom: 40px;
color: #5a5a5a;
}
/* CUSTOMIZE THE CAROUSEL
-------------------------------------------------- */
/* Carousel base class */
.carousel {
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: 600px;
width: 1200px;
background-color:#bbb;
left: 50%;
margin-left: -600px;
}
.carousel img {
position: absolute;
top: 0;
}
/* RESPONSIVE CSS
-------------------------------------------------- */
#media (min-width: 768px) {
/* Remve the edge padding needed for mobile */
.marketing {
padding-left: 0;
padding-right: 0;
}
/* Navbar positioning foo */
.navbar-wrapper {
margin-top: 20px;
margin-bottom: -90px; /* Negative margin to pull up carousel. 90px is roughly margins and height of navbar. */
}
/* The navbar becomes detached from the top, so we round the corners */
.navbar-wrapper .navbar {
border-radius: 4px;
}
/* Bump up size of carousel content */
.carousel-caption p {
margin-bottom: 20px;
font-size: 21px;
line-height: 1.4;
}
}
This might help if you want the image to be re-sized according to the screen size of the device. The width = "100%" will do the job.
<img src="http://lorempixel.com/1200/600/abstract/2" width = "100%">
just add this to your css:
.carousel-inner > .item > img, .carousel-inner > .item > a > img {
width: 100%;
}
add this
.carousel .item img {
left: -9999px; /*important */
right: -9999px; /*important */
margin: 0 auto; /*important */
max-width: none; /*important */
min-width: 100%;
position: absolute;
}
And of course one of the parent divs needs to have a position:relative, but I believe it does by default.
See it here: http://paginacrestinului.ro/