Responsive card grid with full image height on hover - html

I'm hoping to get some advice on creating a responsive card that will display the full height of an image on hover. I've been working on it for the past hour and have come up with a solution, which you can see in my jsfiddle file. However, it's not quite as responsive as I'd like it to be. I'd really appreciate any tips or recommendations you might have.
Here is my take: jsfiddle
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(270px, 1fr));
gap: 2rem;
}
.card {
position: relative;
background: #eee;
display: flex;
flex-direction: column;
height: 420px;
}
.card .thumbnail {
position: relative;
width: 100%;
height: 275px;
}
.card:hover {
z-index: 999;
}
.card:hover .thumbnail {
height: auto;
}
.card:hover .details {
top: 100%;
}
.card .thumbnail img {
width: 100%;
height: 100%;
object-fit: cover;
transition: height 0.3s ease-in-out;
}
.card .details {
display: flex;
flex-direction: column;
position: absolute;
width: 100%;
background: blue;
bottom: 0;
height: 170px;
}
.card .details h1 {}
<div class="card-grid">
<div class="card">
<div class="thumbnail">
<img src="#" alt="Card Thumbnail">
</div>
<div class="details">
<h1>Card Title</h1>
<p>Card description goes here</p>
</div>
</div>
<div class="card">
<div class="thumbnail">
<img src="#" alt="Card Thumbnail">
</div>
<div class="details">
<h1>Card Title</h1>
<p>Card description goes here</p>
</div>
</div>
<div class="card">
<div class="thumbnail">
<img src="#" alt="Card Thumbnail">
</div>
<div class="details">
<h1>Card Title</h1>
<p>Card description goes here</p>
</div>
</div>
<div class="card">
<div class="thumbnail">
<img src="#" alt="Card Thumbnail">
</div>
<div class="details">
<h1>Card Title</h1>
<p>Card description goes here</p>
</div>
</div>
<div class="card">
<div class="thumbnail">
<img src="#" alt="Card Thumbnail">
</div>
<div class="details">
<h1>Card Title</h1>
<p>Card description goes here</p>
</div>
</div>
<div class="card">
<div class="thumbnail">
<img src="#" alt="Card Thumbnail">
</div>
<div class="details">
<h1>Card Title</h1>
<p>Card description goes here</p>
</div>
</div>
<div class="card">
<div class="thumbnail">
<img src="#" alt="Card Thumbnail">
</div>
<div class="details">
<h1>Card Title</h1>
<p>Card description goes here</p>
</div>
</div>
</div>
I am using position: absolute in order to prevent the card height from changing and disrupting the grid layout.
Thank you!

Related

How to make an absolutely positioned element have the same size as another element

