Vertically center text on an image - dynamically - html

I have images and a text that is displayed in the center of the image when somebody is hovering over the image.
HTML looks like this:
<article>
<div class="entry-content">
<a href="http://www.linktopost.com">
<h3 class="entry-title">Ring #1</h3>
<img width="620" height="387" src="http://i.telegraph.co.uk/multimedia/archive/02725/scotch-whisky_2725818b.jpg" class="aligncenter" alt="Platzhalter_3">
</a>
</div>
</article>
CSS:
article {
float:left;
width:30%;
display:block;
}
.entry-content {
width: 620px;
margin: 0 auto;
position: relative;
height: 387px;
}
.entry-content:hover .entry-title {
color: #000;
display: table-cell;
background-color: #fff;
opacity: 0.75;
}
.entry-title {
position: absolute;
width: 100%;
height: 100%;
z-index: 9999;
line-height: 387px;
text-align: center;
display: none;
}
article img {
position:absolute;
}
You can see it here:
http://codepen.io/anon/pen/rOXOez
Is there any chance to not use fixed pixel values in the CSS - so that the hover effect is valid and working for any picture? In this example I had to use the width and height of the picture in the CSS to achieve what I wanted.
Thank you!

Solution 1:
Relative/Absolute positioning and center with transform: translate()
https://jsfiddle.net/94efk8kz/
article {
position: relative;
}
.hover-content {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: white;
color: black;
opacity: 0;
-webkit-transition: all 0.3s ease-in-out;
-moz-transition: all 0.3s ease-in-out;
-o-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
}
.hover-content h3 {
position: absolute;
top: 50%;
left: 50%;
margin: 0;
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
Solution 2:
Flexbox
https://jsfiddle.net/94efk8kz/1/
article {
position: relative;
}
img {
width: 100%;
}
.hover-content {
width: 100%;
height: 100%;
top: 0;
display: flex;
position: absolute;
align-items: center;
justify-content: center;
background: white;
color: black;
opacity: 0;
-webkit-transition: all 0.3s ease-in-out;
-moz-transition: all 0.3s ease-in-out;
-o-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
}
Solution 3
CSS Tables
https://jsfiddle.net/94efk8kz/2/
.entry-content a {
float: left;
width:30%;
position: relative;
height: 100%;
}
.hover-content {
width: 100%;
height: 100%;
top: 0;
left: 0;
position: absolute;
background: white;
color: black;
opacity: 0;
bottom: 0;
right: 0;
-webkit-transition: all 0.3s ease-in-out;
-moz-transition: all 0.3s ease-in-out;
-o-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
}
.v-align {
display: table;
height: 100%;
margin: 0 auto;
}
.hover-content h3 {
display: table-cell;
vertical-align: middle;
}

Try with these changes
.entry-content {
width: initial;
}
.entry-content:hover .entry-title {
margin: 0;
}
.article img {
display: block;
/* position: absolute; remove this,avoid using positon absolute where you can */
width: 100%;
}

Related

How to proceed in my case to display text over the image in CSS?

I have the following CSS sheet for a responsive flip card that is displaying two background images on both sides. I am trying to get text on top of them, but it is not showing in the browser for some reason. Here is the CSS:
.container {
padding: 1em;
margin: 0px auto;
max-width: 100%;
text-align: center;
-webkit-perspective: 800px;
perspective: 800px;
}
.card {
width: 90%;
border-radius:20px;
height: 170px;
margin: 10 auto;
position: relative;
-webkit-transition: all 0.4s ease-out;
-moz-transition: all 0.4s ease-out;
transition: all 0.4s ease-out;
-webkit-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
transform-style: preserve-3d;
}
.face:after {
position: absolute;
top: 100%;
left: 0;
z-index: -1;
display: block;
content: '';
height: 22px;
width: 100%;
background: url('http://media.a-g.fr/crea/talenance/shadow.png') no-repeat;
background-size: 100%;
}
.face {
border-radius:20px;
position: absolute;
width: 100%;
height: 100%;
text-align: center;
line-height: 300px;
color: white;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
transition: 0.3s all ease-in-out;
-webkit-transition: 0.3s all ease-in-out;
-moz-transition: 0.3s all ease-in-out;
}
.front {
background: url(https://s3.amazonaws.com/uifaces/faces/twitter/eomerx/128.jpg) center center;
background-size:cover;
z-index: 2;
text-align: center;
font-size: 4em;
position: absolute;
}
.back {
z-index: 1;
background-color: #28255f;
transform:rotateY(180deg);
}
.back a {
line-height: 250px;
font-size: 14px;
font-weight: 700;
text-decoration: none;
width: 100%;
height: 50%;
text-align: center;
color: black;
display: block;
transition: 0.3s all ease-in-out;
-webkit-transition: 0.3s all ease-in-out;
-moz-transition: 0.3s all ease-in-out;
}
.back a:before {
content:"";
background-size:cover;
position: absolute;
width:50px;
height: 50px;
left:0;
right:0;
margin:0 auto;
filter:invert(100%) brightness(150%);
-webkit-filter:invert(100%) brightness(150%);
}
.back a:nth-of-type(1):before {
background:url(http://media.a-g.fr/crea/talenance/linkedin.png) no-repeat center center;
top:30px;
}
.back a:nth-of-type(2):before {
background:url(http://media.a-g.fr/crea/talenance/email.png) no-repeat center center;
bottom:30px;
}
.back a:hover {
opacity:0.5;
}
.card:hover .front {
transform:rotateY(-180deg);
}
.card:hover .back {
transform:rotateY(0deg);
}
I am trying to insert text there on top of the background image, but I am not sure why this HTML doesn't work:
<div class="container">
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12 cardContainer">
<div class="card" id="card">
<div class="face front">
TEXT HERE
</div>
<div class="face back">
</div>
</div>
</div>
</div>
Thank for your help!
Add line-height:1; to class front or remove line-height: 300px; from class face, depending on your use for class face elsewhere.
.container {
padding: 1em;
margin: 0px auto;
max-width: 100%;
text-align: center;
-webkit-perspective: 800px;
perspective: 800px;
}
.card {
width: 90%;
border-radius:20px;
height: 170px;
margin: 10 auto;
position: relative;
-webkit-transition: all 0.4s ease-out;
-moz-transition: all 0.4s ease-out;
transition: all 0.4s ease-out;
-webkit-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
transform-style: preserve-3d;
}
.face:after {
position: absolute;
top: 100%;
left: 0;
z-index: -1;
display: block;
content: '';
height: 22px;
width: 100%;
background: url('http://media.a-g.fr/crea/talenance/shadow.png') no-repeat;
background-size: 100%;
}
.face {
border-radius:20px;
position: absolute;
width: 100%;
height: 100%;
text-align: center;
line-height: 300px;
color: white;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
transition: 0.3s all ease-in-out;
-webkit-transition: 0.3s all ease-in-out;
-moz-transition: 0.3s all ease-in-out;
}
.front {
background: url(https://s3.amazonaws.com/uifaces/faces/twitter/eomerx/128.jpg) center center;
background-size:cover;
z-index: 2;
text-align: center;
font-size: 4em;
position: absolute;
line-height:1;
}
.back {
z-index: 1;
background-color: #28255f;
transform:rotateY(180deg);
}
.back a {
line-height: 250px;
font-size: 14px;
font-weight: 700;
text-decoration: none;
width: 100%;
height: 50%;
text-align: center;
color: black;
display: block;
transition: 0.3s all ease-in-out;
-webkit-transition: 0.3s all ease-in-out;
-moz-transition: 0.3s all ease-in-out;
}
.back a:before {
content:"";
background-size:cover;
position: absolute;
width:50px;
height: 50px;
left:0;
right:0;
margin:0 auto;
filter:invert(100%) brightness(150%);
-webkit-filter:invert(100%) brightness(150%);
}
.back a:nth-of-type(1):before {
background:url(http://media.a-g.fr/crea/talenance/linkedin.png) no-repeat center center;
top:30px;
}
.back a:nth-of-type(2):before {
background:url(http://media.a-g.fr/crea/talenance/email.png) no-repeat center center;
bottom:30px;
}
.back a:hover {
opacity:0.5;
}
.card:hover .front{
transform:rotateY(-180deg);
}
.card:hover .back{
transform:rotateY(0deg);
}
<div class="container">
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12 cardContainer">
<div class="card" id="card">
<div class="face front">
TEXT HERE
</div>
<div class="face back">
</div>
</div>
</div>
Remove the line-height from .face selector.
Final css:
.container {
padding: 1em;
margin: 0px auto;
max-width: 100%;
text-align: center;
-webkit-perspective: 800px;
perspective: 800px;
}
.card {
width: 90%;
border-radius:20px;
height: 170px;
margin: 10 auto;
position: relative;
-webkit-transition: all 0.4s ease-out;
-moz-transition: all 0.4s ease-out;
transition: all 0.4s ease-out;
-webkit-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
transform-style: preserve-3d;
}
.face:after {
position: absolute;
top: 100%;
left: 0;
z-index: -1;
display: block;
content: '';
height: 22px;
width: 100%;
background: url('http://media.a-g.fr/crea/talenance/shadow.png') no-repeat;
background-size: 100%;
}
.face {
border-radius:20px;
position: absolute;
width: 100%;
height: 100%;
text-align: center;
/*line-height: 300px;*/
color: white;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
transition: 0.3s all ease-in-out;
-webkit-transition: 0.3s all ease-in-out;
-moz-transition: 0.3s all ease-in-out;
}
.front {
background: url(https://s3.amazonaws.com/uifaces/faces/twitter/eomerx/128.jpg) center center;
background-size:cover;
z-index: 2;
text-align: center;
font-size: 4em;
position: absolute;
}
.back {
z-index: 1;
background-color: #28255f;
transform:rotateY(180deg);
}
.back a {
line-height: 250px;
font-size: 14px;
font-weight: 700;
text-decoration: none;
width: 100%;
height: 50%;
text-align: center;
color: black;
display: block;
transition: 0.3s all ease-in-out;
-webkit-transition: 0.3s all ease-in-out;
-moz-transition: 0.3s all ease-in-out;
}
.back a:before {
content:"";
background-size:cover;
position: absolute;
width:50px;
height: 50px;
left:0;
right:0;
margin:0 auto;
filter:invert(100%) brightness(150%);
-webkit-filter:invert(100%) brightness(150%);
}
.back a:nth-of-type(1):before {
background:url(http://media.a-g.fr/crea/talenance/linkedin.png) no-repeat center center;
top:30px;
}
.back a:nth-of-type(2):before {
background:url(http://media.a-g.fr/crea/talenance/email.png) no-repeat center center;
bottom:30px;
}
.back a:hover {
opacity:0.5;
}
.card:hover .front{
transform:rotateY(-180deg);
}
.card:hover .back{
transform:rotateY(0deg);
}
Demo: https://jsfiddle.net/v5g9eg1e/
CSS I hope this is what you are looking For.
.container {
padding: 1em;
margin: auto;
max-width: 100%;
text-align: center;
-webkit-perspective: 800px;
perspective: 800px;
}
.card {
width: 90%;
border-radius:20px;
height: 170px;
margin: 10 auto;
position: relative;
-webkit-transition: all 0.4s ease-out;
-moz-transition: all 0.4s ease-out;
transition: all 0.4s ease-out;
-webkit-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
transform-style: preserve-3d;
}
.face:after {
position: absolute;
top: 100%;
left: 0;
z-index: -1;
display: block;
content: '';
height: 22px;
width: 100%;
background: url('http://media.a-g.fr/crea/talenance/shadow.png') no-repeat;
background-size: 100%;
}
.face {
border-radius:20px;
position: absolute;
width: 100%;
height: 100%;
text-align: center;
line-height: 300px;
color: white;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
transition: 0.3s all ease-in-out;
-webkit-transition: 0.3s all ease-in-out;
-moz-transition: 0.3s all ease-in-out;
}
.front {
background: url(https://s3.amazonaws.com/uifaces/faces/twitter/eomerx/128.jpg) center center;
background-size:cover;
z-index: 2;
text-align: center;
font-size: 4em;
position: absolute;
}
.back {
z-index: 1;
background-color: #28255f;
transform:rotateY(180deg);
}
.back a {
line-height: 250px;
font-size: 14px;
font-weight: 700;
text-decoration: none;
width: 100%;
height: 50%;
text-align: center;
color: black;
display: block;
transition: 0.3s all ease-in-out;
-webkit-transition: 0.3s all ease-in-out;
-moz-transition: 0.3s all ease-in-out;
}
.back a:before {
content:"";
background-size:cover;
position: absolute;
width:50px;
height: 50px;
left:0;
right:0;
margin:0 auto;
filter:invert(100%) brightness(150%);
-webkit-filter:invert(100%) brightness(150%);
}
.back a:nth-of-type(1):before {
background:url(http://media.a-g.fr/crea/talenance/linkedin.png) no-repeat center center;
top:30px;
}
.back a:nth-of-type(2):before {
background:url(http://media.a-g.fr/crea/talenance/email.png) no-repeat center center;
bottom:30px;
}
.back a:hover {
opacity:0.5;
}
.card:hover .front {
transform:rotateY(-180deg);
}
.card:hover .back {
transform:rotateY(0deg);
}
#text {
text-align: center}
HTML
<div id="text">
<h2>TEXT HERE<h2>
</div>
<div class="container">
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12 cardContainer">
<div class="card" id="card">
<div class="face front">
</div>
<div class="face back">
</div>
</div>
</div>

Slanted Div Hover Troubles

I've created a Slanted Div, however I ran into problem I cannot solve, I've googled this but did not find any answers.
body {
background: black;
}
#slantedwrapper {
overflow: hidden;
margin-left: 50px;
}
#slanted {
display: inline-block;
/* margin-right:-4px; */
width: 400px;
margin-left: -45px;
/* background-image: url("http://www.keenthemes.com/preview/conquer/assets/plugins/jcrop/demos/demo_files/image2.jpg"); */
}
#slanted a {
position: relative;
background-color: #1d1d1d;
/* background-image: url("http://www.keenthemes.com/preview/conquer/assets/plugins/jcrop/demos/demo_files/image2.jpg"); */
box-sizing: border-box;
background-size: cover;
/* padding:1em; */
display: block;
transform: skewX(-30deg);
width: 100%;
min-height: 3.5em;
text-align: center;
border-right: 5px solid #20c397;
height: 150px;
/* line-height: 110px; */
overflow: hidden;
}
#slanted span {
color: white;
position: absolute;
box-sizing: border-box;
transform: skewX(30deg);
left: 0;
width: 100%;
/* height: 150px; */
/* background-image: url("http://www.keenthemes.com/preview/conquer/assets/plugins/jcrop/demos/demo_files/image2.jpg"); */
}
}
}
.current a {
background:#70cb00;
}
#slanted a img {
transform: skewX(30deg);
overflow: hidden;
margin-top: -20px;
padding-top: 0px;
width: 123%;
height: 123%;
margin-left: -50px;
opacity: 0.6;
-webkit-transition: opacity 0.3s linear 0s;
-o-transition: opacity 0.3s linear 0s;
transition: opacity 0.3s linear 0s;
}
#slanted img:hover {
opacity:1;
}
#caption {
background-color: #333333;
width: 100%;
height: 25px;
display: block;
position: absolute;
bottom: 0;
z-index: 99;
opacity: 0.7;
color: #D2D2D2;
-webkit-transition: background-color 0.3s linear 0s;
-o-transition: background-color 0.3s linear 0s;
transition: background-color 0.3s linear 0s;
}
/*Combination hover effects*/
#slanted:hover #caption {
background-color: #20c397;
opacity:1.0;
}
#slanted:hover img {
opacity:1.0;
}
/* END OFCombo hover effects*/
p.nonskew {
transform: skewX(30deg);
color: White;
margin: 0;
margin-left: 22%;
padding: 1.5%;
text-align: left;
font-size: 0.8em;
}
<div id="slantedwrapper">
<div id="slanted">
<a href="#">
<div id="caption">
<p class="nonskew">A Caption: Description</p>
</div>
<img src="http://www.keenthemes.com/preview/conquer/assets/plugins/jcrop/demos/demo_files/image2.jpg" alt="SLANTED DIV"></a>
</div>
<!--end of wrapper-->
</div>
JSFiddle version
here's the problem:
Hover over the div, it hovers fine, but at the bottom right corner, where nothing is there (where the overflow is hidden) still hovers if you place your mouse over the blank area where the angle begins, how do I solve this into when it hovers- it only applies to shape of the div only?
Thank you
You seem to have the right idea, using both the unskew and intuitive to using the skew, however, something like the below example may work for you:
html {
background: radial-gradient(#222, blue);
height: 100%;
}
div.wrap{
height: 150px;
width: 300px; position: relative;
overflow: hidden;
}
div.innerwrap {
height: 100%;
width: 100%;
transform: skewX(-30deg);
position: absolute;top:0;left:0;
overflow: hidden;
margin-left: -70px;
transition: all 0.4s;
border-right: 5px solid tomato;
cursor:pointer;
}
div.innerwrap:hover span {
background: gold;
}
div.innerwrap:before {
content: "";
position: absolute;
top: 0;
right: 0;
width: 100%;
height: 100%;
background: url(http://lorempixel.com/300/300);
transform: skewX(30deg);
transform-origin: top left;
}
div span {
position: absolute;
bottom: 0;
left: 0%;
width: 120%;
transform: skewX(30deg);
background: red;
text-align:center;
transition: all 0.4s;
}
<div class="wrap">
<div class="innerwrap">
<span>TITLE</span>
</div>
</div>
For further information, #Harry has created a wide variety of examples here in which you may find useful.

Hover over image top and bottom with different effect

I have an image and I try to make the top 50% show a different colour and text when hover over. And the same for the lower 50% of the height of the image.
I came so far to get the below 50% but its all over the page, not just the image, and the top 50% doesnt show. Is it possible to achieve my goal?
BOOTPLY... BOOTPLY
/* CSS used here will be applied after bootstrap.css */
.image img {
display: block;
}
.thumb-wrap img {
width: 50%;
height: auto;
}
.thumb-wrap:hover .thumb-caption {
opacity: 0.7;
}
.thumb-caption {
position: absolute;
left: 0px;
bottom: 0;
width: 100%;
height: 50%;
background-color: #000;
margin: 0;
z-index: 1;
text-align: center;
opacity: 0;
-webkit-transition: all, .5s;
-moz-transition: all, .5s;
-o-transition: all, .5s;
-ms-transition: all, .5s;
transition: all, .5s;
}
/*.thumb-caption-above {
position: absolute;
left: 0px;
top: 0;
width: 100%;
height: 50%;
background-color:red;
margin: 0;
z-index: 0;
text-align: center;
opacity: 0;
-webkit-transition: all, .5s;
-moz-transition: all, .5s;
-o-transition: all, .5s;
-ms-transition: all, .5s;
transition: all, .5s;
}*/
<div class="image">
<div class="thumb-wrap">
<img src="http://placehold.it/550x150">
<h2 class="thumb-caption"><a href="#">More info
</a></h2>
<h2 class="thumb-caption-above"><a href="#">See larger
</a></h2>
</div></div>
<div id="push"></div>
Try this:
.thumb-wrap {
width: 550px;
position: relative;
}
.thumb-caption:hover {
opacity: 0.7;
}
/* Default styles for hover block */
.thumb-caption {
position: absolute;
left: 0;
bottom: 0;
width: 100%;
height: 50%;
background-color: #000;
margin: 0;
z-index: 1;
text-align: center;
opacity: 0;
transition: all, .5s;
}
/* Alternate positioning and background color */
.thumb-caption.above {
top: 0;
background: red;
}
/* For the text color */
.thumb-caption > a {
color: blue; /* default */
}
.thumb-caption.above > a {
color: yellow; /* alternate */
}
<div class="image">
<div class="thumb-wrap">
<img src="http://placehold.it/550x150">
<h2 class="thumb-caption">More info</h2>
<h2 class="thumb-caption above">See larger</h2>
</div>
</div>
The important part for positioning was adding position: relative on your .thumb-wrap container element. I removed the CSS browser prefixes for brevity, but you could add them back in.
The following example shows up both alternatives (info and zoom) at the same time, with the one immediately on hover highlighted.
* {
font-size:1.05em;
color: white;
font-family: arial;
}
#image {
width:550px;
height:150px;
position:relative;
background: url('http://i.imgur.com/HNj6tRD.jpg');
background-repeat: no-repeat;
background-size:100% 100%;
}
.coverUP {
width: 100%;
height: 50%;
position:absolute;
top:0%;
}
.coverDOWN {
width: 100%;
height: 50%;
position:absolute;
top:50%;
}
.coverUP:hover::after {
background: #cc99ff;
opacity: 0.6;
pointer-events: none;
transition: all, 0.6s;
}
.coverDOWN:hover::before {
background: #cc99ff;
opacity: 0.6;
pointer-events: none;
transition: all, 0.6s;
}
.coverUP:hover::before {
background: purple;
opacity: 0.8;
pointer-events: none;
transition: all, 0.6s;
}
.coverDOWN:hover::after {
background: purple;
opacity: 0.8;
pointer-events: none;
transition: all, 0.6s;
}
.coverUP::after {
content: "\A ZOOM";
white-space: pre;
text-align:center;
width: 100%;
height: 100%;
background: #cc99ff;
position:absolute;
top:100%;
opacity: 0.0;
pointer-events: none;
transition: all, 0.6s;
}
.coverDOWN::before {
content: "\A INFO";
white-space: pre;
text-align:center;
width: 100%;
height: 100%;
background: #cc99ff;
position:absolute;
top:-100%;
opacity: 0.0;
pointer-events: none;
transition: all, 0.6s;
}
.coverUP::before {
content: "\A INFO";
white-space: pre;
text-align:center;
width: 100%;
height: 100%;
background: purple;
position:absolute;
top:0%;
opacity: 0.0;
pointer-events: none;
transition: all, 0.6s;
}
.coverDOWN::after {
content: "\A ZOOM";
white-space: pre;
text-align:center;
width: 100%;
height: 100%;
background: purple;
position:absolute;
top:0%;
opacity: 0.0;
pointer-events: none;
transition: all, 0.6s;
}
<div id="image" alt=image>
<a href="#">
<div class="coverUP"></div>
</a>
<a href="#">
<div class="coverDOWN"></div>
</a>
</div>

Unable to set height of hover overlay to 100% of container

I'm using the Instafeed.js plugin and trying to style the "likes" hover overlay to fill the entire image box, however, I'm struggling to set the height to 100%.
I'm trying to avoid setting the container's height to a pixel value since the entire plugin is currently responsive.
I've looked around and tried different combinations of display and position values, but it's been mostly trial and error.
CSS:
#instafeed {
width: 100%;
margin-bottom: 80px;
}
#instafeed a {
position: relative;
}
#instafeed .ig-photo {
width: 25%;
vertical-align: middle;
}
#instafeed .likes {
width: 100%;
height: auto;
top: 0;
left: 0;
right: 0;
position: absolute;
background: #f18a21;
opacity: 0;
font-family: 'LinotypeUniversW01-Thin_723604', Arial, sans-serif;
font-size: 28px;
color: #ffffff;
line-height: 100%;
text-align: center;
text-shadow: 0 1px rgba(0, 0, 0, 0.5);
-webkit-font-smoothing: antialiased;
-webkit-transition: opacity 100ms ease;
-moz-transition: opacity 100ms ease;
-o-transition: opacity 100ms ease;
-ms-transition: opacity 100ms ease;
transition: opacity 100ms ease;
}
#instafeed a:hover .likes {
opacity: 0.8;
}
Demo: http://jsfiddle.net/rc1wj5t9/
Any help/advice would be appreciated!
It's because the anchor elements are inline by default, which means that they are not inheriting the height of their children img elements.
One possible solution is to set the display of the anchor elements to inline-block, and specify a width of 25%. Then for the children img elements, set a max-width of 100%:
Updated Example
#instafeed a {
position: relative;
display: inline-block;
max-width: 25%;
}
#instafeed .ig-photo {
max-width: 100%;
vertical-align: middle;
}
#instafeed .likes {
width: 100%;
height: auto;
top: 0; right: 0;
bottom: 0; left: 0;
}
To center the text, I used one of the techniques for vertically/horizontally centering text from one of my previous answers.
#instafeed .likes > span {
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
white-space: nowrap;
}
Here is how I fixed it.
CSS
#instafeed {
width: 100%;
margin:0px;
}
#instafeed a {
position: relative;
display:inline-block;
float:left;
width: 25%;
}
#instafeed .ig-photo {
width: 100%;
vertical-align: middle;
}
#instafeed .likes {
position: absolute;
width: 100%;
opacity: 0;
font-family: 'LinotypeUniversW01-Thin_723604', Arial, sans-serif;
font-size: 28px;
color: #ffffff;
text-align: center;
top: 50%;
transform: translateY(-50%);
text-shadow: 0 1px rgba(0,0,0,0.5);
-webkit-font-smoothing: antialiased;
-webkit-transition: opacity 100ms ease;
-moz-transition: opacity 100ms ease;
-o-transition: opacity 100ms ease;
-ms-transition: opacity 100ms ease;
transition: opacity 100ms ease;
z-index:10;
}
#instafeed a:hover .likes {
opacity: 1;
}
#instafeed a:hover::after{
content:"";
position:absolute;
width: 100%;
height: 100%;
top: 0; left: 0;
background: #f18a21;
opacity:0.7;
z-index: 5;
}
updated fiddle

