CSS3: Center Text (content) in div:before - html

I wrote some code to create a layover with some text. Now I have two problems:
1) I want to center the text.
2) I want to make the layover to be a link.
As I am a CSS newbie, I hope you can give me any advice!
Best regards!
.hover_div {
position:relative;
width:200px;
height:200px;
}
.hover_div img {
width:100%;
vertical-align:top;
}
.hover_div:after {
content: "";
position: absolute;
width:100%; height: 100%;
top: 0; left: 0;
background:rgba(0,0,0,0.6);
opacity:0;
transition: all 0.5s;
-webkit-transition: all 0.5s;
}
.hover_div:before {
content: attr(data-content);
color:#fff;
position:absolute;
opacity:0;
transition: all 0.5s;
-webkit-transition: all 0.5s;
z-index: 1;
}
.hover_div:hover:after {
opacity:1;
}
.hover_div:hover:before {
opacity:1;
}
<div data-content="Elektro" class="hover_div">
<img src="http://i.stack.imgur.com/Sjsbh.jpg" alt="" />
</div>

I've updated your code in the following JSFiddle: https://jsfiddle.net/rvxo7fn5/
I've added the following lines to the :before:
text-align:center;
width: 100%;
margin-top:50%;
transform: translateY(-50%);
Basically, text-align: center; of course centers the text horizontally. However, an absolute positioned div has no width, which is why I added the width: 100%. Next you need to center it vertically. The margin-top: 50%; moves the div 50% of the height of the parent div. transform: translateY(-50%) moves the div back 50% of the height of the div itself. This aligns it in the center of the parent. (50% of parent height - 50% height of child div).
You also mentioned wanting it to be a link. This can be achieved by simply replacing the <div> with a <a> and adding display: block; to your .hover_div class. This gives it the properties a div would also have.
Hope that helps!

Update the :hover style as below.
.hover_div:hover:before
{
opacity:1;
width:100%;
text-align:center;
text-decoration:underline;
cursor:pointer;
}

You can make it link by replacing <div> with <a>.
.hover_div {
position:relative;
display: block;
width:200px;
height:200px;
}
.hover_div img {
width:100%;
vertical-align:top;
}
.hover_div:after {
content: "";
position: absolute;
width:100%; height: 100%;
top: 0; left: 0;
background:rgba(0,0,0,0.6);
opacity:0;
transition: all 0.5s;
-webkit-transition: all 0.5s;
}
.hover_div:before {
content: attr(data-content);
color:#fff;
position:absolute;
text-align: center;
opacity:0;
transition: all 0.5s;
-webkit-transition: all 0.5s;
transform: translateY(-50%);
z-index: 1;
width: 100%;
left: 0;
top: 50%;
}
.hover_div:hover:after {
opacity:1;
}
.hover_div:hover:before {
opacity:1;
}
<a href="#" data-content="Elektro" class="hover_div">
<img src="http://i.stack.imgur.com/Sjsbh.jpg" alt="" />
</a>

