Keep image centered in carousel when shrunk to mobile - carousel

I'm trying to keep the carousel image centred when shrunk to mobile size but it either shrinks it by distorting it, retaining the height but squeezing the width in, or just shows the left side of the picture if I use absolute. I need it to display the centre of the picture full height, while just chopping off the edges. it also needs to retain the carousel-caption over the top of it. I've tried a couple of d-block or d-flex but nothing works.
Don't mind the frog stuff, it's just a place filler!
EDIT:
I've managed to keep the aspect ratio of the pic as it shrinks by changing the css to remove the height: 32rem when less than 950px. I still would like to retain the height of the image and to crop the edges as it shrinks though but can't find any way of doing it. Does it need to be in some sort of fixed container and use object-fit cover and let it crop? What container can I use inside the carousel container? I'm really new to this!
<div class="carousel-inner">
<div class="carousel-item active">
<img class="d-flex justify-content-center" src="Images/Kermit.JPEG" alt="kermit looking
christmassy with his hat on">
<div class="container">
<div class="carousel-caption text-start">
<h1>Got a frog fetish?</h1>
<p>You've come to the right place!</p>
<p><a class="btn btn-lg btn-primary" href="#">Click here to watch</a></p>
</div>
</div>
</div>
<div class="carousel-item">
<img class="d-block w-100" src="Images/kermitsnow.JPEG" alt="kermit in the snow">
<div class="container">
<div class="carousel-caption">
<h1>Chilly frogs your fetish?</h1>
<p>Come see our videos of snow frogs</p>
<p><a class="btn btn-lg btn-primary" href="#">Click here to watch</a></p>
</div>
</div>
</div>
the css:
#media only screen and (max-width: 950px){
.carousel-item > img {
position: absolute;
max-width: 100%;
top: 0;
left: 0;
}
}
#media only screen and (min-width: 951px){
.carousel-item > img {
position: absolute;
max-width: 100%;
top: 0;
left: 0;
height: 32rem;
}
}

