I have been working on several things related to this for hours now. I finally got it to work, but somehow it broke again. So here I am.
For my use-case I want to display images with Packery. The images have to sort themselves so that the grid has no gaps. The user will be able to choose the sizing of the images inside the grid, and the grid should sort accordingly.
With every method that I've tried, they display over the rest of the page, or stack on top of each other.
I tested this with Masonry, Packery and Isotope. All give the same results.
My code sample below is of the Packery attempt.
Edit: After more testing, I figured out that this issue only occurs when combining height and width classes. When only one of those is present, no overlapping occurs. I do need this functionality though.
The blue line shows where the images should end.
$(document).ready(function() {
let $grid = $('.grid').imagesLoaded(function() {
// init Packery after all images have loaded
$('.grid').packery({
itemSelector: '.grid-item',
percentPosition: true
});
$grid.packery('layout');
});
// I have also tried this approach
let $grid = $('.grid').packery({
itemSelector: '.grid-item',
percentPosition: true
});
// layout Packery after each image loads
$grid.imagesLoaded().progress(function() {
$grid.packery();
});
});
* {
box-sizing: border-box;
}
/* force scrollbar, prevents initial gap */
html {
overflow-y: scroll;
}
body {
font-family: sans-serif;
}
/* ---- grid ---- */
.grid {
background: #DDD;
}
/* clear fix */
.grid:after {
content: '';
display: block;
clear: both;
}
/* ---- .element-item ---- */
/* 5 columns, percentage width */
.grid-item {
float: left;
width: 16.667%;
height: 200px;
}
.grid-item img {
display: block;
width: 100%;
height: 100%;
}
.grid-item--small-width {
width: 16.667%;
}
/*Small*/
.grid-item--double-small-width {
width: 33.334%;
}
/*Double*/
.grid-item--half-width {
width: 50%;
}
/*Half*/
.grid-item--three-fourth-width {
width: 66.668%;
}
/*Bit More Than Half*/
.grid-item--big-width {
width: 83.335%;
}
/*Big*/
.grid-item--full-width {
width: 100%;
}
/*Full*/
.grid-item--small-height {
height: 16.667%;
}
/*Small*/
.grid-item--double-small-height {
height: 33.334%;
}
/*Double*/
.grid-item--half-height {
height: 50%;
}
/*Half*/
.grid-item--three-fourth-height {
height: 66.668%;
}
/*Bit More Than Half*/
.grid-item--big-height {
height: 83.335%;
}
/*Big*/
.grid-item--full-height {
height: 100%;
}
/*Full*/
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="css/masonry.classes.css">
<section>
<div class="container-fluid text-center" style="background-color: white; color: black">
<div class="container" style="margin-bottom: 20px">
<div class="row">
<h1>Portfolio</h1>
<div class="col-md-12">
<div class="grid" style="height: 1200px;">
<div class="grid-sizer"></div>
<div class="grid-item grid-item--small-width grid-item--full-height"><img class="img-responsive" src="http://landstededev.test/storage/projects/projectImage_4.png"></div>
<div class="grid-item grid-item--small-width grid-item--half-height"><img class="img-responsive" src="http://landstededev.test/storage/projects/projectImage_4.png"></div>
<div class="grid-item grid-item--small-width grid-item--small-height"><img class="img-responsive" src="http://landstededev.test/storage/projects/projectImage_4.png"></div>
<div class="grid-item grid-item--small-width grid-item--small-height"><img class="img-responsive" src="http://landstededev.test/storage/project/projectImage_4.png"></div>
<div class="grid-item grid-item--full-width grid-item--small-height"><img class="img-responsive" src="http://landstededev.test/storage/projects/projectImage_4.png"></div>
<div class="grid-item grid-item--small-width grid-item--small-height"><img class="img-responsive" src="http://landstededev.test/storage/projects/projectImage_4.png"></div>
<div class="grid-item grid-item--small-width grid-item--small-height"><img class="img-responsive" src="http://landstededev.test/storage/projects/projectImage_4.png"></div>
<div class="grid-item grid-item--small-width grid-item--small-height"><img class="img-responsive" src="http://landstededev.test/storage/projects/projectImage_4.png"></div>
<div class="grid-item grid-item--small-width grid-item--small-height"><img class="img-responsive" src="http://landstededev.test/storage/projects/projectImage_4.png"></div>
<div class="grid-item grid-item--small-width grid-item--small-height"><img class="img-responsive" src="http://landstededev.test/storage/projects/projectImage_4.png"></div>
</div>
</div>
</div>
</div>
</div>
</section>
<section>
<div class="container-fluid" style="background-color: #f0f0f0; color: #121212">
<div class="container">
<div class="row align-items-center justify-content-center">
<div class="col-4 text-center">
Link To Twitter
<br>
</div>
<div class="col-8" style="border-left: 2px solid #121212">
<h2 class="title">Title</h2>
<p>Some description will go here</p>
</div>
</div>
</div>
</div>
</section>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script type="text/javascript" src="https://unpkg.com/packery#2.1.2/dist/packery.pkgd.min.js"></script>
<script src="https://unpkg.com/imagesloaded#4/imagesloaded.pkgd.min.js"></script>
The javascript does not work with or without the $grid.packery('layout'); part. I have this here because it fixed an issue I had before.
After experimenting for a few more hours, I figured out that it is not possible to combine width and height classes that both use percentages.
I now use percentages for width only. Height now uses fixed px values.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I would like to have the IMG aligned on the same line using bootstrap how to do it? I am quite junior.
Is it necessary to define col-12 below the row?
img {
width: 15rem;
height: 15rem;
margin: 10px 0;
}
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/js/bootstrap.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row" *ngFor="let img of lstImg">
<div class="col-sm-3" *ngIf="img != 'tImg'; else elseBlock">
<img src="https://thumbs.dreamstime.com/z/tv-test-image-card-rainbow-multi-color-bars-geometric-signals-retro-hardware-s-minimal-pop-art-print-suitable-89603635.jpg" alt="img">
</div>
<div class="col-sm-6" #elseBlock>
<img src="https://thumbs.dreamstime.com/z/tv-test-image-card-rainbow-multi-color-bars-geometric-signals-retro-hardware-s-minimal-pop-art-print-suitable-89603635.jpg" alt="img">
</div>
</div>
</div>
Contain two different sized images on the same row (Option 1)
The Bootstrap way... Adding .img-fluid class to the images applies max-width: 100%; and height: auto; so that they scale with the parent element.
img {
height: 15rem;
}
.row {
background: #e3e3e3; /* let's visualise the row */
}
.row div[class^='col'] {
padding: 10px;
}
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/js/bootstrap.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row" *ngFor="let img of lstImg">
<div class="col-sm-3" *ngIf="img != 'tImg'; else elseBlock">
<img class="img-fluid" src="https://thumbs.dreamstime.com/z/tv-test-image-card-rainbow-multi-color-bars-geometric-signals-retro-hardware-s-minimal-pop-art-print-suitable-89603635.jpg" alt="img">
</div>
<div class="col-sm-6" #elseBlock>
<img class="img-fluid" src="https://thumbs.dreamstime.com/z/tv-test-image-card-rainbow-multi-color-bars-geometric-signals-retro-hardware-s-minimal-pop-art-print-suitable-89603635.jpg" alt="img">
</div>
</div>
</div>
Contain two different sized images on the same row (Option 2)
Adding object-fit: contain; allows the images to fill their container. We'll also need to remove the .img-fluid class
img {
height: 100%;
width: 100%;
object-fit: contain;
}
.row {
background: #e3e3e3; /* let's visualise the row */
}
.row div[class^='col'] {
padding: 10px;
height: 15rem;
}
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/js/bootstrap.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row" *ngFor="let img of lstImg">
<div class="col-sm-3" *ngIf="img != 'tImg'; else elseBlock">
<img src="https://thumbs.dreamstime.com/z/tv-test-image-card-rainbow-multi-color-bars-geometric-signals-retro-hardware-s-minimal-pop-art-print-suitable-89603635.jpg" alt="img">
</div>
<div class="col-sm-6" #elseBlock>
<img src="https://thumbs.dreamstime.com/z/tv-test-image-card-rainbow-multi-color-bars-geometric-signals-retro-hardware-s-minimal-pop-art-print-suitable-89603635.jpg" alt="img">
</div>
</div>
</div>
Images fill their container
Changing to object-fit: cover; allows the images to fill their container.
img {
height: 100%;
width: 100%;
object-fit: cover;
}
.row {
background: #e3e3e3; /* let's visualise the row */
}
.row div[class^='col'] {
padding: 10px;
height: 15rem;
}
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/js/bootstrap.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row" *ngFor="let img of lstImg">
<div class="col-sm-3" *ngIf="img != 'tImg'; else elseBlock">
<img src="https://thumbs.dreamstime.com/z/tv-test-image-card-rainbow-multi-color-bars-geometric-signals-retro-hardware-s-minimal-pop-art-print-suitable-89603635.jpg" alt="img">
</div>
<div class="col-sm-6" #elseBlock>
<img src="https://thumbs.dreamstime.com/z/tv-test-image-card-rainbow-multi-color-bars-geometric-signals-retro-hardware-s-minimal-pop-art-print-suitable-89603635.jpg" alt="img">
</div>
</div>
</div>
More info on the CSS object-fit Property here.
I have 3 images which are not the same size in Desktop format, how do I get the same size? Then in mobile format I would like it to overlap by taking the entire width.
I tried with flex-direction: column but it doesn't work. I use flexbox for my code.
.background-color {
background-color: #f05f40;
}
h2 {
text-align: center;
padding-top: 2%;
margin-top: 0;
}
.row {
display: flex;
width: 100%;
text-align: center;
}
.content {
justify-content: space-around;
padding: 0 10px;
}
img {
height: 200px;
object-fit: cover;
object-position: center center;
}
#media screen and (max-width: 600px) {
.column {
width: 100%;
}
.content {
flex-direction: column;
}
}
<div class="background-color">
<h2 id="projets">Mes Projets</h2>
<div class="row">
<div class="column">
<div class="content">
<img class="img" src="/assets/img/projectImage/pain.jpg" alt="Bred" style="width: 100%;">
<h3>My Work</h3>
<p>Lorem ipsum..</p>
</div>
</div>
<div class="column">
<div class="content">
<img class="img" src="/assets/img/projectImage/catmash.jpg" alt="catmash" style="width: 100%;">
<h3>My Work</h3>
<p>Lorem ipsum..</p>
</div>
</div>
<div class="column">
<div class="content">
<img class="img" src="/assets/img/projectImage/snakgame.jpg" alt="snakegame" style="width: 100%;">
<h3>My Work</h3>
<p>Lorem ipsum..</p>
</div>
</div>
</div>
</div>
You are add flex-direction: column inside .content which doesn't have display: flex. Instead you need to add in .row which wraps all three contents
#media screen and (max-width: 600px) {
.column {
width: 100%;
}
.content {
flex-direction: column;
}
}
Take a look at this:
<!DOCTYPE html>
<html lang="en">
<head>
<title>3 Images responsive</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="thumbnail">
<img src="https://www.w3schools.com/w3images/lights.jpg" alt="Lights" style="width:100%">
</div>
</div>
<div class="col-md-4">
<div class="thumbnail">
<img src="https://www.w3schools.com/w3images/nature.jpg" alt="Nature" style="width:100%">
</div>
</div>
<div class="col-md-4">
<div class="thumbnail">
<img src="https://www.w3schools.com/w3images/fjords.jpg" alt="Fjords" style="width:100%">
</div>
</div>
</div>
</div>
</body>
</html>
You would achieve the same size for all images by setting the width and height to the same value. I normally just resize all images in a image editing software such as Photoshop and then just import them. Saves you the work with sizing in CSS.
I need it to be responsive.. I've tried setting larger image as a div background but it's not responsive.. I end up with the two right images large than the large image... I'm not sure what to do.. I'm using bootstrap, please see my code and any advice would be appreciated.
Also, the two images on the right will display:none on mobile.. so it's not mobile I'm worried about, its from ipad and up that I need it to be responsive and all stay aligned
.large-image {
div {
position: relative;
background-repeat: no-repeat;
background-size: cover;
background-position: center;
width: 100%;
}
}
.large-image-two {
div {
height: 50vw;
#include breakpoint(up, xl) {
height: 43vw;
}
}
#include breakpoint(up, xl) {
max-height: 1100px;
}
}
.tile {
display: none;
#include breakpoint(up, lg) {
display: block;
}
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<section class="image-tile">
<div class="container-fluid padding-sides padding-large pt-0">
<div class="row">
<div class="col-xxl-10 col-lg-9 col-12 large-image large-image-two">
<div class="full-height" style="background-image: url(https://i.picsum.photos/id/847/1370/700.jpg?hmac=kz_PXEVjCOQtig90AKAetNcWNNoJdWMjRMYhOLc6r7w)"></div>
</div>
<div class="col-xxl-2 col-lg-3 col-12 tile">
<div class="row d-flex flex-column full-height">
<div class="top-image col-8">
<img class="full-width" src="https://i.picsum.photos/id/682/250/400.jpg?hmac=ub0-x7bHa0z6PHozDd9A5baHp22hyWuq0NZuhHYmmBE">
</div>
<div class="col-4">
<img class="full-width" src="https://i.picsum.photos/id/545/250/270.jpg?hmac=lfBUvuMl6_uUp1lJI6e4A7KBiB4vzkocSlai_L0fUyE">
</div>
</div>
</div>
</div>
</div>
</section>
I am working with Bootstrap 4 and am trying to have a grid of 4 images, 2 on top and 2 on bottom. I am using the img-fluid class but the image resizes based on the width only making the image height too large and it is getting cut off. I tried setting max-height: 100% but that didn't work.
What's going wrong?
.images-container {
height: 95vh;
}
.nav-button {
width: 100%;
height: 50%;
margin: auto;
font-size: 5em;
color: black;
}
.footer-container {
text-align: center;
}
.bottom-nav-box {
font-size: 1.5em;
margin: 0;
}
.sim-image {
max-height: 100%;
}
i {
margin: auto;
}
body {
height: 100%;
}
html {
height: 100%;
}
footer {
height: 5vh;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="col-sm-10 h-100 center-container">
<div class="row images-container">
<div class="row top-images-row h-50">
<div class="w-50 d-inline-block image-div">
<img src="https://lorempixel.com/875/656/" class="sim-image" id="simulatorImageTopLeft">
</div>
<div class="w-50 d-inline-block image-div" id="simulatorImageTopRight">
<img src="https://lorempixel.com/875/656/" class="img-fluid sim-image" id="simulatorImageTopRight">
</div>
</div>
<div class="row bottom-images-row h-50">
<div class="col image-div center-block text-center">
<img src="https://lorempixel.com/875/656/" class="img-fluid sim-image" id="simulatorImageBottomLeft">
</div>
<div class="col image-div center-block text-center">
<div class="row">
<div class="col center-block text-center">
<img src="https://lorempixel.com/875/656/" class="img-fluid sim-image" id="simulatorImageBottomRight">
</div>
<div class="col center-block text-center">
<img src="https://lorempixel.com/875/656/" class="img-fluid sim-image" id="simulatorImageBottomRight">
</div>
</div>
</div>
</div>
</div>
</div>
Not 100% sure I'm interpreting the question correctly, but if you want a 2×2 grid of images then you should just be able to do it relatively quick like this;
html:
<div class="panel">
<img src="blah" />
<img src="blah" />
<img src="blah" />
<img src="blah" />
</div>
css:
.panel {
width = whateversizeyouwant;
height = whateversizeyouwant;
display: flex;
flex-wrap: wrap; //the flex stuff should make it display 2×2
}
.panel > img {
width: 50%;
height: 50%;
}
That should work for any set of 4 images.
If you want them to stay the same aspect ratios then under .panel > img set height: auto. Keep in mind this won't give you a perfect square, and the size of the panel div will impact how the images can be sized.
JSFiddle
body {
background-color: #111111;
color: #ffffff;
/*max-width: 100%;*/
overflow-x: hidden;
}
.row {
max-width: 100rem;
}
.svgrow {
min-width: 70rem;
}
.svgrow svg {
overflow-x: auto;
}
I want to have this svg horizontal scrollable on small screens, without the body being horizontal scrollable. In addition to that I want only relative units to be used.
I already tried to put the overflow property in different positions, but I can't get it to work.
I use the foundation framework.
In order for the scrolling to happen, you have to give the container a fixed width. The contents (the SVG) needs to have a width that is greater than the container element. Usually that means giving it a specific width, because otherwise it will just resize to its container.
.svgrow {
max-width: 100vw;
overflow-x: auto;
}
.svgrow svg {
min-width: 70rem;
}
/* My CSS */
body {
overflow-x: hidden;
}
.row {
max-width: 100rem;
}
.svgrow {
max-width: 100vw;
overflow-x: auto;
}
.svgrow svg {
min-width: 70rem;
}
svg {
margin-bottom: 2.5em;
}
.off-canvas-toolbar {
position: fixed;
height: 100%;
width: 8%;
}
<div class="container">
<div class="off-canvas position-left" id="sidebar" data-off-canvas>
</div>
<div class="off-canvas-content" data-off-canvas-content>
<div class="off-canvas-toolbar">
<div class="custom-menu-icon" data-toggle="sidebar">
<i class="fa fa-bars">Icon</i>
</div>
</div>
<div class="row svgrow">
<div class="small-12 columns">
<h1>Title</h1>
<svg version="1.2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 2000 1000">
<polygon points="0,1000 60,0 2000,0 2000,1000" fill="#fdedbc"></polygon>
<polygon points="70,1000 970,0 1800,1000" fill="#7c5b18"></polygon>
</svg>
</div>
</div>
<div class="row">
<div id="searchResult" class="small-12 columns" style="">
<h2 class="center">Search Results</h2>
<div class="small-12 medium-6 large-4 columns">
<div class="searchResult">
<div class="caption">
Test <span class="creator">by User</span>
</div>
</div>
</div>
<div class="small-12 medium-6 large-4 columns">
<div class="searchResult">
<div class="caption">
Test <span class="creator">by User</span>
</div>
</div>
</div>
<div class="small-12 medium-6 large-4 columns">
<div class="searchResult">
<div class="caption">
Test <span class="creator">by User</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>