I have a layout like this:
body {
text-align: center;
}
.link {
position: relative;
}
.info {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
position: absolute;
top: 0;
text-align: center;
width: 100%;
height: 100%;
background-color: #263238;
color: #1DE9B6;
opacity: 0;
visibility: hidden;
}
.link:hover .info {
opacity: 1;
visibility: visible;
}
.col-md-3 {
margin-bottom: 30px;
}
<div class="container">
<div class="row">
<div class="col-md-3">
<div class="link">
<img class="img-fluid" src="http://image.tmdb.org/t/p/w300/c9XxwwhPHdaImA2f1WEfEsbhaFB.jpg" alt="img">
<div class="info">Info</div>
</div>
</div>
<div class="col-md-3">
<div class="link">
<img class="img-fluid" src="http://image.tmdb.org/t/p/w300/c9XxwwhPHdaImA2f1WEfEsbhaFB.jpg" alt="img">
<div class="info">Info</div>
</div>
</div>
<div class="col-md-3">
<div class="link">
<img class="img-fluid" src="http://image.tmdb.org/t/p/w300/c9XxwwhPHdaImA2f1WEfEsbhaFB.jpg" alt="img">
<div class="info">Info</div>
</div>
</div>
<div class="col-md-3">
<div class="link">
<img class="img-fluid" src="http://image.tmdb.org/t/p/w300/c9XxwwhPHdaImA2f1WEfEsbhaFB.jpg" alt="img">
<div class="info">Info</div>
</div>
</div>
<div class="col-md-3">
<div class="link">
<img class="img-fluid" src="http://image.tmdb.org/t/p/w300/c9XxwwhPHdaImA2f1WEfEsbhaFB.jpg" alt="img">
<div class="info">Info</div>
</div>
</div>
<div class="col-md-3">
<div class="link">
<img class="img-fluid" src="http://image.tmdb.org/t/p/w300/c9XxwwhPHdaImA2f1WEfEsbhaFB.jpg" alt="img">
<div class="info">Info</div>
</div>
</div>
<div class="col-md-3">
<div class="link">
<img class="img-fluid" src="http://image.tmdb.org/t/p/w300/c9XxwwhPHdaImA2f1WEfEsbhaFB.jpg" alt="img">
<div class="info">Info</div>
</div>
</div>
<div class="col-md-3">
<div class="link">
<img class="img-fluid" src="http://image.tmdb.org/t/p/w300/c9XxwwhPHdaImA2f1WEfEsbhaFB.jpg" alt="img">
<div class="info">Info</div>
</div>
</div>
</div>
</div>
When the layout gets small, and only one column is displayed horizontally, the div which is supposed to show on hover gets as big as the column, but I only want it to be as big as the fluid image. How would I need to achieve this?
Here is the codepen link: https://codepen.io/Kestvir/pen/gKVwLe
Just add display: inline-block to the link element
.link {
position: relative;
display: inline-block;
}
Or you can also add d-inline-block position-relative classes on the .link elements .
body {
text-align: center;
}
.link {
position: relative;
display: inline-block;
}
.info {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
position: absolute;
top: 0;
text-align: center;
width: 100%;
height: 100%;
background-color: #263238;
color: #1DE9B6;
opacity: 0;
visibility: hidden;
}
.link:hover .info {
opacity: 1;
visibility: visible;
}
.col-md-3 {
margin-bottom: 30px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-md-3">
<div class="link">
<img class="img-fluid" src="http://image.tmdb.org/t/p/w300/c9XxwwhPHdaImA2f1WEfEsbhaFB.jpg" alt="img">
<div class="info">Info</div>
</div>
</div>
<div class="col-md-3">
<div class="link">
<img class="img-fluid" src="http://image.tmdb.org/t/p/w300/c9XxwwhPHdaImA2f1WEfEsbhaFB.jpg" alt="img">
<div class="info">Info</div>
</div>
</div>
<div class="col-md-3">
<div class="link">
<img class="img-fluid" src="http://image.tmdb.org/t/p/w300/c9XxwwhPHdaImA2f1WEfEsbhaFB.jpg" alt="img">
<div class="info">Info</div>
</div>
</div>
<div class="col-md-3">
<div class="link">
<img class="img-fluid" src="http://image.tmdb.org/t/p/w300/c9XxwwhPHdaImA2f1WEfEsbhaFB.jpg" alt="img">
<div class="info">Info</div>
</div>
</div>
<div class="col-md-3">
<div class="link">
<img class="img-fluid" src="http://image.tmdb.org/t/p/w300/c9XxwwhPHdaImA2f1WEfEsbhaFB.jpg" alt="img">
<div class="info">Info</div>
</div>
</div>
<div class="col-md-3">
<div class="link">
<img class="img-fluid" src="http://image.tmdb.org/t/p/w300/c9XxwwhPHdaImA2f1WEfEsbhaFB.jpg" alt="img">
<div class="info">Info</div>
</div>
</div>
<div class="col-md-3">
<div class="link">
<img class="img-fluid" src="http://image.tmdb.org/t/p/w300/c9XxwwhPHdaImA2f1WEfEsbhaFB.jpg" alt="img">
<div class="info">Info</div>
</div>
</div>
<div class="col-md-3">
<div class="link">
<img class="img-fluid" src="http://image.tmdb.org/t/p/w300/c9XxwwhPHdaImA2f1WEfEsbhaFB.jpg" alt="img">
<div class="info">Info</div>
</div>
</div>
</div>
</div>

How to put an image below another image?

I want to set images below one another like this
but these images displaying like this
Here is my html code with css. In code, i used columns but problem is not solved. Images is not displaying as i want to display.
<div class="row">
<div class="card">
<img alt="Card image cap" class="card-img-top" src="https://i.pinimg.com/736x/04/54/98/045498ad4b9b3cdd4d780fcfcaf3392a--cake-decorating-fondant-spring-cakes-decorating.jpg" >
</div>>
<div class="card">
<img alt="Card image cap" class="card-img-top" src="http://www.sliceofitaly.com/files/product/full/0415BDDC-justformylove.jpg" >
</div>
<div class="card">
<img alt="Card image cap" class="card-img-top" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSmZNLgk07M9ksvjkM8QHxqdFOkxnqFROYgMBu1Q5T-m2jNfrwF" >
</div>
<div class="card">
<img alt="Card image cap" class="card-img-top" src="http://www.fnstatic.co.uk/images/content/recipe/mini-christmas-cakes.jpg" >
</div>
<div class="card">
<img alt="Card image cap" class="card-img-top" src="https://i.pinimg.com/736x/32/6c/19/326c191b3b4a373d0b73189551b4a693--spider-man-cakes-spiders.jpg" >
</div>
<div class="card">
<img alt="Card image cap" class="card-img-top" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSqESHTW7PwOycYQucPrFkEaTPfdsIod7GYqm3fIUNyQUzHGiS0" >
</div>
<div class="card">
<img alt="Card image cap" class="card-img-top" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ0KuVORGDRBMrnC4j4oMm-TU_LmU2JZ0KLeDbD49h5Rt97dXTEEg" >
</div>
<div class="card">
<img alt="Card image cap" class="card-img-top" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ0KuVORGDRBMrnC4j4oMm-TU_LmU2JZ0KLeDbD49h5Rt97dXTEEg" >
</div>
<div class="card">
<img alt="Card image cap" class="card-img-top" src="http://www.fnstatic.co.uk/images/content/recipe/mini-christmas-cakes.jpg" >
</div>
<div class="card">
<img alt="Card image cap" class="card-img-top" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ0KuVORGDRBMrnC4j4oMm-TU_LmU2JZ0KLeDbD49h5Rt97dXTEEg" >
</div>
<div class="card">
<img alt="Card image cap" class="card-img-top" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ0KuVORGDRBMrnC4j4oMm-TU_LmU2JZ0KLeDbD49h5Rt97dXTEEg" >
</div>
</div>
css for images
.card-img-top
{
width: 20%;
height:auto;
margin:5px;
display:inline-block;
float:right;
}
I tried using the column-count attribute on the row-div. Usually it is used to split up big chunks of text into equal columns, but I found it to be working with images aswell!
.row {
/* Prevent vertical gaps */
line-height: 0;
-webkit-column-count: 3;
-webkit-column-gap: 0px;
-moz-column-count: 3;
-moz-column-gap: 0px;
column-count: 3;
column-gap: 0px;
}
.card {
width: 100%;
height: auto;
}
.card-img-top {
width: 100% !important;
height: auto !important;
margin: 5px;
}
This is my demo code, I am using flexBox. see in full mode
Set Box height:auto so it will automatically set height of all Box
<html>
<style>
html{
height: 100%;
}
body{
height: 100%;
}
#wrapper{
height: 100vh;
display: flex;
flex-direction: column;
flex-wrap: wrap;
}
.box{
display: flex;
background: black;
margin: 0.5%;
}
#box-1{
height: 50%;
width: 32%;
}
#box-2{
height: 46%;
width: 32%;
}
#box-3{
height: 20%;
width: 33%;
}
#box-4{
height: 49%;
width: 33%;
}
#box-5{
height: 25%;
width: 33%;
}
#box-6{
height: 20%;
width: 33%;
}
#box-7{
height: 75%;
width: 33%;
}
p{
color: white;
}
</style>
<body>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div id="wrapper">
<div id="box-1" class="box">
<p>Hi</p>
</div>
<div id="box-2" class="box">
</div>
<div id="box-3" class="box">
</div>
<div id="box-4" class="box">
</div>
<div id="box-5" class="box">
</div>
<div id="box-6" class="box">
</div>
<div id="box-7" class="box">
</div>
</div>
</body>
</html>
</body>
</html>
This is example with picture....!
<html>
<style>
html{
height: 100%;
}
body{
height: 100%;
display:flex;
}
#wrapper{
min-height: 100vh;
height: auto;
display: flex;
flex-direction: column;
width:32.3%
}
.box{
display: flex;
background: black;
margin: 0.5%;
width: 100%;
height: auto;
}
.box img{
max-height:50%;
min-height:15%;
width:100%;
}
p{
color: white;
}
</style>
<body>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div id="wrapper">
<div class="box">
<img alt="Card image cap" class="card-img-top" src="https://i.pinimg.com/736x/04/54/98/045498ad4b9b3cdd4d780fcfcaf3392a--cake-decorating-fondant-spring-cakes-decorating.jpg" />
</div>
<div class="box">
<img alt="Card image cap" class="card-img-top" src="http://www.sliceofitaly.com/files/product/full/0415BDDC-justformylove.jpg" />
</div>
<div class="box">
<img alt="Card image cap" class="card-img-top" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSmZNLgk07M9ksvjkM8QHxqdFOkxnqFROYgMBu1Q5T-m2jNfrwF" />
</div>
</div>
<div id="wrapper">
<div class="box">
<img alt="Card image cap" class="card-img-top" src="http://www.fnstatic.co.uk/images/content/recipe/mini-christmas-cakes.jpg" />
</div>
<div class="box">
<img alt="Card image cap" class="card-img-top" src="https://i.pinimg.com/736x/32/6c/19/326c191b3b4a373d0b73189551b4a693--spider-man-cakes-spiders.jpg" />
</div>
<div class="box">
<img alt="Card image cap" class="card-img-top" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSqESHTW7PwOycYQucPrFkEaTPfdsIod7GYqm3fIUNyQUzHGiS0" />
</div>
</div>
<div id="wrapper">
<div class="box">
<img alt="Card image cap" class="card-img-top" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ0KuVORGDRBMrnC4j4oMm-TU_LmU2JZ0KLeDbD49h5Rt97dXTEEg" />
</div>
<div class="box">
<img alt="Card image cap" class="card-img-top" src="https://i.pinimg.com/736x/32/6c/19/326c191b3b4a373d0b73189551b4a693--spider-man-cakes-spiders.jpg" />
</div>
<div class="box">
<img alt="Card image cap" class="card-img-top" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ0KuVORGDRBMrnC4j4oMm-TU_LmU2JZ0KLeDbD49h5Rt97dXTEEg" />
</div>
</div>
</body>
</html>
</body>
</html>

