Justify image in flex container so it doesn't stretch. Then add JS carousel (multiple images)
This problem is best broken into two parts:
- Layout: Horizontally centre an image with the container wrapping around the height/width of the content (no space).
- Carousel: Add a carousel to display a slideshow of images in this space.
The reason I've broken this into two parts is I tried to use slick.js for a carousel and it screwed the layout (example below). So thought I best get a solid UI before re-introducting that.
1. Layout
This was working in Chrome but checking Firefox and Safari (Mac OS), I realised my approach was flawed. Played around with various declarations for flex
but no luck. I thought I'd resolved the issue with overflow: hidden;
on the parent of the img
but that only fixed the layout in Chrome. Removing this at least seems to show a consistent results across the x3 browsers (Chrome, Firefox, Safari) I'm looking at.
Layout works fine if you view in Chrome: https://codepen.io/moy/pen/NWaObyW
2. Carousel
For reference, as my end goal is to use a carousel (so multiple images) in this space. I thought it was worth showing what happened in all 3 browsers when I added in slick.js.
Link: https://codepen.io/moy/pen/xxXyRmW
So the layout totally screws. Obviously me not the plug-in!
I don't need to use slick.js. I used it I found a way to include a dynamic count (1 of 3) when changing slides.
I'm looking to fade or slide between images. Display the counter and a caption (which I was thinking of using a data
attribute) to add the text to a div outside. So I'm open to something else if it's not possible to use slick.js with this layout.
Hope someone can help. Who knew such a small thing would cause such a big headache!
Solution 1:
Multiple issues :
-
slickjs
doesn't like its container to be aflex
element - There are several way to fix
slickjs
not working on firefox, one way I find not too messy is to refresh it right after its init
I fixed some of your CSS (grid container, flex containers...), I also added some extra wrappers in your HTML. Flex elements must be separated from your gallery stuff.
var $status = $('.slick-count');
var $slickElement = $('.slick');
$slickElement.on('init reInit afterChange', function (event, slick, currentSlide, nextSlide) {
//currentSlide is undefined on init -- set it to 0 in this case (currentSlide is 0 based)
var i = (currentSlide ? currentSlide : 0) + 1;
$status.text(i + ' / ' + slick.slideCount);
});
$slickElement.slick({
autoplay: true,
dots: false,
arrows: false
});
$slickElement.slick("refresh");
html {
background: white;
font-size: 62.5%;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
-webkit-overflow-scrolling: touch;
-webkit-tap-highlight-color: white;
-webkit-text-size-adjust: 100%;
}
/**
* Base `body` styling.
*/
body {
background-color: transparent;
color: black;
font-variant-ligatures: common-ligatures discretionary-ligatures historical-ligatures;
font-size: 1.8rem;
font-weight: 500;
line-height: 1.5;
margin: 0;
padding: 0;
text-rendering: optimizeLegibility;
}
h1 {
font-size: 3.6rem;
font-weight: inherit;
line-height: 1;
margin: 0 0 24px;
padding: 0;
}
p {
margin: 0 0 24px;
padding: 0;
}
img {
display: block;
width: 100%;
max-width: 100%;
}
.grid__item {
box-sizing: border-box;
padding: 24px 24px 0;
}
.gallery {
border: 2px solid black;
box-sizing: border-box;
margin-bottom: 24px;
}
@media only screen and (min-width: 1000px) {
.grid {
background-color: black;
display: grid;
grid-column-gap: 2px;
height: 100vh;
grid-auto-columns: minmax(0, 1fr);
grid-auto-flow: column;
}
.grid__item {
background-color: white;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.gallery {
overflow: hidden;
}
.gallery img {
object-fit: cover;
height: 100%;
}
/* img {
object-fit: contain;
max-height: none;
} */
.grid__item-header {
flex: 1 1 auto;
}
.grid__item-footer {
display: flex;
align-items: center;
justify-content: flex-start;
margin-top: auto;
}
.grid__item-footer .gallery-count {
display: flex;
flex: 1 1 0%;
justify-content: flex-end;
}
}
.slick-track,
.slick-slider,
.slick-list {
height: 100%;
}
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.8.1/slick.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<div class="grid">
<div class="grid__item">
<div>
<h1 class="brand-name">Brand Name</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p></div>
<p class="grid__item-footer">Footer Item</span></p>
</div>
<div class="grid__item">
<div class="grid__item-header">
<div class="gallery">
<div class="slick">
<div><img src="https://www.fillmurray.com/600/850" /></div>
<div><img src="https://www.fillmurray.com/600/850" /></div>
</div>
</div>
</div>
<p class="grid__item-footer">Name of Project <span class="slick-count"></span></p>
</div>
</div>