The text can be centered by adding left: 50%; top: 50%; transform: translate(-50%, -50%).
Pseudo elements like :after and :before cannot be accessed from the DOM. So a link can't be made out of it.
.hover_div {
position: relative;
width: 200px;
height: 200px;
}
.hover_div img {
width: 100%;
vertical-align: top;
}
.hover_div:after {
content: "";
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
background: rgba(0, 0, 0, 0.6);
opacity: 0;
transition: all 0.5s;
-webkit-transition: all 0.5s;
}
.hover_div:before {
content: attr(data-content);
color: #fff;
position: absolute;
opacity: 0;
transition: all 0.5s;
-webkit-transition: all 0.5s;
z-index: 1;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.hover_div:hover:after {
opacity: 1;
}
.hover_div:hover:before {
opacity: 1;
}
<div data-content="Elektro" class="hover_div">
<img src="http://i.stack.imgur.com/Sjsbh.jpg" alt="" />
</div>

.hover_div {
position:relative;
width:200px;
height:200px;
}
.hover_div img {
width:100%;
vertical-align:top;
}
.hover_div:after {
content: "";
position: absolute;
width:100%; height: 100%;
top: 0; left: 0;
background:rgba(0,0,0,0.6);
opacity:0;
transition: all 0.5s;
-webkit-transition: all 0.5s;
}
.hover_div:before {
content: attr(data-content);
color:#fff;
position:absolute;
opacity:0;
transition: all 0.5s;
-webkit-transition: all 0.5s;
z-index: 1;
text-align:center;
height:100%;
width:100%;
padding-top:50%;
}
.hover_div:hover:after {
opacity:1;
}
.hover_div:hover:before {
opacity:1;
}
<div data-content="Elektro" class="hover_div">
<img src="http://i.stack.imgur.com/Sjsbh.jpg" alt="" />
</div>

Related

CSS Animation: `backface-visibility` causing cross-browser problems?

I'm developing a flip animation to show new numbers; it's much like an analog clock or calendar with the hinge in the middle.
The approach is straight forward: have a div with:
The bottom half of the first number on one side
The top half of the second number rotated 180 degrees so it's on the back
In order to show the new number, I rotate that whole div around the center of the container, revealing the back of the rotating div:
Number flip animation in latest Firefox
However, in Chrome, the animation doesn't always work. Sometimes half disappears completely until the transition animation is complete and sometimes the old number doesn't render: Number flip animation in latest Chrome with the bottom of the number not appearing till after animation is complete
In Safari 12, it's worse, it doesn't seem to respect backface-visibility, even with the -webkit- prefix:
Safari 12 Number animation, the bottom half of the first number is inverted after animation is complete
Pre-Chromium Edge handles this fine, but new (checked in v83) Edge has the same issue as Chrome.
I've tried messing around with the properties and have looked through other backface-visibility questions here.
Here's the code, hover over the numbers to see the flip:
body {
background: #2e517d;
}
.container {
width: 175px;
height: 192px;
background: #4e9bfa;
position: relative;
left: 50%;
transform: translate(-50%, 50%);
perspective: 1000px;
}
.cover {
width: 175px;
height: 50%;
position: absolute;
top: 96px;
background-color: #34b58c;
transform: rotateX(0deg);
transform-style: preserve-3d;
transform-origin: top;
transition: all 0.5s ease-out;
}
.container:hover .cover {
transform: rotateX(180deg);
}
.flip {
margin: 0;
display: block;
position: absolute;
width: 100%;
height: 100%;
backface-visibility: hidden;
}
.container p {
font-size: 1000%;
margin: 0;
}
.container>p {
height: 96px;
overflow: hidden;
}
.front-number-bottom {
position: relative;
height: 96px;
overflow: hidden;
background-color: red;
}
.front-number-bottom p {
margin: 0;
position: relative;
top: -96px;
}
.back-number-top {
position: relative;
height: 96px;
overflow: hidden;
}
.back-number-bottom {
height: 96px;
overflow: hidden;
position: relative;
z-index: -1;
}
.back-number-bottom p {
margin: 0;
position: relative;
top: -96px;
}
div.front {
background: red;
}
div.back {
background: green;
transform: rotateX(180deg);
}
<body>
<div class="container">
<p>76</p>
<div id="cover" class="cover">
<div class="flip front">
<div class="front-number-bottom">
<p>76</p>
</div>
</div>
<div class="flip back">
<div class="back-number-top">
<p>77</p>
</div>
</div>
</div>
<div class="back-number-bottom">
<p>77</p>
</div>
</div>
</div>
</body>
Is this a sound approach that can be easily fixed in Chromium browsers and Safari?
Would a different approach be better?
I guess your code is a bit complex. I would simplify your logic like below where you no more need backface-visibility: hidden;
Note the usage of two important things:
the mask that allow me to cut the element and show only 50% of the height (top or bottom). This will make the animation more realistic since each number will have both top and bottom part seperated.
the z-index trick where I apply a transtion that change the z-index exactly at the middle of the animation (when the rotations are at 90deg)1.
.card {
width: 175px;
height: 192px;
position: relative;
z-index:0;
left: 50%;
transform: translate(-50%, 50%);
font-size: 160px;
}
.card span,
.card span::before,
.card span::after {
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
}
.card span {
position:absolute;
z-index:2;
perspective: 1000px;
}
.card span:first-child {
z-index:3;
transition:0s 0.25s all linear;
}
.card span::before,
.card span::after{
content:attr(data-number);
-webkit-mask:linear-gradient(#fff,#fff) top/100% 50% no-repeat;
mask:linear-gradient(#fff,#fff) top/100% 50% no-repeat;
background:red;
transition:0.5s all linear;
transform-style: preserve-3d;
}
.card span::after {
-webkit-mask-position:bottom;
mask-position:bottom;
background:green;
}
.card span:first-child::after {
transform: rotateX(0deg);
}
.card span:last-child::before {
transform: rotateX(-180deg);
}
/* Hover */
.card:hover span:first-child {
z-index:1;
}
.card:hover span:first-child::after {
transform: rotateX(180deg);
}
.card:hover span:last-child::before {
transform: rotateX(0deg);
}
<div class="card">
<span data-number="76"></span>
<span data-number="77"></span>
</div>
The mask can be replaced with clip-path too:
.card {
width: 175px;
height: 192px;
position: relative;
z-index:0;
left: 50%;
transform: translate(-50%, 50%);
font-size: 160px;
}
.card span,
.card span::before,
.card span::after {
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
}
.card span {
z-index:2;
perspective: 1000px;
}
.card span:first-child {
z-index:3;
transition:0s 0.25s all linear;
}
.card span::before,
.card span::after{
content:attr(data-number);
clip-path:polygon(0 0,100% 0,100% 50%,0 50%);
background:red;
transition:0.5s all linear;
transform-style: preserve-3d;
}
.card span::after {
clip-path:polygon(0 50%,100% 50%,100% 100%,0 100%);
background:green;
}
.card span:first-child::after {
transform: rotateX(0deg);
}
.card span:last-child::before {
transform: rotateX(-180deg);
}
/* Hover */
.card:hover span:first-child {
z-index:1;
}
.card:hover span:first-child::after {
transform: rotateX(180deg);
}
.card:hover span:last-child::before {
transform: rotateX(0deg);
}
<div class="card">
<span data-number="76"></span>
<span data-number="77"></span>
</div>
Another optimization using counter and without setting an explicit width/height
.card {
margin:0 5px;
font-family:monospace;
display:inline-block;
text-align:center;
position: relative;
z-index:0;
font-size: 150px;
counter-reset:num calc(var(--n,1) - 1);
}
/* this will defined the height/width*/
.card::after {
content:counter(num);
visibility:hidden;
}
/**/
.card span,
.card span::before,
.card span::after {
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
}
.card span {
z-index:2;
perspective: 1000px;
counter-increment:num;
}
.card span:first-child {
z-index:3;
transition:0s 0.25s all linear;
}
.card span::before,
.card span::after{
content:counter(num);
clip-path:polygon(0 0,100% 0,100% 50%,0 50%);
background:red;
transition:0.5s all linear;
transform-style: preserve-3d;
}
.card span::after {
clip-path:polygon(0 50%,100% 50%,100% 100%,0 100%);
background:green;
}
.card span:first-child::after,
.card:hover span:last-child::before{
transform: rotateX(0deg);
}
.card span:last-child::before {
transform: rotateX(-180deg);
}
.card:hover span:first-child::after {
transform: rotateX(180deg);
}
.card:hover span:first-child {
z-index:1;
}
<div class="card" style="--n:75">
<span></span><span></span>
</div>
<div class="card" style="--n:5">
<span></span><span></span>
</div>
<div class="card" style="--n:100">
<span></span><span></span>
</div>
1 When using linear it's pretty easy but it's more trick with other ease functions. Here is a related question that can help you identify the middfle of ease functions: When exactly does an ease animation reach its midpoint?

Animate borders (border-left, border-top) of box on hover

I would like to animate two borders on hover specifically border-left and border-top. After doing some research it does not seem you can actually "animate" the borders themselves so you have to create a "line" which on hover should have its width set to 100% to have the same effect.
I know how to do this with underlining menu items, but I would like to do it with this box I'm trying to create.
Specifically on hover (while maintaining the css effects already written up)
1) border-left should extend to the top and right after that-> 2) border-top extending from the left to the right.
Also was wondering how I can choose which borders to extend if I don't want to to just do border-left or border-top.
This is my box thus far (unfortunately nothing with animating borders):
CSS:
#txt{
position:absolute;
top:50%;
left:50%;
transform:translate(-50%, -50%);
font-size:2vw;
}
#box{
position:fixed;
top:25%;
left:25%;
height:20vw;
width:20vw;
border-right: 2px solid deepskyblue;
border-bottom: 2px solid deepskyblue;
background-color:black;
color:ghostwhite;
}
#box:hover{
color:deepskyblue;
transition: color 0.25s ease;
}
#box:after{
content:"";
position:absolute;
bottom: 0;
left: 0;
width:100%;
height:100%;
transform: scale(0, 0);
transform-origin:bottom right;
background: ghostwhite;
z-index: -1;
transition: transform 0.25s ease;
}
#box:hover::after{
transform: scale(1, 1);
color:deepskyblue;
}
HTML:
<div id="box">
<span id="txt">TEXT</span>
</div>
You can make the #txt element as large as the parent box and then use pseudo-element on that to make "borders" and animate the dimensions of those pseudo-elements.
If you add a transiton-delay in I think you can get the effect you are after.
#txt {
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
display: flex;
justify-content: center;
align-items: center;
}
#box {
font-size: 2vw;
position: fixed;
top: 1em;
left: 40vw;
height: 20vw;
width: 20vw;
background-color: black;
color: ghostwhite;
}
#box:hover {
color: deepskyblue;
transition: color 0.25s ease;
}
#box:after {
content: "";
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 100%;
transform: scale(0, 0);
transform-origin: bottom right;
background: ghostwhite;
z-index: -1;
transition: transform 0.25s ease;
}
#box:hover::after {
transform: scale(1, 1);
color: deepskyblue;
}
#txt::before {
content: "";
position: absolute;
z-index: 2;
left: 0;
bottom: 0;
height: 0;
}
#txt::before {
width: 0;
border-left: 2px solid deepskyblue;
transition: height .25s .5s ease;
}
#txt:hover::before {
height: 100%;
}
#txt::after {
content: "";
position: absolute;
width: 0%;
top: 0;
left: 0;
border-top: 2px solid deepskyblue;
transition: width 0.25s .75s ease;
}
#txt:hover::after {
width: 100%;
}
<div id="box">
<span id="txt">TEXT</span>
</div>