Overlay with words across multiple images

I am making a instagram type of feed and have a 4 images with a further 4 below those 4. I need to make a overlay which I can put words in front of all 8 images showing instagram followers and a link to their instagram.
I have most of it done but the overlay I cannot seem to get it to work. Any help would be appreciated, thanks in advance.
Josh
<ul class="images">
<div class="col-sm-3">
<img src="img/instagram/image-1.jpg" alt="Image Gallery" class="img-responsive">
</div>
<div class="col-sm-3">
<img src="img/instagram/image-2.jpg" alt="Image Gallery" class="img-responsive">
</div>
</ul>
Use o overlay div with position absolute covering the entire images container like the example....
.images{
position:relative;
display:inline-block;
}
.overlay{
position: absolute;
top: 0;
left: 0;
background: rgba(255,255,255,0.5);
width: 100%;
height: 100%;
text-align: center;
display: flex;
align-items: center;
justify-content: center;
visibility: hidden;
}
.images:hover .overlay{
visibility: visible;
}
<div class="images">
<div class="col-sm-3">
<img src="http://via.placeholder.com/350x150" alt="Image Gallery" class="img-responsive">
</div>
<div class="col-sm-3">
<img src="http://via.placeholder.com/350x150" alt="Image Gallery" class="img-responsive">
</div>
<div class='overlay'>
<span>some text</span>
</div>
</div>
Like this? You can put whatever words in the overlay as you want. I used xs columns so you could see it fully.
.overlay{
display:block;
position:absolute;
top:0;
right:0;
bottom:0;
left:0;
background-color:rgba(0,0,0,.5);
z-index:10;
}
.overlay h2{
color:#fff;
}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid">
<div class="overlay">
<h2>Follow me on Insta</h2>
</div>
<div class="row images">
<div class="col-xs-3">
<img src="http://fillmurray.com/200/300" alt="Image Gallery" class="img-responsive">
</div>
<div class="col-xs-3">
<img src="http://fillmurray.com/200/300" alt="Image Gallery" class="img-responsive">
</div>
<div class="col-xs-3">
<img src="http://fillmurray.com/200/300" alt="Image Gallery" class="img-responsive">
</div>
<div class="col-xs-3">
<img src="http://fillmurray.com/200/300" alt="Image Gallery" class="img-responsive">
</div>
</div>
</div>
I think the below is what you need. I used flexboxes for layout.
* {
box-sizing: border-box;
}
.container {
display: flex;
justify-content: flex-start;
flex-flow: wrap;
width: 500px;
}
.item {
display: inline-block;
margin: 0 0.5em 0.5em 0;
position: relative;
}
.text {
opacity: 0;
background-color: rgba(0, 0, 0, 0.6);
position: absolute;
display: flex;
align-items: center;
justify-content: center;
width: 100px;
height: 100px;
top: 0;
}
.text:hover {
border: thin solid red;
animation: opac 0.3s ease forwards;
}
#keyframes opac {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="row-sm-12 container">
<div class="row-sm-3 item">
<img src="http://placehold.it/100">
<div class="text">Bla bla</div>
</div>
<div class="row-sm-3 item">
<img src="http://placehold.it/100">
<div class="text">Bla bla</div>
</div>
<div class="row-sm-3 item">
<img src="http://placehold.it/100">
<div class="text">Bla bla</div>
</div>
<div class="row-sm-3 item">
<img src="http://placehold.it/100">
<div class="text">Bla bla</div>
</div>
<div class="row-sm-3 item">
<img src="http://placehold.it/100">
<div class="text">Bla bla</div>
</div>
<div class="row-sm-3 item">
<img src="http://placehold.it/100">
<div class="text">Bla bla</div>
</div>
<div class="row-sm-3 item">
<img src="http://placehold.it/100">
<div class="text">Bla bla</div>
</div>
<div class="row-sm-3 item">
<img src="http://placehold.it/100">
<div class="text">Bla bla</div>
</div>
</div>