With Carousels the assumption is made that you have control over your images and thus can standardise on things like width and height -- I assume in this case that's not so (else you would just edit the images and crop them to all have the same height to width ratio at the very least).
The conundrum here is that centring is a function of the container the thing resides in, so in this case you'd need to wrap the <img> in a <div> and manipulate its CSS to centre it and limit its height with CSS max-height: 400px. But for images with heights larger than their widths you'd get centring, but with cropping to show only the top of the images.
One way to achieve a flexible image carousel is to use CSS background-image to control sizing and position. In your case I would nominate a height the carousel would operate in, e.g. 400px and then use a transparent 1x1 pixel to establish the image. You can then use CSS to manipulate the images with a background-image: url() CSS.
Try this HTML:
<div id="carouselExample1" class="carousel slide" data-bs-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active">
<img id="img1" class="d-block w-100" src="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" height="400px">
<div class="container">
<div class="carousel-caption text-start">
<h1>Got a frog fetish?</h1>
<p>You've come to the right place!</p>
<p><a class="btn btn-lg btn-primary" href="#">Click here to watch</a></p>
</div>
</div>
</div>
<div class="carousel-item">
<img id="img2" class="d-block w-100" src="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" height="400px">
<div class="container">
<div class="carousel-caption">
<h1>Chilly frogs your fetish?</h1>
<p>Come see our videos of snow frogs</p>
<p><a class="btn btn-lg btn-primary" href="#">Click here to watch</a></p>
</div>
</div>
</div>
</div>
</div>
..with this CSS:
#img1, #img2 {
background-position: center;
Background-repeat: no-repeat;
background-size: cover;
/* ..OR..
background-size: contain;
background-color: #1f2227;
*/
}
#img1 {
background-image: url(https://placekitten.com/640/360)
}
#img2 {
background-image: url(https://placekitten.com/360/640)
}
(If you're using dynamic HTML generation then inject a style="backgoround-image:url(xxx)" attribute into the <img>.)
See this JSFiddle for an example.

Related

How to make swiper-slide width the same as it's child?

I am trying to implement a slideshow with swiperjs with slidesPerView: 'auto' option with the following layout:
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide py-3">
<div class="swiper-zoom-container">
<img src="...">
</div>
</div>
</div>
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
<div class="swiper-pagination-wrapper">
<div class="swiper-pagination"></div>
</div>
</div>
the problem is that no matter how small the images' width is, the slides are always 100% wide, e.g. I need to make the orange containers here as wide as their gray children:
https://codepen.io/anon/pen/JVpBZO
I can't simply hardcode the width as I don't know it beforehand.
I tried to set display: inline-flex on .swiper-slide but it doesn't change anything.

Bootstrap accordion icons not working on collapsed states

I am working on a accordion in Bootstrap V 3.37. I have icons on the accordion which change with open and closed states. When I the state set to collapse init works fine. But when I have the accordion with none of them accordions collapsed in the icons are not working properly.
The icons should be working as:
Arrow facing down on closed state
Arrow facing up on open state
My code is as follows so far:
HTML Structure
<!-- Panel list accordion -->
<div class="panel-group">
<div class="panel-heading-one panel-main-heading-one">
<h4 class="panel-title panel-main-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
<img src="../images/image.svg" class="panel-icon">
<p>Title here</p>
</a>
</h4>
</div>
<div id="collapseTwo" class="panel-collapse collapse">
<div class="panel-body panel-bodymain-one">
<p>Text body here</p>
</div>
</div>
<div class="panel-heading-one panel-main-heading-one">
<h4 class="panel-title panel-main-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapseThree">
<img src="../images/image.svg" class="panel-icon">
<p>Title here</p>
</a>
</h4>
</div>
<div id="collapseThree" class="panel-collapse collapse">
<div class="panel-body panel-bodymain-one">
<p>Title body here</p>
</div>
</div>
<div class="panel-heading-one panel-main-heading-one">
<h4 class="panel-title panel-main-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapseThree">
<img src="../images/image.svg" class="panel-icon">
<p>Title here</p>
</a>
</h4>
</div>
<div id="collapseThree" class="panel-collapse collapse">
<div class="panel-body panel-bodymain-one">
<p>Text body here</p>
</div>
</div>
</div>
</div>
<!-- End panel list accordion -->
CSS
.panel-heading-one .accordion-toggle:after {
/* symbol for "opening" panels */
font-family: 'Glyphicons Halflings';
content: "\e113";
float: right;
color: #ffffff;
height: 24px;
width: 40px;
background-color: #000000;
}
.panel-heading-one .accordion-toggle.collapsed:after {
/* symbol for "collapsed" panels */
content: "\e114";
}
Screenshots attached also.
Closed state - still point up:
Open state is fine as it points up:
Any help would be great.
Change your css to:
.panel-heading-one .accordion-toggle:after{
font-family: 'Glyphicons Halflings';
content: "\e114";
float: right;
color: #ffffff;
height: 24px;
width: 40px;
background-color: #000000;
}
.panel-heading-one .accordion-toggle[aria-expanded="true"]:after {
/* symbol for "opening" panels */
content: "\e113";
}
.panel-heading-one .accordion-toggle[aria-expanded="false"]:after {
/* symbol for "collapsed" panels */
content: "\e114";
}
And it will work perfectly.
Extra: In your accordion you have used same href='#collapseThree'
for last and second last item. When you will click on last element, it
will open second last item. So change your href in last element.

Cannot bottom align image in jumbotron

I'm trying to bottom align an img in a jumbotron, but cannot figure it out.
<div class="jumbotron">
<div class="container">
<div class="row">
<div class="col-lg-8">
<h1>Hello, world!</h1>
<p>This is a template for a simple marketing or informational website. It includes a large callout called the hero unit and three supporting pieces of content. Use it as a starting point to create something more unique.</p>
<p><a class="btn btn-primary btn-lg">Learn more »</a></p>
</div>
<div class="col-lg-4 text-center">
<img src="http://via.placeholder.com/200x200">
</div>
</div>
</div>
</div>
I tried adding the following to the CSS
.vertical-bottom {
display: flex;
align-items: bottom;
}
And then added the additional class to the DIV containing the img
<div class="col-lg-4 text-center vertical-bottom">
This didn't work, why is it so hard to vertically align things in bootstrap?
Here a kind of code:
Bootply
Css:
The media query is set to 1200 because I see you're using col-lg-xx
#media screen and (min-width:1200px) {
.flex_container img{
object-fit: contain;
object-position: 100% 100%;
}
.flex_container {
display: flex;
}
}
Html:
<div class="jumbotron">
<div class="container">
<div class="row flex_container">
<div class="col-lg-8">
<h1>Hello, world!</h1>
<p>This is a template for a simple marketing or informational website. It includes a large callout called the hero unit and three supporting pieces of content. Use it as a starting point to create something more unique.</p>
<p><a class="btn btn-primary btn-lg">Learn more »</a></p>
</div>
<div class="col-lg-4 text-center flex_container">
<img src="http://via.placeholder.com/200x200" style="">
</div>
</div>
</div>
</div>

Bootstrap Column Content Centering and Absolute Positioning

