Im trying to combine Swiper and Bootstrap; trying to make each item in a Bootstrap grid have a paginator for some text. The issue I'm having is that the columns are for some reason all taking 100% width.
Using bootstrap 5
var swiper = new Swiper(".swiper1", {
slidesPerView: 1,
spaceBetween: 30,
loop: true,
pagination: {
el: ".swiper-pagination",
clickable: true,
},
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev",
},
});
var swiper = new Swiper(".swiper2", {
slidesPerView: 1,
spaceBetween: 30,
loop: true,
pagination: {
el: ".swiper-pagination",
clickable: true,
},
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev",
},
});
var swiper = new Swiper(".swiper3", {
slidesPerView: 1,
spaceBetween: 30,
loop: true,
pagination: {
el: ".swiper-pagination",
clickable: true,
},
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev",
},
});
.past-projects {
margin-top: 100px;
}
.past-projects h1 {
margin-bottom: 20px !important;
}
.past-projects .row .project {
width: 300px;
}
.past-projects .row .project img {
width: 100%;
height: 60%;
border-radius: 8px;
border: 3px solid #4b4b4b;
}
.past-projects .row .project .text {
text-align: center;
margin-top: 3px;
width: 100%;
display: block;
color: white;
}
.discord {
margin-top: 50px;
color: white;
text-align: center;
}
.discord a {
margin-top: 20px !important;
margin-right: 10px !important;
}
.swiper {
width: 100%;
height: 100%;
}
.swiper-slide {
color: white;
text-align: center;
font-size: 18px;
/* Center slide text vertically */
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
}
.swiper-slide img {
display: block;
width: 100%;
height: 100%;
-o-object-fit: cover;
object-fit: cover;
}
.swiper {
margin-left: auto;
margin-right: auto;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.min.css" />
<script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
<div class="bg-dark">
<div class="past-projects">
<h1 class="ps-1 text-white mx-auto text-center">Past Projects</h1>
<div class="row g-4">
<div class="col">
<div class="project mx-auto">
<img src="https://media.istockphoto.com/vectors/thumbnail-image-vector-graphic-vector-id1147544807?k=20&m=1147544807&s=612x612&w=0&h=pBhz1dkwsCMq37Udtp9sfxbjaMl27JUapoyYpQm0anc=" alt="">
<div class="text">
<p class="title">BloxRoyale</p>
<div class="swiper swiper1">
<div class="swiper-wrapper">
<p class="swiper-slide">ENG SLO GR</p>
<p class="swiper-slide">SP RU FP</p>
</div>
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
</div>
</div>
</div>
<div class="col">
<div class="project mx-auto">
<img src="https://media.istockphoto.com/vectors/thumbnail-image-vector-graphic-vector-id1147544807?k=20&m=1147544807&s=612x612&w=0&h=pBhz1dkwsCMq37Udtp9sfxbjaMl27JUapoyYpQm0anc=" alt="">
<div class="text">
<p class="title">BloxRoyale</p>
<div class="swiper swiper2">
<div class="swiper-wrapper">
<p class="swiper-slide">ENG SLO GR</p>
<p class="swiper-slide">SP RU FP</p>
</div>
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
</div>
</div>
</div>
</div>
<div class="col">
<div class="project mx-auto">
<img src="https://media.istockphoto.com/vectors/thumbnail-image-vector-graphic-vector-id1147544807?k=20&m=1147544807&s=612x612&w=0&h=pBhz1dkwsCMq37Udtp9sfxbjaMl27JUapoyYpQm0anc=" alt="">
<div class="text">
<p class="title">BloxRoyale</p>
<div class="swiper swiper3">
<div class="swiper-wrapper">
<p class="swiper-slide">ENG SLO GR</p>
<p class="swiper-slide">SP RU FP</p>
</div>
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
var swiper = new Swiper(".mySwiper", {
loop: true,
spaceBetween:24,
grabCursor: true,
pagination: {
el: ".swiper-pagination",
dynamicBullets: true,
},
breakpoints: {
768: {
slidesPerView: 1,
},
1024: {
slidesPerView: 3,
spaceBetween: 48,
},
},
});
Related
$(window).on({
load: function(){
$(this).trigger('resize');
}
});
$(document).ready(function(){
$('ul.tabs li').click(function(){
var tab_id = $(this).attr('data-tab');
$('ul.tabs li').removeClass('current');
$('.tab-content').removeClass('current');
$(this).addClass('current');
$("#"+tab_id).addClass('current');
})
})
var _prev = document.querySelector(".thumb-prev");
var _next = document.querySelector(".thumb-next");
var thumbnail;
var big;
thumbnail = new Swiper(".thumb", {
observer: true,
observeParents: true,
watchSlidesVisibility: true,
watchSlidesProgress: true,
centeredSlides: false,
slidesPerView: 3,
spaceBetween: 2,
loop: true,
direction: "vertical",
slideToClickedSlide: true,
rebuildOnUpdate: true
});
big = new Swiper(".big", {
observer: true,
observeParents: true,
watchSlidesVisibility: true,
watchSlidesProgress: true,
loop: true,
slidesPerView: 3,
navigation: {
nextEl: _next,
prevEl: _prev,
},
autoplay: {
delay: 1500,
disableOnInteraction: false,
pauseOnMouseEnter: false,
},
rebuildOnUpdate: true
});
big.controller.control = thumbnail;
thumbnail.controller.control = big;
big.update();
var _prev = document.querySelector(".thumb-prev2");
var _next = document.querySelector(".thumb-next2");
var thumbnail2;
var big2;
thumbnail2 = new Swiper(".thumb2", {
observer: true,
observeParents: true,
watchSlidesVisibility: true,
watchSlidesProgress: true,
centeredSlides: false,
slidesPerView: 3,
spaceBetween: 2,
loop: true,
direction: "vertical",
slideToClickedSlide: true,
rebuildOnUpdate: true
});
big2 = new Swiper(".big2", {
observer: true,
observeParents: true,
loop: true,
watchSlidesVisibility: true,
watchSlidesProgress: true,
slidesPerView: 3,
navigation: {
nextEl: _next,
prevEl: _prev,
},
autoplay: {
delay: 1500,
disableOnInteraction: false,
pauseOnMouseEnter: false,
},
rebuildOnUpdate: true
});
big2.controller.control = thumbnail2;
thumbnail2.controller.control = big2;
big2.update();
#import url('https://fonts.googleapis.com/css2?family=Roboto+Slab:wght#100;200;300;400;500;600;700;800;900&display=swap');
.roboto-slab {font-family: 'Roboto Slab', serif;}
.container {
font-family: 'Roboto Slab', serif;
}
/* tab area */
ul.tabs{
margin: 0px;
padding: 0px;
list-style: none;
}
ul.tabs li{
background: none;
color: #222;
display: inline-block;
padding: 10px 15px;
cursor: pointer;
}
ul.tabs li.current{
background: #ededed;
color: #222;
}
.tab-content{
display: none;
padding: 15px 0;
border-top:3px solid #eee;
}
.tab-content.current{
display: inherit;
}
/* //tab area */
body, html {
margin: 0;
padding: 0;
}
.swiper-initialized {
overflow: hidden;
}
.swiper-slide-active {
color: blue !important; /* paired item style */
}
.swiper-slide-thumb-active {
color: greenyellow; /* same as paired item style, but it works when using 'thumbs:...' settings. It's NOT used here */
}
.slider__area {
position: relative;
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 60vw;
}
.slider__box {
position: relative;
display: flex;
justify-content: center;
align-items: center;
}
.big {
display: flex;
justify-content: center;
align-items: center;
width: 50vw;
height: 30vw;
background-color: aquamarine;
}
.big2 {
display: flex;
justify-content: center;
align-items: center;
width: 50vw;
height: 30vw;
background-color:skyblue;
}
.swiper-slide {
display: flex;
justify-content: center;
align-items: center;
font-size: 50px;
font-weight: 600;
color: yellow;
}
.swiper-slide:hover {
background-color: hotpink;
cursor: pointer;
}
.thumb {
width: 20vw;
max-height: 30vh;
background-color: darkorange;
}
.thumb .swiper-slide {
background: crimson;
display: flex;
align-items: center;
cursor: pointer;
}
.thumb .swiper-slide:hover {
background: mediumturquoise;
}
.thumb2 {
width: 20vw;
max-height: 30vh;
background-color: mediumslateblue;
}
.thumb2 .swiper-slide {
background: coral;
display: flex;
align-items: center;
cursor: pointer;
}
.thumb2 .swiper-slide:hover {
background: mediumseagreen;
}
.slider__arrowbox {
position: absolute;
display: flex;
justify-content: space-around;
align-items: center;
width: 20vw;
height: 5vw;
background-color: midnightblue;
top: -7vw;
right: 0vw;
}
.slider__arrowbox .swiper-button-prev {
position: relative;
padding: 0.5vw 2vw;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin-top: 0;
margin-bottom: 0;
color:lime;
}
.slider__arrowbox .swiper-button-next {
position: relative;
padding: 0.5vw 2vw;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin-top: 0;
margin-bottom: 0;
color:lime;
}
.slider__arrowbox .swiper-button-prev:hover,
.slider__arrowbox .swiper-button-next:hover {
color: blue;
background-color: aliceblue;
}
.slider__arrowbox .swiper-button-prev2 {
position: relative;
padding: 0.5vw 2vw;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin-top: 0;
margin-bottom: 0;
color:lime;
}
.slider__arrowbox .swiper-button-next2 {
position: relative;
padding: 0.5vw 2vw;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin-top: 0;
margin-bottom: 0;
color:lime;
}
.slider__arrowbox .swiper-button-prev2:hover,
.slider__arrowbox .swiper-button-next2:hover {
color: blue;
background-color: aliceblue;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/8.2.6/swiper-bundle.min.css" rel="stylesheet"/>
<div class="container">
<ul class="tabs">
<li class="tab-link current" data-tab="tab-1">First Item</li>
<li class="tab-link" data-tab="tab-2">Second Item</li>
</ul>
<div id="tab-1" class="tab-content current">
<div class="slider__area">
<div class="slider__box">
<div class="thumb swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">
# 01
</div>
<div class="swiper-slide">
# 02
</div>
<div class="swiper-slide">
# 03
</div>
<div class="swiper-slide">
# 04
</div>
<div class="swiper-slide">
# 05
</div>
<div class="swiper-slide">
# 06
</div>
<div class="swiper-slide">
# 07
</div>
<div class="swiper-slide">
# 08
</div>
</div>
</div>
<div class="big swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">
#01
</div>
<div class="swiper-slide">
#02
</div>
<div class="swiper-slide">
#03
</div>
<div class="swiper-slide">
#04
</div>
<div class="swiper-slide">
#05
</div>
<div class="swiper-slide">
#06
</div>
<div class="swiper-slide">
#07
</div>
<div class="swiper-slide">
#08
</div>
</div>
</div>
<div class="slider__arrowbox">
<div class="swiper-button-prev thumb-prev"></div>
<div class="swiper-button-next thumb-next"></div>
</div>
</div>
</div>
</div>
<div id="tab-2" class="tab-content">
<div class="slider__area">
<div class="slider__box">
<div class="thumb2 swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">
# 01
</div>
<div class="swiper-slide">
# 02
</div>
<div class="swiper-slide">
# 03
</div>
<div class="swiper-slide">
# 04
</div>
<div class="swiper-slide">
# 05
</div>
<div class="swiper-slide">
# 06
</div>
<div class="swiper-slide">
# 07
</div>
<div class="swiper-slide">
# 08
</div>
</div>
</div>
<div class="big2 swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">
#01
</div>
<div class="swiper-slide">
#02
</div>
<div class="swiper-slide">
#03
</div>
<div class="swiper-slide">
#04
</div>
<div class="swiper-slide">
#05
</div>
<div class="swiper-slide">
#06
</div>
<div class="swiper-slide">
#07
</div>
<div class="swiper-slide">
#08
</div>
</div>
</div>
<div class="slider__arrowbox">
<div class="swiper-button-prev thumb-prev2"></div>
<div class="swiper-button-next thumb-next2"></div>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/8.2.6/swiper-bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
[demo check in codepen][1]
I'm tryting to make swiper slide in the tab.
Problem is if I clicked the second button is fine but after click again 1st menu and 2nd menu, slider is stopping. after that I click the thumbnail then move again.
I already add observer parameter but it doesn't work.
[https://codepen.io/patcaulfield/pen/xxjLxLm][1]
I tried 100% width but had no luck as it does not comply. anyone pls guide me. the swiper sometimes comes over the text it doesn't stay in the column. in mobile or tablet, I want them to be filling the screen with swiper slides and rest when you scroll down.in mobile view everything gets cut once i reach 528px any help
in mobile i want it to look like shown in imagepreview
var galleryThumbs = new Swiper('.gallery-thumbs', {
spaceBetween: 10,
slidesPerView: 4,
freeMode: true,
watchSlidesProgress: true,
});
var galleryTop = new Swiper('.gallery-top', {
spaceBetween: 10,
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
thumbs: {
swiper: galleryThumbs
}
});
ul.breadcrumb {
list-style: none;
display: flex;
flex-wrap: wrap;
width: 568px;
padding-left: 0;
}
ul.breadcrumb li {
display: inline;
font-size: 18px;
}
ul.breadcrumb li+li:before {
padding: 8px;
color: black;
content: "/\00a0";
}
ul.breadcrumb li a {
color: #0275d8;
text-decoration: none;
}
ul.breadcrumb li a:hover {
color: #01447e;
text-decoration: underline;
}
.product{
display: flex;
justify-content: center;
align-items: center;
margin: 0 auto;
}
.page-container {
display: grid;
grid-template-columns: auto 622px;
grid-column-gap: 21px;
grid-row-gap: 10px;
}
.swiper-container {
width: 538px;
height: 725px;
}
.swiper {
width: 100%;
height: 300px;
margin-left: auto;
margin-right: auto;
}
.swiper-slide {
background-size: cover;
background-position: center;
}
.gallery-top {
height: 78%;
width: 100%;
}
.gallery-thumbs {
margin-top: 6px;
height: 18%;
box-sizing: border-box;
padding: 10px 0;
}
.gallery-thumbs .swiper-slide {
width: 25%;
height: 100%;
opacity: 0.4;
}
.gallery-thumbs .swiper-slide-thumb-active {
opacity: 1;
}
.box2 {
background-color: yellow;
min-width: 200px;
}
.product-title{
font-size: 28px;
}
.box3 {
background-color: blue;
grid-row: 2;
grid-column: 1 / span 3;
min-height: 200px;
}
#media only screen and (max-width: 800px) {
.page-container {
display: flex;
align-items: center;
flex-direction: column;
justify-content: center;
}
}
<script src="https://unpkg.com/swiper#7/swiper-bundle.min.js"></script>
<link href="https://unpkg.com/swiper#7/swiper-bundle.min.css" rel="stylesheet"/>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="https://unpkg.com/swiper#7/swiper-bundle.min.css"/>
</head>
<body>
<div class="product">
<div class="page-container"> <!-- Container -->
<div class="swiper-container">
<!-- Column 1 (Swiper) -->
<!-- Swiper -->
<div class="swiper gallery-top">
<div class="swiper-wrapper">
<div class="swiper-slide" style="background-image:url(https://skylandtourism.com/wp-content/uploads/2018/03/Morning-Safari.jpg)"></div>
<div class="swiper-slide" style="background-image:url(https://skylandtourism.com/wp-content/uploads/2018/03/Morning-Safari.jpg)"></div>
<div class="swiper-slide" style="background-image:url(https://skylandtourism.com/wp-content/uploads/2018/03/Morning-Safari.jpg)"></div>
<div class="swiper-slide" style="background-image:url(https://skylandtourism.com/wp-content/uploads/2018/03/Morning-Safari.jpg)"></div>
<div class="swiper-slide" style="background-image:url(https://skylandtourism.com/wp-content/uploads/2018/03/Morning-Safari.jpg)"></div>
</div>
<!-- Add Arrows -->
<div class="swiper-button-next swiper-button-white"></div>
<div class="swiper-button-prev swiper-button-white"></div>
</div>
<div class="swiper gallery-thumbs">
<div class="swiper-wrapper">
<div class="swiper-slide" style="background-image:url(https://skylandtourism.com/wp-content/uploads/2018/03/Morning-Safari.jpg)"></div>
<div class="swiper-slide" style="background-image:url(https://skylandtourism.com/wp-content/uploads/2018/03/Morning-Safari.jpg)"></div>
<div class="swiper-slide" style="background-image:url(https://skylandtourism.com/wp-content/uploads/2018/03/Morning-Safari.jpg)"></div>
<div class="swiper-slide" style="background-image:url(https://skylandtourism.com/wp-content/uploads/2018/03/Morning-Safari.jpg)"></div>
<div class="swiper-slide" style="background-image:url(https://skylandtourism.com/wp-content/uploads/2018/03/Morning-Safari.jpg)"></div>
</div>
</div>
</div>
<!-- Column 2 (Details) -->
<div class="box2">
<ul class="breadcrumb">
<li>Home</li>
<li>Desert Safari</li>
<li>Morning Safari</li>
</ul>
<h1 class="product-title">Morning Safari</h1>
</div>
<!-- Content that spans two rows -->
<div class="box3">
Content that spans two rows
</div>
</div>
</div>
<script src="https://unpkg.com/swiper#7/swiper-bundle.min.js"></script>
</body>
</html>
So typically my approach, when I am faced with something like this, would be to add media queries and make sure that the page-container div is not a grid display in those queries. For example, what I would do is give it flex at a certain width and change the flex-direction from row to column, and align the items in the center. Using this flex-direction: column; is great for mobile devices.
var galleryThumbs = new Swiper('.gallery-thumbs', {
spaceBetween: 10,
slidesPerView: 4,
freeMode: true,
watchSlidesProgress: true,
});
var galleryTop = new Swiper('.gallery-top', {
spaceBetween: 10,
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
thumbs: {
swiper: galleryThumbs
}
});
ul.breadcrumb {
list-style: none;
display: flex;
flex-wrap: wrap;
width: 568px;
padding-left: 0;
}
ul.breadcrumb li {
display: inline;
font-size: 18px;
}
ul.breadcrumb li+li:before {
padding: 8px;
color: black;
content: "/\00a0";
}
ul.breadcrumb li a {
color: #0275d8;
text-decoration: none;
}
ul.breadcrumb li a:hover {
color: #01447e;
text-decoration: underline;
}
.product{
display: flex;
justify-content: center;
align-items: center;
margin: 0 auto;
}
.page-container {
display: grid;
grid-template-columns: auto 622px;
grid-column-gap: 21px;
grid-row-gap: 10px;
}
.swiper-container {
width: 538px;
height: 725px;
}
.swiper {
width: 100%;
height: 300px;
margin-left: auto;
margin-right: auto;
}
.swiper-slide {
background-size: cover;
background-position: center;
}
.gallery-top {
height: 78%;
width: 100%;
}
.gallery-thumbs {
margin-top: 6px;
height: 18%;
box-sizing: border-box;
padding: 10px 0;
}
.gallery-thumbs .swiper-slide {
width: 25%;
height: 100%;
opacity: 0.4;
}
.gallery-thumbs .swiper-slide-thumb-active {
opacity: 1;
}
.box2 {
background-color: yellow;
min-width: 200px;
}
.product-title{
font-size: 28px;
}
.box3 {
background-color: blue;
grid-row: 2;
grid-column: 1 / span 3;
min-height: 200px;
}
#media only screen and (max-width: 800px) {
.page-container {
display: flex;
align-items: center;
flex-direction: column;
justify-content: center;
}
}
#media only screen and (max-width: 550px) {
.swiper-container {
width: 450px;
height: 725px;
}
}
<script src="https://unpkg.com/swiper#7/swiper-bundle.min.js"></script>
<link href="https://unpkg.com/swiper#7/swiper-bundle.min.css" rel="stylesheet"/>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="https://unpkg.com/swiper#7/swiper-bundle.min.css"/>
</head>
<body>
<div class="product">
<div class="page-container"> <!-- Container -->
<div class="swiper-container">
<!-- Column 1 (Swiper) -->
<!-- Swiper -->
<div class="swiper gallery-top">
<div class="swiper-wrapper">
<div class="swiper-slide" style="background-image:url(https://skylandtourism.com/wp-content/uploads/2018/03/Morning-Safari.jpg)"></div>
<div class="swiper-slide" style="background-image:url(https://skylandtourism.com/wp-content/uploads/2018/03/Morning-Safari.jpg)"></div>
<div class="swiper-slide" style="background-image:url(https://skylandtourism.com/wp-content/uploads/2018/03/Morning-Safari.jpg)"></div>
<div class="swiper-slide" style="background-image:url(https://skylandtourism.com/wp-content/uploads/2018/03/Morning-Safari.jpg)"></div>
<div class="swiper-slide" style="background-image:url(https://skylandtourism.com/wp-content/uploads/2018/03/Morning-Safari.jpg)"></div>
</div>
<!-- Add Arrows -->
<div class="swiper-button-next swiper-button-white"></div>
<div class="swiper-button-prev swiper-button-white"></div>
</div>
<div class="swiper gallery-thumbs">
<div class="swiper-wrapper">
<div class="swiper-slide" style="background-image:url(https://skylandtourism.com/wp-content/uploads/2018/03/Morning-Safari.jpg)"></div>
<div class="swiper-slide" style="background-image:url(https://skylandtourism.com/wp-content/uploads/2018/03/Morning-Safari.jpg)"></div>
<div class="swiper-slide" style="background-image:url(https://skylandtourism.com/wp-content/uploads/2018/03/Morning-Safari.jpg)"></div>
<div class="swiper-slide" style="background-image:url(https://skylandtourism.com/wp-content/uploads/2018/03/Morning-Safari.jpg)"></div>
<div class="swiper-slide" style="background-image:url(https://skylandtourism.com/wp-content/uploads/2018/03/Morning-Safari.jpg)"></div>
</div>
</div>
</div>
<!-- Column 2 (Details) -->
<div class="box2">
<ul class="breadcrumb">
<li>Home</li>
<li>Desert Safari</li>
<li>Morning Safari</li>
</ul>
<h1 class="product-title">Morning Safari</h1>
</div>
<!-- Content that spans two rows -->
<div class="box3">
Content that spans two rows
</div>
</div>
</div>
<script src="https://unpkg.com/swiper#7/swiper-bundle.min.js"></script>
</body>
</html>
$(document).ready(function() {
$('.btn_activite').click(function() {
$('#activite').addClass('active');
$('#gestion').removeClass('active');
$('#creation').removeClass('active');
$('#contenu').removeClass('active');
$('#quotidien').removeClass('active');
$('#temps').removeClass('active');
});
$('.btn_gestion').click(function() {
$('#activite').removeClass('active');
$('#gestion').addClass('active');
$('#creation').removeClass('active');
$('#contenu').removeClass('active');
$('#quotidien').removeClass('active');
$('#temps').removeClass('active');
});
$('.btn_creation').click(function() {
$('#activite').removeClass('active');
$('#gestion').removeClass('active');
$('#creation').addClass('active');
$('#contenu').removeClass('active');
$('#quotidien').removeClass('active');
$('#temps').removeClass('active');
});
$('.btn_contenu').click(function() {
$('#activite').removeClass('active');
$('#gestion').removeClass('active');
$('#creation').removeClass('active');
$('#contenu').addClass('active');
$('#quotidien').removeClass('active');
$('#temps').removeClass('active');
});
$('.btn_quotidien').click(function() {
$('#activite').removeClass('active');
$('#gestion').removeClass('active');
$('#creation').removeClass('active');
$('#contenu').removeClass('active');
$('#quotidien').addClass('active');
$('#temps').removeClass('active');
});
$('.btn_temps').click(function() {
$('#activite').removeClass('active');
$('#gestion').removeClass('active');
$('#creation').removeClass('active');
$('#contenu').removeClass('active');
$('#quotidien').removeClass('active');
$('#temps').addClass('active');
});
});
#charset "UTF-8";
#import url("../webfonts/GothamBook/stylesheet.css");
body {
font-family: GothamBook;
color: #FEFEFE;
}
.flex-container {
DISPLAY: FLEX;
justify-content: space-between;
width: 100%;
color: #FFFFFF;
}
.item {
background-color: black;
width: 5%;
margin: 2px;
}
.item-activite {
background-color: black;
width: 15%;
margin: 2px;
text-align: center;
}
.item-gestion {
background-color: #4D4D4D;
width: 15%;
margin: 2px;
text-align: center;
}
.item-creation {
background-color: #40A9F5;
width: 15%;
margin: 2px;
text-align: center;
}
.item-cont {
background-color: #362984;
width: 15%;
margin: 2px;
text-align: center;
}
.item-quot {
background-color: #662D91;
width: 15%;
margin: 2px;
text-align: center;
}
.item-temps {
background-color: #42A246;
width: 15%;
margin: 2px;
text-align: center;
}
.font-black {
color: #000000;
}
.font-grn {
color: #42A246;
}
.container {
display: flex;
justify-content: center;
align-content: center;
}
#activite, #gestion, #creation, #contenu, #quotidien, #temps {
display: none;
}
#activite.active ,#gestion.active , #creation.active ,#contenu.active ,#quotidien.active , #temps.active{
display: block;
}
a {
text-decoration: none;
color: #FFFFFF;
}
.btn_activite, .btn_gestion, .btn_creation, .btn_contenu, .btn_quotidien, .btn_temps {
transition: .2s ease;
-webkit-transition: .2s ease;
cursor: pointer;
}
.btn_activite:hover, .btn_gestion:hover, .btn_creation:hover , .btn_contenu:hover, .btn_quotidien:hover, .btn_temps:hover{
transform: scale(1.1);
-webkit-transform: scale(1.1);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Menu ACCES CBR -->
<div class="flex-container">
<div class="item">
</div>
<div class="item-activite">
<a class="btn_activite" href="#" >
Activité Assurance Q+
</a>
</div>
<div class="item-gestion">
<a href="#" class="btn_gestion">
Gestion Assurance Q+
</a>
</div>
<div class="item-creation">
<a class="btn_creation" href="#" >
Recherche - création
</a>
</div>
<div class="item-cont">
<a href="#" class="btn_contenu">
Recherche - Contenu
</a>
</div>
<div class="item-quot">
<a class="btn_quotidien" href="#">
Quotidien | Hebdo +
</a>
</div>
<div class="item-temps">
<a href="#" class="btn_temps">
Temps | Chrono
</a>
</div>
<div class="item">
</div>
</div>
<!-- Page Activite -->
<div class="container active" id="activite">
<h1>
Activité Assurance Q+
</h1>
</div>
<div class="container" id="gestion">
<h1>
Gestion Assurance Q+
</h1>
</div>
<!-- Page Recherche creation -->
<div class="container" id="creation">
<h1>
Recherche Creation
</h1>
</div>
<!-- Page Aventuriers -->
<div class="container" id="contenu">
<h1>
Recherche Contenu
</h1>
</div>
<!-- Page Équipiers -->
<div class="container" id="quotidien">
<h1>
Quotidien | Hebdo +
</h1>
</div>
<!-- Page Supporters -->
<div class="container" id="temps">
<h1>
Temps | Chrono
</h1>
</div>
This is a top menu with 6 elements each having 15% width. Ignore the div class="item" elements as they just fill the other 10% on each side of the menu.
What I want is to change the order of the 6 classes, so when I click on btn_creation, for example, this menu moves to position 2 (position 1 and 8 are fillers - class "item", I just use positions 2 to 7).
Hope it's clear?
Basically anything I click goes to position 2, moving the rest to the right.
p.s code not working properly in here, but in my computer it works... sorry first time I do this here.
I believe the runnable example below is what is trying to be achieved?
The only change is in the jQuery handling. All the current handling is commented-out, replaced by the short code snippet at the end of the document.ready function. See the comments there.
$(document).ready(function() {
/*
$('.btn_activite').click(function() {
$('#activite').addClass('active');
$('#gestion').removeClass('active');
$('#creation').removeClass('active');
$('#contenu').removeClass('active');
$('#quotidien').removeClass('active');
$('#temps').removeClass('active');
});
$('.btn_gestion').click(function() {
$('#activite').removeClass('active');
$('#gestion').addClass('active');
$('#creation').removeClass('active');
$('#contenu').removeClass('active');
$('#quotidien').removeClass('active');
$('#temps').removeClass('active');
});
$('.btn_creation').click(function() {
$('#activite').removeClass('active');
$('#gestion').removeClass('active');
$('#creation').addClass('active');
$('#contenu').removeClass('active');
$('#quotidien').removeClass('active');
$('#temps').removeClass('active');
});
$('.btn_contenu').click(function() {
$('#activite').removeClass('active');
$('#gestion').removeClass('active');
$('#creation').removeClass('active');
$('#contenu').addClass('active');
$('#quotidien').removeClass('active');
$('#temps').removeClass('active');
});
$('.btn_quotidien').click(function() {
$('#activite').removeClass('active');
$('#gestion').removeClass('active');
$('#creation').removeClass('active');
$('#contenu').removeClass('active');
$('#quotidien').addClass('active');
$('#temps').removeClass('active');
});
$('.btn_temps').click(function() {
$('#activite').removeClass('active');
$('#gestion').removeClass('active');
$('#creation').removeClass('active');
$('#contenu').removeClass('active');
$('#quotidien').removeClass('active');
$('#temps').addClass('active');
});
*/
// For any <a> tag class that starts with btn_
$('a[class^="btn_"]').click(function() {
// Get the parent div and move it after the
// first div. (with the item class)
$(this).parent().insertAfter($('.item')[0]);
});
});
#charset "UTF-8";
#import url("../webfonts/GothamBook/stylesheet.css");
body {
font-family: GothamBook;
color: #FEFEFE;
}
.flex-container {
DISPLAY: FLEX;
justify-content: space-between;
width: 100%;
color: #FFFFFF;
}
.item {
background-color: black;
width: 5%;
margin: 2px;
}
.item-activite {
background-color: black;
width: 15%;
margin: 2px;
text-align: center;
}
.item-gestion {
background-color: #4D4D4D;
width: 15%;
margin: 2px;
text-align: center;
}
.item-creation {
background-color: #40A9F5;
width: 15%;
margin: 2px;
text-align: center;
}
.item-cont {
background-color: #362984;
width: 15%;
margin: 2px;
text-align: center;
}
.item-quot {
background-color: #662D91;
width: 15%;
margin: 2px;
text-align: center;
}
.item-temps {
background-color: #42A246;
width: 15%;
margin: 2px;
text-align: center;
}
.font-black {
color: #000000;
}
.font-grn {
color: #42A246;
}
.container {
display: flex;
justify-content: center;
align-content: center;
}
#activite, #gestion, #creation, #contenu, #quotidien, #temps {
display: none;
}
#activite.active ,#gestion.active , #creation.active ,#contenu.active ,#quotidien.active , #temps.active{
display: block;
}
a {
text-decoration: none;
color: #FFFFFF;
}
.btn_activite, .btn_gestion, .btn_creation, .btn_contenu, .btn_quotidien, .btn_temps {
transition: .2s ease;
-webkit-transition: .2s ease;
cursor: pointer;
}
.btn_activite:hover, .btn_gestion:hover, .btn_creation:hover , .btn_contenu:hover, .btn_quotidien:hover, .btn_temps:hover{
transform: scale(1.1);
-webkit-transform: scale(1.1);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Menu ACCES CBR -->
<div class="flex-container">
<div class="item">
</div>
<div class="item-activite">
<a class="btn_activite" href="#" >
Activité Assurance Q+
</a>
</div>
<div class="item-gestion">
<a href="#" class="btn_gestion">
Gestion Assurance Q+
</a>
</div>
<div class="item-creation">
<a class="btn_creation" href="#" >
Recherche - création
</a>
</div>
<div class="item-cont">
<a href="#" class="btn_contenu">
Recherche - Contenu
</a>
</div>
<div class="item-quot">
<a class="btn_quotidien" href="#">
Quotidien | Hebdo +
</a>
</div>
<div class="item-temps">
<a href="#" class="btn_temps">
Temps | Chrono
</a>
</div>
<div class="item">
</div>
</div>
<!-- Page Activite -->
<div class="container active" id="activite">
<h1>
Activité Assurance Q+
</h1>
</div>
<div class="container" id="gestion">
<h1>
Gestion Assurance Q+
</h1>
</div>
<!-- Page Recherche creation -->
<div class="container" id="creation">
<h1>
Recherche Creation
</h1>
</div>
<!-- Page Aventuriers -->
<div class="container" id="contenu">
<h1>
Recherche Contenu
</h1>
</div>
<!-- Page Équipiers -->
<div class="container" id="quotidien">
<h1>
Quotidien | Hebdo +
</h1>
</div>
<!-- Page Supporters -->
<div class="container" id="temps">
<h1>
Temps | Chrono
</h1>
</div>
I suggest you to modify the name of id, that simplify all the code and use insertafter(): (here i have just add x_ in front of id names..
$(document).ready(function() {
var selector = $('a[class^=btn_]');
selector.on('click', function() {
let clasn = $(this).attr('class').split('_')[1];
$('div[id^=x_]').removeClass('active');
$('#x_' + clasn).addClass('active');
var move = $(this).closest('div');
move.insertAfter($('.item').first());
});
});
#charset "UTF-8";
#import url("../webfonts/GothamBook/stylesheet.css");
body {
font-family: GothamBook;
color: #FEFEFE;
}
.flex-container {
DISPLAY: FLEX;
justify-content: space-between;
width: 100%;
color: #FFFFFF;
}
.item {
background-color: black;
width: 5%;
margin: 2px;
}
.item-activite {
background-color: black;
width: 15%;
margin: 2px;
text-align: center;
}
.item-gestion {
background-color: #4D4D4D;
width: 15%;
margin: 2px;
text-align: center;
}
.item-creation {
background-color: #40A9F5;
width: 15%;
margin: 2px;
text-align: center;
}
.item-cont {
background-color: #362984;
width: 15%;
margin: 2px;
text-align: center;
}
.item-quot {
background-color: #662D91;
width: 15%;
margin: 2px;
text-align: center;
}
.item-temps {
background-color: #42A246;
width: 15%;
margin: 2px;
text-align: center;
}
.font-black {
color: #000000;
}
.font-grn {
color: #42A246;
}
.container {
display: flex;
justify-content: center;
align-content: center;
}
#x_activite,
#x_gestion,
#x_creation,
#x_contenu,
#x_quotidien,
#x_temps {
display: none;
}
#x_activite.active,
#x_gestion.active,
#x_creation.active,
#x_contenu.active,
#x_quotidien.active,
#x_temps.active {
display: block;
}
a {
text-decoration: none;
color: #FFFFFF;
}
.btn_activite,
.btn_gestion,
.btn_creation,
.btn_contenu,
.btn_quotidien,
.btn_temps {
transition: .2s ease;
-webkit-transition: .2s ease;
cursor: pointer;
}
.btn_activite:hover,
.btn_gestion:hover,
.btn_creation:hover,
.btn_contenu:hover,
.btn_quotidien:hover,
.btn_temps:hover {
transform: scale(1.1);
-webkit-transform: scale(1.1);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Menu ACCES CBR -->
<div class="flex-container">
<div class="item">
</div>
<div class="item-activite">
<a class="btn_activite" href="#">
Activité Assurance Q+
</a>
</div>
<div class="item-gestion">
<a href="#" class="btn_gestion">
Gestion Assurance Q+
</a>
</div>
<div class="item-creation">
<a class="btn_creation" href="#">
Recherche - création
</a>
</div>
<div class="item-cont">
<a href="#" class="btn_contenu">
Recherche - Contenu
</a>
</div>
<div class="item-quot">
<a class="btn_quotidien" href="#">
Quotidien | Hebdo +
</a>
</div>
<div class="item-temps">
<a href="#" class="btn_temps">
Temps | Chrono
</a>
</div>
<div class="item">
</div>
</div>
<!-- Page Activite -->
<div class="container active" id="x_activite">
<h1>
Activité Assurance Q+
</h1>
</div>
<div class="container" id="x_gestion">
<h1>
Gestion Assurance Q+
</h1>
</div>
<!-- Page Recherche creation -->
<div class="container" id="x_creation">
<h1>
Recherche Creation
</h1>
</div>
<!-- Page Aventuriers -->
<div class="container" id="x_contenu">
<h1>
Recherche Contenu
</h1>
</div>
<!-- Page Équipiers -->
<div class="container" id="x_quotidien">
<h1>
Quotidien | Hebdo +
</h1>
</div>
<!-- Page Supporters -->
<div class="container" id="x_temps">
<h1>
Temps | Chrono
</h1>
</div>
Can we make custom elements to have the same behavior as dots? What I want is to keep the default dots, but also have some other elements acting as dots. For example, if the slide 2 is active the element dot-2 should have the class active. The same goes for click behavior, if I'll click on the element .dot-3, the slide 3 should be active.
jsfiddle
<div id="carousel" class="slider">
<div class="slider-item">1</div>
<div class="slider-item">2</div>
<div class="slider-item">3</div>
</div>
<div class="extra-dots-box">
<div class="dot-1 extra-dot">
<span>1</span>
<h3>Custom Title</h3>
<p>lorem ipsum lorem ipsum</p>
</div>
<div class="dot-2 extra-dot">
<span>2</span>
<h3>Custom Title</h3>
<p>lorem ipsum lorem ipsum</p>
</div>
<div class="dot-3 extra-dot">
<span>3</span>
<h3>Custom Title</h3>
<p>lorem ipsum lorem ipsum</p>
</div>
</div>
css
.slider-item {
border: 5px solid #333;
background: #ccc;
height: 200px;
font-size: 72px;
text-align: center;
line-height: 200px;
}
/* Slider arrows */
.slick-arrow {
z-index: 9;
}
.slick-prev {
left: 0;
}
.slick-next {
right: 0;
}
/* extra dots */
.extra-dots-box{
display: flex;
margin-top: 100px;
margin-bottom: 100px;
}
.extra-dots-box .extra-dot{
width: 350px;
height: 250px;
background: #111111;
color: #ffffff;
font-size: 18px;
margin-right: 30px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.extra-dots-box .extra-dot:hover{
cursor: pointer;
}
.extra-dots-box .extra-dot:hover,.extra-dots-box .extra-dot.active {
background: red;
}
jQuery
$(document).ready(function() {
var slickOpts = {
slidesToShow: 1,
slidesToScroll: 1,
//centerMode: true,
easing: 'swing', // see http://api.jquery.com/animate/
speed: 700,
dots: true,
customPaging: function(slick,index) {
return '<a>' + (index + 1) + '</a>';
}
};
// Init slick carousel
$('#carousel').slick(slickOpts);
});
jQuery(document).ready(function($) {
$('.slider-for').slick({
slidesToShow: 1,
slidesToScroll: 1,
arrows: false,
fade: true,
dots: true,
customPaging: function(slick,index) {
return '<a>' + (index + 1) + '</a>';
},
asNavFor: '.slider-nav'
});
//set active class to first slide
$('.slider-nav .slick-slide').eq(0).addClass('slick-active');
$('.slider-nav').slick({
slidesToShow: 6,
slidesToScroll: 1,
asNavFor: '.slider-for',
dots: false,
focusOnSelect: true,
responsive: [{
breakpoint: 992,
settings: {
vertical: false,
}
},
{
breakpoint: 768,
settings: {
vertical: false,
}
},
{
breakpoint: 580,
settings: {
vertical: false,
slidesToShow: 3,
}
},
{
breakpoint: 380,
settings: {
vertical: false,
slidesToShow: 2,
}
}
]
});
$('.slider-nav').on('mouseover', '.slick-slide', function (e) {
var $currTarget = $(e.currentTarget),
index = $currTarget.data('slick-index'),
slickObj = $('.slider-for').slick('getSlick');
slickObj.slickGoTo(index);
});
});
.slider-item {
border: 5px solid #333;
background: #ccc;
height: 200px;
font-size: 72px;
text-align: center;
line-height: 200px;
}
/* Slider arrows */
.slick-arrow {
z-index: 9;
}
.slick-prev {
left: 0;
}
.slick-next {
right: 0;
}
/* extra dots */
.extra-dots-box{
display: flex;
margin-top: 100px;
margin-bottom: 100px;
}
.extra-dots-box .extra-dot{
width: 350px;
height: 250px;
background: #111111;
color: #ffffff;
font-size: 18px;
margin-right: 30px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.extra-dots-box .extra-dot:hover{
cursor: pointer;
}
.extra-dots-box .extra-dot:hover,.extra-dots-box .extra-dot.active {
background: red;
}
/*For dots*/
.slick-dots {
position: absolute;
bottom: -45px;
display: block;
width: 100%;
padding: 0;
list-style: none;
text-align: center;
}
.slick-dots li {
position: relative;
display: inline-block;
width: 20px;
height: 20px;
margin: 0 5px;
padding: 0;
cursor: pointer;
}
.slider-nav .slick-slide.slick-current.slick-active .extra-dot {
border: 1px solid #80D6CF;
border-radius: 2px;
background-color: red;
box-shadow: 0 0 2px 0 rgba(0,0,0,0.1), 0 2px 4px 0 rgba(0,0,0,0.1);
padding: 3px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.9.0/slick.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.9.0/slick.min.js"></script>
<div id="carousel" class="slider slider-for">
<div class="slider-item">1</div>
<div class="slider-item">2</div>
<div class="slider-item">3</div>
</div>
<div class="extra-dots-box slider-nav thumb-image">
<div class="dot-1 extra-dot">
<span>1</span>
<h3>Custom Title</h3>
<p>lorem ipsum lorem ipsum</p>
</div>
<div class="dot-2 extra-dot">
<span>2</span>
<h3>Custom Title</h3>
<p>lorem ipsum lorem ipsum</p>
</div>
<div class="dot-3 extra-dot">
<span>3</span>
<h3>Custom Title</h3>
<p>lorem ipsum lorem ipsum</p>
</div>
</div>
I'm using Swiper to create some image sliders, and I'm trying to enclose the sliders within 2 flexbox containers such that my images are responsive.
I'd like to add some text on top of the images, aligned with the upper left corner of the image, and this is where everything goes wrong.
I believe I'm dealing with Flexbox "problem" very similar to Why don't flex items shrink past content size? in which case the parent DIV is keeping the width of the original image. The problem is the fixes in the post don't appear to work once I wrap the image in <div class="img-wrapper">. In fact I've spent quite a bit of time researching, and nothing quite seems to get it working the way I want.
This question is a fork of CSS object-fit: contain; is keeping original image width in layout
NOTE: Open the below code snippet in a new window to see the issue.
var swiper = new Swiper('.swiper-container', {
slidesPerView: 1,
spaceBetween: 50,
// init: false,
pagination: {
el: '.swiper-pagination',
clickable: true,
},
});
img {
object-fit: contain;
min-width: 0;
height: 100%;
max-width: 100%;
}
.img-num {
float: left;
position: absolute;
padding-left: 10px;
text-shadow: 1px 1px 2px black, 0 0 1em blue, 0 0 0.2em blue;
color: white;
font: 1.5em Georgia, serif;
}
.img-wrapper {
border-style: solid;
border-width: thin;
height: 100%;
object-fit: contain;
min-width: 0;
}
html,
body {
margin: 0;
height: 100%;
}
body {
background: #eee;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
font-size: 14px;
color: #000;
margin: 0;
padding: 0;
}
.page {
height: 100%;
display: flex;
}
.main-container {
flex: 1 1 0;
display: flex;
min-width: 0;
flex-direction: column;
}
.half-containers {
flex: 0 1 50%;
overflow: auto;
box-sizing: border-box;
border: 0.5px solid red;
display: flex;
}
.page-header {
flex: 0 0 auto;
background-color: #dcdcdc;
}
.page-footer {
flex: 0 0 auto;
background-color: #dcdcdc;
}
.swiper-container {
width: 100%;
height: 100%;
}
.swiper-slide {
text-align: center;
font-size: 18px;
background: #fff;
/* Center slide text vertically */
display: -webkit-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.4.6/css/swiper.css" rel="stylesheet" />
<div class="page">
<div class="main-container">
<div class="page-header">
This is a header
</div>
<div class="half-containers">
<!-- Swiper -->
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">
<div class="img-wrapper">
<div class="img-num">1</div>
<img src='https://i.imgur.com/mSPw98T.jpg' />
</div>
</div>
<div class="swiper-slide">
<div class="img-wrapper">
<div class="img-num">2</div>
<img src='https://i.imgur.com/mSPw98T.jpg' />
</div>
</div>
</div>
</div>
</div>
<div class="half-containers">
<!-- Swiper -->
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">
<div class="img-wrapper">
<div class="img-num">3</div>
<img src='https://i.imgur.com/mSPw98T.jpg' />
</div>
</div>
<div class="swiper-slide">
<div class="img-wrapper">
<div class="img-num">4</div>
<img src='https://i.imgur.com/mSPw98T.jpg' />
</div>
</div>
</div>
</div>
</div>
<div class="page-footer">
This is a footer
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.4.6/js/swiper.js"></script>
First, the use of object-fit is useless when applied to a div and not to the image1. Even the use of it within the image in this case is useless because the image is not getting distorted. object-fit only apply when the ratio is lost in order to bring it back again.
So you will have the same issue without:
var swiper = new Swiper('.swiper-container', {
slidesPerView: 1,
spaceBetween: 50,
// init: false,
pagination: {
el: '.swiper-pagination',
clickable: true,
},
});
img {
height: 100%;
max-width: 100%;
}
.img-num {
float: left;
position: absolute;
padding-left: 10px;
text-shadow: 1px 1px 2px black, 0 0 1em blue, 0 0 0.2em blue;
color: white;
font: 1.5em Georgia, serif;
}
.img-wrapper {
border-style: solid;
border-width: thin;
height: 100%;
}
html,
body {
margin: 0;
height: 100%;
}
body {
background: #eee;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
font-size: 14px;
color: #000;
margin: 0;
padding: 0;
}
.page {
height: 100%;
display: flex;
}
.main-container {
flex: 1 1 0;
display: flex;
min-width: 0;
flex-direction: column;
}
.half-containers {
flex: 0 1 50%;
overflow: auto;
box-sizing: border-box;
border: 0.5px solid red;
display: flex;
}
.page-header {
flex: 0 0 auto;
background-color: #dcdcdc;
}
.page-footer {
flex: 0 0 auto;
background-color: #dcdcdc;
}
.swiper-container {
width: 100%;
height: 100%;
}
.swiper-slide {
text-align: center;
font-size: 18px;
background: #fff;
/* Center slide text vertically */
display: flex;
justify-content: center;
align-items: center;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.4.6/css/swiper.css" rel="stylesheet" />
<div class="page">
<div class="main-container">
<div class="page-header">
This is a header
</div>
<div class="half-containers">
<!-- Swiper -->
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">
<div class="img-wrapper">
<div class="img-num">1</div>
<img src='https://i.imgur.com/mSPw98T.jpg' />
</div>
</div>
<div class="swiper-slide">
<div class="img-wrapper">
<div class="img-num">2</div>
<img src='https://i.imgur.com/mSPw98T.jpg' />
</div>
</div>
</div>
</div>
</div>
<div class="half-containers">
<!-- Swiper -->
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">
<div class="img-wrapper">
<div class="img-num">3</div>
<img src='https://i.imgur.com/mSPw98T.jpg' />
</div>
</div>
<div class="swiper-slide">
<div class="img-wrapper">
<div class="img-num">4</div>
<img src='https://i.imgur.com/mSPw98T.jpg' />
</div>
</div>
</div>
</div>
</div>
<div class="page-footer">
This is a footer
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.4.6/js/swiper.js"></script>
Now the issue you are facing is that the image wrapper is first sized considerd the image width then the image is getting resized inside that wrapper. It's somehow complex but if you remove height:100% from the image you will have this:
var swiper = new Swiper('.swiper-container', {
slidesPerView: 1,
spaceBetween: 50,
// init: false,
pagination: {
el: '.swiper-pagination',
clickable: true,
},
});
img {
/*height: 100%;*/
max-width: 100%;
}
.img-num {
float: left;
position: absolute;
padding-left: 10px;
text-shadow: 1px 1px 2px black, 0 0 1em blue, 0 0 0.2em blue;
color: white;
font: 1.5em Georgia, serif;
}
.img-wrapper {
border-style: solid;
border-width: thin;
height: 100%;
}
html,
body {
margin: 0;
height: 100%;
}
body {
background: #eee;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
font-size: 14px;
color: #000;
margin: 0;
padding: 0;
}
.page {
height: 100%;
display: flex;
}
.main-container {
flex: 1 1 0;
display: flex;
min-width: 0;
flex-direction: column;
}
.half-containers {
flex: 0 1 50%;
overflow: auto;
box-sizing: border-box;
border: 0.5px solid red;
display: flex;
}
.page-header {
flex: 0 0 auto;
background-color: #dcdcdc;
}
.page-footer {
flex: 0 0 auto;
background-color: #dcdcdc;
}
.swiper-container {
width: 100%;
height: 100%;
}
.swiper-slide {
text-align: center;
font-size: 18px;
background: #fff;
/* Center slide text vertically */
display: flex;
justify-content: center;
align-items: center;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.4.6/css/swiper.css" rel="stylesheet" />
<div class="page">
<div class="main-container">
<div class="page-header">
This is a header
</div>
<div class="half-containers">
<!-- Swiper -->
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">
<div class="img-wrapper">
<div class="img-num">1</div>
<img src='https://i.imgur.com/mSPw98T.jpg' />
</div>
</div>
<div class="swiper-slide">
<div class="img-wrapper">
<div class="img-num">2</div>
<img src='https://i.imgur.com/mSPw98T.jpg' />
</div>
</div>
</div>
</div>
</div>
<div class="half-containers">
<!-- Swiper -->
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">
<div class="img-wrapper">
<div class="img-num">3</div>
<img src='https://i.imgur.com/mSPw98T.jpg' />
</div>
</div>
<div class="swiper-slide">
<div class="img-wrapper">
<div class="img-num">4</div>
<img src='https://i.imgur.com/mSPw98T.jpg' />
</div>
</div>
</div>
</div>
</div>
<div class="page-footer">
This is a footer
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.4.6/js/swiper.js"></script>
This is actually what you want considering the width but the height is big thus the image is overflowing. By adding height:100% you will decrease the height and also the width because the image will try to keep its ratio. Of course, the wrapper will not get resized again because will have a cycle thus it will stuck at that size.
An easy fix is to simply make the image wrapper position: absolute;. its height is already defined as 100% and its width will shrink to fit the image width. No need to adjust top/left because it's already centred without the use of absolute thus it will be kept centred:
var swiper = new Swiper('.swiper-container', {
slidesPerView: 1,
spaceBetween: 50,
// init: false,
pagination: {
el: '.swiper-pagination',
clickable: true,
},
});
img {
height: 100%;
max-width: 100%;
}
.img-num {
top:0;
left:0;
position: absolute;
padding-left: 10px;
text-shadow: 1px 1px 2px black, 0 0 1em blue, 0 0 0.2em blue;
color: white;
font: 1.5em Georgia, serif;
}
.img-wrapper {
border-style: solid;
border-width: thin;
height: 100%;
position: absolute;
}
html,
body {
margin: 0;
height: 100%;
}
body {
background: #eee;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
font-size: 14px;
color: #000;
margin: 0;
padding: 0;
}
.page {
height: 100%;
display: flex;
}
.main-container {
flex: 1 1 0;
display: flex;
min-width: 0;
flex-direction: column;
}
.half-containers {
flex: 0 1 50%;
overflow: auto;
box-sizing: border-box;
border: 0.5px solid red;
display: flex;
}
.page-header {
flex: 0 0 auto;
background-color: #dcdcdc;
}
.page-footer {
flex: 0 0 auto;
background-color: #dcdcdc;
}
.swiper-container {
width: 100%;
height: 100%;
}
.swiper-slide {
text-align: center;
font-size: 18px;
background: #fff;
/* Center slide text vertically */
display: flex;
justify-content: center;
align-items: center;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.4.6/css/swiper.css" rel="stylesheet" />
<div class="page">
<div class="main-container">
<div class="page-header">
This is a header
</div>
<div class="half-containers">
<!-- Swiper -->
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">
<div class="img-wrapper">
<div class="img-num">1</div>
<img src='https://i.imgur.com/mSPw98T.jpg' />
</div>
</div>
<div class="swiper-slide">
<div class="img-wrapper">
<div class="img-num">2</div>
<img src='https://i.imgur.com/mSPw98T.jpg' />
</div>
</div>
</div>
</div>
</div>
<div class="half-containers">
<!-- Swiper -->
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">
<div class="img-wrapper">
<div class="img-num">3</div>
<img src='https://i.imgur.com/mSPw98T.jpg' />
</div>
</div>
<div class="swiper-slide">
<div class="img-wrapper">
<div class="img-num">4</div>
<img src='https://i.imgur.com/mSPw98T.jpg' />
</div>
</div>
</div>
</div>
</div>
<div class="page-footer">
This is a footer
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.4.6/js/swiper.js"></script>
1The object-fit property specifies how the contents of a replaced element should be fitted to the box established by its used height and width.ref
A div is not a replaced element. Replaced elements are element like canvas, img, iframe, etc (https://html.spec.whatwg.org/multipage/rendering.html#replaced-elements)
UPDATE
The above seems to work only on Chrome so I would consider a small JS hack to rectify the width of the element on the other browsers:
var swiper = new Swiper('.swiper-container', {
slidesPerView: 1,
spaceBetween: 50,
// init: false,
pagination: {
el: '.swiper-pagination',
clickable: true,
},
});
$('.img-wrapper').each(function() {
$(this).width($(this).find('img').width())
})
$( window ).resize(function() {
$('.img-wrapper').each(function() {
$(this).css('width','100%').width($(this).find('img').width())
})
});
img {
height: 100%;
max-width: 100%;
object-fit:cover;
}
.img-num {
top:0;
left:0;
position: absolute;
padding-left: 10px;
text-shadow: 1px 1px 2px black, 0 0 1em blue, 0 0 0.2em blue;
color: white;
font: 1.5em Georgia, serif;
}
.img-wrapper {
border-style: solid;
border-width: thin;
height: 100%;
position: absolute;
max-width:100%;
}
html,
body {
margin: 0;
height: 100%;
}
body {
background: #eee;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
font-size: 14px;
color: #000;
margin: 0;
padding: 0;
}
.page {
height: 100%;
display: flex;
}
.main-container {
flex: 1 1 0;
display: flex;
min-width: 0;
flex-direction: column;
}
.half-containers {
flex: 0 1 50%;
overflow: auto;
box-sizing: border-box;
border: 0.5px solid red;
display: flex;
}
.page-header {
flex: 0 0 auto;
background-color: #dcdcdc;
}
.page-footer {
flex: 0 0 auto;
background-color: #dcdcdc;
}
.swiper-container {
width: 100%;
height: 100%;
}
.swiper-slide {
text-align: center;
font-size: 18px;
background: #fff;
/* Center slide text vertically */
display: flex;
justify-content: center;
align-items: center;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.4.6/css/swiper.css" rel="stylesheet" />
<div class="page">
<div class="main-container">
<div class="page-header">
This is a header
</div>
<div class="half-containers">
<!-- Swiper -->
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">
<div class="img-wrapper">
<div class="img-num">1</div>
<img src='https://i.imgur.com/mSPw98T.jpg' />
</div>
</div>
<div class="swiper-slide">
<div class="img-wrapper">
<div class="img-num">2</div>
<img src='https://i.imgur.com/mSPw98T.jpg' />
</div>
</div>
</div>
</div>
</div>
<div class="half-containers">
<!-- Swiper -->
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">
<div class="img-wrapper">
<div class="img-num">3</div>
<img src='https://i.imgur.com/mSPw98T.jpg' />
</div>
</div>
<div class="swiper-slide">
<div class="img-wrapper">
<div class="img-num">4</div>
<img src='https://i.imgur.com/mSPw98T.jpg' />
</div>
</div>
</div>
</div>
</div>
<div class="page-footer">
This is a footer
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.4.6/js/swiper.js"></script>