bootstrap columns won't create spaces between them no matter what I try, is it my html structure?

I have searched through almost every question asked about spacing between bootstrap columns but nothing seems to work. I'm thinking it could be a simple rookie mistake with the way I have structured my html/css but I can't seem to figure it out.
I've worked with bootstrap many times before but using a theme, this is my first time however only using a simple bootstrap navbar template with nothing else inside.
.img-container{
position: relative;
display: inline-block;
}
.img-container .overlay{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.1);
}
.img-container:hover .overlay{
display: block;
background-color: rgba(0,0,0,0.6);
}
.img-container .overlay .services-caption{
color: #fff;
font-size: 100px;
position: absolute;
top: 25%;
left: 20%;
}
<!--Services-->
<section class="services">
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="img-container">
<img src="images/services/shop.jpg" alt="...">
<div class="overlay">
<span class="services-caption">Shop</span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="img-container">
<img src="images/services/shop.jpg" alt="...">
<div class="overlay">
<span class="services-caption">Shop</span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="img-container">
<img src="images/services/shop.jpg" alt="...">
<div class="overlay">
<span class="services-caption">Shop</span>
</div>
</div>
</div>
</div>
</div>
</section>
Add bootstrap col-md-offset-* class to achieve spacing between columns.
<div class="col-md-4 col-md-offset-1">
The above code will create a gap of 1 column size.
You can try padding, put test class on each <div class="col-md-4 test">
<style>
.test {
padding-bottom: 10px;
}
</style>
<div class="col-md-4 test">
<div class="img-container">
<img src="images/services/shop.jpg" alt="...">
<div class="overlay">
<span class="services-caption">Shop</span>
</div>
</div>
</div>
You can add a class called img-responsive. It will make the image responsively.
<div class="col-md-4">
<div class="img-container">
<img src="images/services/shop.jpg" class="img-responsive" alt="...">
<div class="overlay">
<span class="services-caption">Shop</span>
</div>
</div>
</div>