I have a bootstrap row that has 3 columns. The columns display horizontally in everything except mobile. In mobile they stack vertically, which is what I want... here's the issue.
I have responsive (img-responsive) images within each of the 3 columns. In the mobile display, I want the images to be centered within the column. On top of that I need to have 2 div's with absolute positioning over the responsive images. It works fine in chrome, but in I.E. and FireFox, the images are not responsive due to the outer div which has display:inline-block.
Here's the code:
<div class="row">
<div class="col-md-4 col-sm-6" style="text-align:center;">
<div style="margin-bottom:15px;position:relative;display: inline-block;">
<div style="position:absolute;top:0;right:0;padding:5px;font-weight:bold;background-color:#bb091f;color:#FFF;">
Live Deal
</div>
<img class="img-responsive" style="border:solid 1px #666;display:inline-block !important;" src="http://www.weekesisland.com/photos/atv.jpg" />
<div class="deal2Timer" style="position:absolute;bottom:1px;right:1px;width:190px;opacity:.9;"></div>
</div>
</div>
<div class="col-md-4 col-sm-6" style="text-align:center;">
<div style="margin-bottom:15px;position:relative;display: inline-block;">
<div style="position:absolute;top:0;right:0;padding:5px;font-weight:bold;background-color:#bb091f;color:#FFF;">
Live Deal
</div>
<img class="img-responsive" style="border:solid 1px #666;display:inline-block !important;" src="http://www.weekesisland.com/photos/atv.jpg" />
<div class="deal3Timer" style="position:absolute;bottom:1px;right:1px;width:190px;opacity:.9;"></div>
</div>
</div>
<div class="col-md-4 col-sm-6" style="text-align:center;">
<div style="margin-bottom:15px;position:relative;display: inline-block;">
<div style="position:absolute;top:0;right:0;padding:5px;font-weight:bold;background-color:#bb091f;color:#FFF;">
Live Deal
</div>
<img class="img-responsive" style="border:solid 1px #666;display:inline-block !important;" src="http://www.weekesisland.com/photos/atv.jpg" />
<div class="deal4Timer" style="position:absolute;bottom:1px;right:1px;width:190px;opacity:.9;"></div>
</div>
</div>
</div>
And here's how it looks in chrome vs. firefox.
Chrome desktop:
Chrome mobile:
In mobile, when the windows width is smaller than the image, the image scales due to img-responsive.
Firefox desktop:
Firefox Mobile
Note how the firefox images are no longer responsive.... however it is centered in the column in the mobile display, and the absolute positioning of the 2 div's is correct.
Is there any way to accomplish this that is cross-browser compatible? I've tried a bunch of options, but without inline-block, I can't get the absolute elements positioned properly on the responsive image as well as centered in the column for the mobile display.
To get better results as responsive images, I use jQuery.
You should try something like : example jQuery reponsive image with CODEPEN link
Here's what I did to fix it... it was simpler than I thought:
<div class="col-md-4 col-sm-6" style="text-align:center;">
<div class="homeLiveDealMobile">
<div class="liveDealBlock">
Live Deal
</div>
<img class="img-responsive imgBorder" src="http://www.weekesisland.com/photos/atv.jpg" />
<div class="deal3Timer homepageTimer"></div>
</div>
</div>
CSS:
.liveDealBlock {
position:absolute;
top:0;
right:0;
padding:5px;
font-weight:bold;
background-color:#bb091f;
color:#FFF;
}
.homeLiveDealMobile {
margin-bottom:15px !important;
position:relative;
}
.homepageTimer {
position:absolute;
bottom:1px;
right:1px;
width:190px;
opacity:.9;
}
#media(max-width:767px){
.homeLiveDealMobile {
max-width:391px !important;
margin:auto;
}
}

How can I place a div below a navbar

I need to place a div below my top navbar which should not scroll with the page content just like the navbar.
I've tried to create it as a second navbar but it is still scrolling or causing a blank area above the two navbars.
How can I do this?
UPDATE
In this sample the green bar is on top of the navbar, but I need it to stay below the navbar: http://jsfiddle.net/julianonunes/y8YqK/
If I move the green bar code (code sample below) before the navbar, it is hidden by the navbar.
Ex:
<div class="navbar navbar-decoration navbar-fixed-top"></div>
<div class="navbar navbar-default navbar-remote navbar-fixed-top">
<div class="navbar-header"> Brand
</div>
</div>
Ok. The problem is your second div includes 'navbar-fixed-top' which is glueing it to the top of your screen. You don't want it fixed to the top - you just want it fixed in place below the main navbar. Change your html for the second nag to:
<div class="navbar navbar-default navbar-remote navbar-decoration ">
And add to your navbar-decoration class:
width: 100%
position: fixed;
You may have tried this already, but in the css for the second navbar put
position: fixed;
If you don't want to add to your stylesheet, you can put it in the div like this:
<div style="position: fixed;"> </div>
I am not sure, but you may also have to add display:block like this:
<div style="position: fixed; display: block;"> </div>
With Bootstrap, use this logic within their html/css:
<div class="navbar" style="position: fixed; display: block;">
<div class="navbar-inner">
<a class="brand" href="#">Title</a>
<ul class="nav">
<li class="active">Home</li>
<li>Link</li>
<li>Link</li>
</ul>
</div>
</div>
My last guess:
<div class="navbar" style="margin-top: 10px;">
<div class="navbar-inner">
<a class="brand" href="#">Title</a>
<ul class="nav">
<li class="active">Home</li>
<li>Link</li>
<li>Link</li>
</ul>
</div>
</div>
Hope one of those works.
I've got it fixed by adding margin-top to the second navbar with navbar-fixed-top.
<div class="navbar navbar-default navbar-remote navbar-fixed-top">
<div class="navbar-header"> Brand
</div>
</div>
<div class="navbar navbar-decoration navbar-fixed-top"></div>
CSS
.navbar-decoration {
margin-top: 50px;
min-height: 5px;
max-height: 5px;
/* ... */
}