How do I vertically center caption on img hover?

I am currently having problems centering my caption inside a div upon hover. The image inside the div is bigger than the div and I want the caption to be vertically centered inside that div?
HTML
<div class="container-fluid">
<div id="page">
<!-- GRID ITEM -->
<div class="item s1">
<a href="#">
<div class="grid-image">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/4e/Pleiades_large.jpg/1024px-Pleiades_large.jpg">
</div>
<div class="item-caption">
<h5>I want this to be center</h5>
</div>
</a>
</div>
<!-- /GRID ITEM -->
</div>
</div>
CSS
#page .item {
width: calc(16.66% - 10px);
display: inline-block;
height: 0;
float: left;
padding-bottom: 16.66%;
overflow: hidden;
background-color: salmon;
margin: 5px;
position: relative;
}
#page .item.s1 {
width: calc(50% - 10px);
padding-bottom: 50%;
overflow: hidden;
background-color: navy;
}
.item > a {
position: relative;
display: block;
overflow: hidden;
color: white;
}
.item:hover .grid-image:after {
background: rgba(0, 0, 0, .7);
}
.item:hover .grid-image > img {
-webkit-transform: scale(1.1);
-moz-transform: scale(1.1);
-ms-transform: scale(1.1);
-o-transform: scale(1.1);
transform: scale(1.1);
}
.item:hover .item-caption {
opacity: 1;
z-index: 3;
visibility: visible;
}
.item-caption,
.grid-image > img,
.grid-image:after {
-webkit-transition: all 0.3s ease-in-out 0s;
-moz-transition: all 0.3s ease-in-out 0s;
-ms-transition: all 0.3s ease-in-out 0s;
-o-transition: all 0.3s ease-in-out 0s;
transition: all 0.3s ease-in-out 0s;
}
.item-caption {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: rgba(29, 106, 154, 0.72);
color: #fff;
visibility: hidden;
text-align: center;
opacity: 0;
}
.grid-image {
position: relative;
overflow: hidden;
}
.grid-image img {
display: block;
overflow: hidden;
}
.grid-image:after {
position: absolute;
display: block;
content: "";
height: 100%;
width: 100%;
top: 0;
left: 0;
}
JavaScript
var $container = $('#page');
$container.masonry({
columnWidth: '.grid-sizer',
itemSelector: '.item',
percentPosition: true,
gutter: 10
});
Here's a fiddle
Thank you!
try this: http://jsfiddle.net/2stywe2t/1/
What I did:
Add a new div to your HTML, right under .item-caption.
Gave .item-caption the display: table attribute and a few others
Gave the new div display: table-cell, and vertical-align: middle
Removed position:relative from the a ancestor
Make sense?
Got the answer !! Just tidy up your code a bit.
var $container = $('#page');
$container.masonry({
columnWidth: '.grid-sizer',
itemSelector: '.item',
percentPosition: true,
gutter: 10
});
#page .item {
width: calc(16.66% - 10px);
display: inline-block;
height: 0;
float: left;
padding-bottom: 16.66%;
overflow: hidden;
background-color: salmon;
margin: 5px;
position: relative;
}
#page .item.s1 {
width: calc(50% - 10px);
padding-bottom: 50%;
overflow: hidden;
background-color: navy;
position: relative!important;
}
.item > a {
position: absolute;
display: block;
width: 100%;
height: 100%;
overflow: hidden;
color: white;
}
.item:hover .grid-image:after {
background: rgba(0, 0, 0, .7);
}
.item:hover .grid-image > img {
-webkit-transform: scale(1.1);
-moz-transform: scale(1.1);
-ms-transform: scale(1.1);
-o-transform: scale(1.1);
transform: scale(1.1);
}
.item:hover .item-caption {
opacity: 1;
z-index: 3;
visibility: visible;
}
.item-caption, .grid-image > img, .grid-image:after {
-webkit-transition: all 0.3s ease-in-out 0s;
-moz-transition: all 0.3s ease-in-out 0s;
-ms-transition: all 0.3s ease-in-out 0s;
-o-transition: all 0.3s ease-in-out 0s;
transition: all 0.3s ease-in-out 0s;
}
.item-caption {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: rgba(29, 106, 154, 0.72);
color: #fff;
visibility: hidden;
text-align: center;
opacity: 0;
display: table;
height: 100%;
width: 100%;
}
.grid-image {
position: relative;
overflow: hidden;
}
.grid-image img {
display: block;
overflow: hidden;
}
.grid-image:after {
position: absolute;
display: block;
content:"";
height: 100%;
width: 100%;
top: 0;
left: 0;
}
h5 {
display: table-cell;
vertical-align: middle;
}
<div class="container-fluid">
<div id="page">
<!-- GRID ITEM -->
<div class="item s1"> <a href="#">
<div class="grid-image">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/4e/Pleiades_large.jpg/1024px-Pleiades_large.jpg">
</div>
<div class="item-caption">
<h5>I want this to be center</h5>
</div>
</a>
</div>
<!-- /GRID ITEM -->
</div>
</div>
</body>
</html>
You simply need to add one line of code to your CSS to make this work:
h5 {
margin-top: 50%;
transform: translateY(-50%); /* not entirely necessary, but makes centering precise */
}
DEMO: http://jsfiddle.net/2stywe2t/4/