How do I center align a div's content with content elements floated towards the left?

I'm trying to build a simple image gallery where the gallery items are placed in a div. But since the items are floated left, they don't center align overall when I add text-align: center; to the parent div. (I'm using Bootstrap 3 and parent div is panel-body. The contents lie in panel-body.)
CSS:
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<style type="text/css" media="screen">
.image-box {
float: left;
}
.image-box img {
width: 128px;
height: 128px;
}
</style>
HTML:
<div class="panel panel-default">
<div class="panel-heading text-center">
<h3 class="panel-title">GALLERY</h3>
</div>
<div class="panel-body">
<div class="image-box">
<img src="member-icon.png">
<P class="caption">Image Description</P>
</div>
<div class="image-box">
<img src="member-icon.png">
<P class="caption">Image Description</P>
</div>
<div class="image-box">
<img src="member-icon.png">
<P class="caption">Image Description</P>
</div>
</div>
</div>
text-align: center; will not work when use floating. use inline block.
Please see code below
.panel-body {
text-align: center;
}
.image-box {
display: inline-block;
margin-left: -4px;
}
.image-box img {
width: 128px;
height: 128px;
}
<div class="panel panel-default">
<div class="panel-heading text-center">
<h3 class="panel-title">GALLERY</h3>
</div>
<div class="panel-body">
<div class="image-box">
<img src="http://placehold.it/300x300">
<P class="caption">Image Description</P>
</div>
<div class="image-box">
<img src="http://placehold.it/300x300">
<P class="caption">Image Description</P>
</div>
<div class="image-box">
<img src="http://placehold.it/300x300">
<P class="caption">Image Description</P>
</div>
</div>
</div>
Here is a live demo
HTML file:
<div class="panel panel-default">
<div class="panel-heading text-center">
<h3 class="panel-title">GALLERY</h3>
</div>
<div class="panel-body">
<div class="col-md-offset-4 col-xs-offset-3">
<div class="image-box">
<img src="member-icon.png">
<p class="caption">Image Description</p>
</div>
<div class="image-box">
<img src="member-icon.png">
<p class="caption">Image Description</p>
</div>
<div class="image-box">
<img src="member-icon.png">
<p class="caption">Image Description</p>
</div>
</div>
</div>
</div>
CSS file:
.image-box {
float: left;
}
.image-box img {
width: 128px;
height: 128px;
padding:15px;
}
See Demo: Div content center in panel body