How to change a link-image on mouseover in this unusual setting?

I have a bunch of pictures as links on one site, like this:
<IMG SRC="img/contact2.png" class="mobile" border="0" alt="Contact">
Later on, I position and size them correctly with CSS, and added a hover effect:
.mobile{
position: absolute;
left: 69%;
top: -12%;
width: 19%;
height: auto;
transition: linear;
transition-duration: 0.5s;
z-index: 993; }
.mobile {
position:absolute;
top: -11%;
left: 68%;
width:23%;
height:auto;
display:block;
transition: linear;
transition-duration: 0.5s;
z-index:999;
}
So far, so good, works as intended, but what if, instead of simply resizing it and slightly changing it's position, I want it to display a different image on mouseover?
I tried doing it like this:
with this CSS
.mobile
{
position: absolute;
display: block;
background-image: url("img/contact2.png") center top no-repeat;
width: 23.2%;
height: auto;
left: 10%;
top: 0%;
transition: linear;
transition-duration: 0.5s;
z-index: 992; }
.mobile:hover {
position:absolute;
background-image: url('img/contact3.png');
top: -3%;
left: 11%;
width:26%;
height:auto;
display:block;
transition: linear;
transition-duration: 0.5s;
z-index:999;
}
But that just results in the image disappearing altogether. What am I doing wrong? I think it has something to do with the use of % for size, especially the "height: auto", but because of the context of the rest of the site, I can't replace that.
If you need any more info, just let me know, I didn't wanna spam you guys with more than necessary.
Below I will once copy-paste the entire code, since I realize that with the minimal info I gave last time, my problems couldn't really be answered. Maybe with this, someone can copy it and see what I mean:
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=800" />
<title>Mainsite</title>
</head>
<body style="padding:0px; margin:0px; overflow: scroll;" >
<body background="http://i.imgur.com/YAxp4xz.jpg">
<style>
html,body { height:100%; }
</style>
<div class="text">
<style>
#font-face {
font-family: 'gt-walsheim';
src: url('web/gt-walsheim.ttf');
}
</style>
<style type="text/css">
.prozent {line-height: 150%;
font-family: "gt-walsheim";
}
</style>
<br>
<p class="prozent" align="center">
<font size="+3.5"><b><font color="#804040">RESTAURANT-NAME</font></b> </font>
<br>
<br>
<font size="+2"><font color="#804040">Blablablablablabla Über uns balbalba ablab balbal Willkommen ablabla blabal
<br>
<br>
<br>
<br>
<br>
</div>
<!-- Ende dv class="text" -->
<div class='box'>
<div class='content'>
<div class="tisch">
<IMG SRC="http://i.imgur.com/QebCkyE.png" class="menu" border="0" alt="Zur Speisekarte">
<IMG SRC="http://i.imgur.com/qoHQ9Bd.gif" class="teller" border="0" alt="">
<IMG SRC="http://i.imgur.com/WNR18gb.png" class="handy" border="0" alt="Kontakt">
<IMG SRC="http://i.imgur.com/I4GTdXr.png" class="pad" border="0" width="47%" alt="Öffnungszeiten">
</div>
<!-- Ende tisch -->
</div>
</div>
<style>
.text {
position: relative;
width: 100%;
z-index: 990; }
.box{
position: relative;
width: 100%; /* desired width */
}
.box:before{
content: "";
display: block;
padding-top: 35%; /* initial ratio of 1:1*/
}
.content{
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
.tisch
{
position: relative;
height: 70%;
<!-- x% höhe von alles -->
width: 100%;
z-index: 991; }
.menu
{
position: absolute;
width: 23.2%;
height: auto;
left: 10%;
top: 0%;
transition: linear;
transition-duration: 0.5s;
z-index: 992; }
.menu:hover {
position:absolute;
top: -3%;
left: 11%;
width:26%;
height:auto;
display:block;
transition: linear;
transition-duration: 0.5s;
z-index:999;
}
.teller
{
position: absolute;
width: 35%;
height: auto;
left: 30%;
top: 0%;
z-index: 992; }
.handy
{
position: absolute;
left: 69%;
top: -17%;
width: 19%;
height: auto;
transition: linear;
transition-duration: 0.5s;
z-index: 993; }
.handy:hover {
position:absolute;
top: -15%;
left: 68%;
width:23%;
height:auto;
display:block;
transition: linear;
transition-duration: 0.5s;
z-index:999;
}
.pad
{
position: absolute;
left: 78%;
top: 48%;
width: 12%;
height: auto;
transition: linear;
transition-duration: 0.5s;
z-index: 993; }
.pad:hover {
position:absolute;
top: 43%;
left: 76%;
width:14%;
height:auto;
display:block;
transition: linear;
transition-duration: 0.5s;
z-index:999;}
</style>
Those are of course all placeholder graphics, but that's not important-
What I want it to be able to not only transition in size and position, but to an entirely different image as well.
You may notice that everything is defined with %, and works just fine. This (and all those divs) are necessary as a setup, so no matter how I resize the window, the pictures not only stay proportional to it, but also in position to each other.
Not quite sure while changing that the picture source is defined in the stylesheet, instead of in the link itself, causes problems and makes my pictures disappear.
This definition doesn't work for 2 reasons. First, a block level element is only as tall as the contents inside. Without contents inside, it is 0 height. So this element doesn't appear on the page because it is 0 height. Second issue is you're using background-image wrong. What you meant to do there is use background, the short-hand property, since you're including center top no-repeat.
.mobile
{
position: absolute;
display: block;
background-image: url("img/contact2.png") center top no-repeat;
width: 23.2%;
height: auto;
left: 10%;
top: 0%;
transition: linear;
transition-duration: 0.5s;
z-index: 992; }
Besides the issues I just mentioned, this definition doesn't work because you misspelled "mobile"
.moblile:hover {
position:absolute;
background-image: url('img/contact3.png');
top: -3%;
left: 11%;
width:26%;
height:auto;
display:block;
transition: linear;
transition-duration: 0.5s;
z-index:999;
}
If you address those issues, it works.
html,body {height:100%;}
.mobile {
position: absolute;
display: block;
background: url("http://www.w3schools.com/css/img_fjords.jpg") center top no-repeat;
width: 23.2%;
height: 100%;
left: 10%;
top: 0%;
transition: linear;
transition-duration: 0.5s;
z-index: 992;
}
.mobile:hover {
position: absolute;
background-image: url('https://s3-us-west-1.amazonaws.com/powr/defaults/image-slider2.jpg');
top: -3%;
left: 11%;
width: 26%;
display: block;
transition: linear;
transition-duration: 0.5s;
z-index: 999;
}

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>

Vertically center text on an image - dynamically

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%;
}