How to vertically center on hover element text? - html

I have a text element that appears over the image on mouseover. The text bit is horizontally centered but I'm still having troubles centering it vertically despite trying several techniques. Trying to achieve this without Javascript.
HTML:
<figure class="wp-caption-my">
<img class="demo-my" src="image.png" alt="Image" />
<figcaption class="wp-caption-text-my">This is a caption text</figcaption>
</figure>
CSS:
.wp-caption-my {
position: relative;
padding: 0;
margin: 0;
}
.wp-caption-my img {
display: block;
max-width: 100%;
height: auto;
}
.wp-caption-text-my {
opacity: 0;
position: absolute;
width: 100%;
height: 100%;
color: #fff;
left: 0;
bottom: 0;
padding: 0.75em 1em;
font-weight: 700;
z-index: 2;
-webkit-box-sizing: border-box;
box-sizing: border-box;
background-color: rgba(0,191,255,0.9);
text-align: center;
-webkit-transition: opacity .3s ease-in-out !important;
transition: opacity .3s ease-in-out !important;
}
.wp-caption-my:hover .wp-caption-text-my {
opacity: 1;
}
img.demo-my {
width: 100% !important;
height: 100% !important;
}

I think you can use CSS pseudo elements this way:
.wp-caption-my {
position: relative;
padding: 0;
margin: 0;
}
.wp-caption-my img {
display: block;
max-width: 100%;
height: auto;
}
.wp-caption-text-my {
opacity: 0;
position: absolute;
width: 100%;
height: 100%;
color: #fff;
left: 0;
bottom: 0;
padding: 0.75em 1em;
font-weight: 700;
z-index: 2;
-webkit-box-sizing: border-box;
box-sizing: border-box;
background-color: rgba(0,191,255,0.9);
text-align: center;
-webkit-transition: opacity .3s ease-in-out !important;
transition: opacity .3s ease-in-out !important;
}
.wp-caption-my:hover .wp-caption-text-my {
opacity: 1;
}
.wp-caption-text-my::before {
content: "";
display: inline-block;
width: 0px;
height: 100%;
vertical-align: middle;
}
.wp-caption-text-my span {
display: inline-block;
vertical-align: middle;
}
img.demo-my {
width: 100% !important;
height: 100% !important;
}
<figure class="wp-caption-my">
<img class="demo-my" src="http://janisweb.tk/grahams/wp-content/uploads/2016/07/officewaste.png" alt="Image" />
<figcaption class="wp-caption-text-my"><span>This is a caption text</span></figcaption>
</figure>
This is done by wrapping your text in a span and adding this CSS to your styles:
.wp-caption-text-my::before {
content: "";
display: inline-block;
width: 0px;
height: 100%;
vertical-align: middle;
}
.wp-caption-text-my span {
display: inline-block;
vertical-align: middle;
}

Wrap the text inside another element, and set top:
HTML
<figcaption class="wp-caption-text-my"><h3>This is a caption text</h3></figcaption>
CSS
.wp-caption-text-my h3 {
position: relative;
top: 50%;
line-height: 18px;
margin-top: -18px;
}
Check it with more text:
Codepen: https://codepen.io/anon/pen/LkAyEd

Aligning your text to the center of your image, using pseudo element :before on-hover.
.wp-caption-text-my:before{
content:'';
display:block;
width:100%;
height:50%;
background:#f22;
}
I have added background color to pseudo element, so you could understand what it does and then you can align it accordingly to your needs.
.wp-caption-my {
position: relative;
padding: 0;
margin: 0;
}
.wp-caption-my img {
display: block;
max-width: 100%;
height: auto;
}
.wp-caption-text-my {
opacity: 0;
position: absolute;
width: 100%;
height: 100%;
color: #fff;
left: 0;
bottom: 0;
padding: 0.75em 1em;
font-weight: 700;
z-index: 2;
-webkit-box-sizing: border-box;
box-sizing: border-box;
background-color: rgba(0,191,255,0.9);
text-align: center;
-webkit-transition: opacity .3s ease-in-out !important;
transition: opacity .3s ease-in-out !important;
}
.wp-caption-text-my:before{
content:'';
display:block;
width:100%;
height:50%;
background:#f22;
}
.wp-caption-my:hover .wp-caption-text-my {
opacity: 1;
}
img.demo-my {
width: 100% !important;
height: 100% !important;
}
<figure class="wp-caption-my">
<img class="demo-my" src="https://source.unsplash.com/random" alt="Image" />
<figcaption class="wp-caption-text-my">This is a caption text
<figcaption style="padding-top:5px;">Add such more captions</figcaption>
<figcaption style="padding-top:5px;">And style them as you want.</figcaption>
</figcaption>
</figure>

To center horizontally and vertically an absolute element you can easily do
position: absolute
margin: auto;
top: 0; left: 0; bottom: 0; right: 0;
fiddle here: http://jsfiddle.net/mBBJM/1/
from https://codemyviews.com/blog/how-to-center-anything-with-css#comment-684580538

Related

Adding a transition to a card

.grid_content {
max-width: 700px;
margin: 0 auto;
position: relative;
overflow: hidden;
}
img {
width: 100%;
height: 100%;
display: block;
}
.caption {
position: absolute;
z-index: 1;
bottom: 0;
padding: 30px;
colour: white;
width: 100%;
height: 100%;
opacity: 0;
transition: 1s;
}
h4 {
font-family: var(--font-style-custom);
letter-spacing: 3px;
}
p {
margin: 5px;
}
h4,
p {
position: relative;
top: 85px;
}
.caption:hover {
transition: 1s;
opacity: 1;
background: linear-gradient(hsl(0, 0%, 100%, 0.1), hsl(0 0% 0% / 0.6));
}
<div class="grid_content">
<img src="https://img.freepik.com/free-psd/box-packaging-mockup-isolated_23-2149208917.jpg?w=740&t=st=1663223199~exp=1663223799~hmac=6f0288bfcdf858b4908269c5406636c1f3c0bfca3e70041f67d9adc5b0be7dea" alt="Brading image">
<div class="caption">
<h4>Laynard</h4>
<p>Brand-identity</p>
</div>
</div>
Whenever I hover on the image, I want the background color to slowly transition in and out, but here it only transitions in and disappears when I don't hover, I've added transition to the caption div element in the card and it still didn't work
As initially the opacity of the element with the class caption's opacity is set to 0, it's recommended to include the background property in .caption {...} rather than in .caption:hover {...}.
Additionally, remove the transition property in .caption:hover as it's useless.
The issue with your implementation is that when the cursor hovers over the element, the background property is set, and when the cursor is not longer hovering over the element, the background property is reset and the transiton property doesn't get applied to it.
.grid_content {
max-width: 700px;
margin: 0 auto;
position: relative;
overflow: hidden;
}
img {
width: 100%;
height: 100%;
display: block;
}
.caption {
position: absolute;
z-index: 1;
bottom: 0;
padding: 30px;
colour: white;
width: 100%;
height: 100%;
opacity: 0;
transition: 1s;
background: linear-gradient(hsl(0, 0%, 100%, 0.1), hsl(0 0% 0% / 0.6));
}
h4 {
font-family: var(--font-style-custom);
letter-spacing: 3px;
}
p {
margin: 5px;
}
h4,
p {
position: relative;
top: 85px;
}
.caption:hover {
opacity: 1;
}
<div class="grid_content">
<img src="https://img.freepik.com/free-psd/box-packaging-mockup-isolated_23-2149208917.jpg?w=740&t=st=1663223199~exp=1663223799~hmac=6f0288bfcdf858b4908269c5406636c1f3c0bfca3e70041f67d9adc5b0be7dea" alt="Brading image">
<div class="caption">
<h4>Laynard</h4>
<p>Brand-identity</p>
</div>
</div>
Move your background style from .caption:hover to .caption
You already made the transition of opacity
.grid_content {
max-width: 700px;
margin: 0 auto;
position: relative;
overflow: hidden;
}
img {
width: 100%;
height: 100%;
display: block;
}
.caption {
position: absolute;
z-index: 1;
bottom: 0;
padding: 30px;
colour: white;
width: 100%;
height: 100%;
opacity: 0;
transition: 1s;
background: linear-gradient(hsl(0, 0%, 100%, 0.1), hsl(0 0% 0% / 0.6));
}
h4 {
font-family: var(--font-style-custom);
letter-spacing: 3px;
}
p {
margin: 5px;
}
h4,
p {
position: relative;
top: 85px;
}
.caption:hover {
transition: 1s;
opacity: 1;
}
<div class="grid_content">
<img src="https://img.freepik.com/free-psd/box-packaging-mockup-isolated_23-2149208917.jpg?w=740&t=st=1663223199~exp=1663223799~hmac=6f0288bfcdf858b4908269c5406636c1f3c0bfca3e70041f67d9adc5b0be7dea" alt="Brading image">
<div class="caption">
<h4>Laynard</h4>
<p>Brand-identity</p>
</div>
</div>

Text on image on hover affected by filter brightness

I'm trying to create images with texts on them but when it hovers the brightness of the image goes down yet it shouldn't affect the text. Can I achieve that just with css?
#mixin easeOut {
transition: all 0.3s ease-out;
}
.team-pics {
display: flex;
flex-wrap: wrap;
div {
width: 33%;
img {
display: block;
width: 100%;
padding: 1rem;
#include easeOut;
}
.team-pic-caption {
opacity: 0;
position: absolute;
bottom: 0;
left: 0;
width: 100%;
padding: 1rem;
#include easeOut;
color: #fff;
}
&:hover {
filter: brightness(.5);
.team-pic-caption {
opacity: 1;
}
}
}
}
<div class="team-pics">
<div>
<img src="https://images.unsplash.com/photo-1515550833053-1793be162a45?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=31a23f7d7e8b0e43153da6565aa7157e&auto=format&fit=crop&w=500&q=60" />
<div class="team-pic-caption">
<h3>Josh Garwood</h3>
<p>Co-Founder and Technical Director</p>
</div>
</div>
<div>
<img src="https://images.unsplash.com/photo-1513267096918-a2532362a784?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=f971c866a1e22a858a0c2ea72274838c&auto=format&fit=crop&w=500&q=60" />
<div class="team-pic-caption">
<h3>Jason Benjamin</h3>
<p>Co-Founder and Marketing Director</p>
</div>
</div>
</div>
https://codepen.io/yubind/pen/mGWQBr
I have a few potential solutions for you
Here is the first (background will blur, but text will remain the same, code is adaptable to your needs of course):
#imgtext {
position: relative;
float: left;
width: 300px;
padding: 30px;
margin: 15px;
border-radius: 20px;
height: 150px;
overflow: hidden;
}
#imgtext:after {
content: '';
display: block;
position: absolute;
z-index: -1;
top: 0;
left: 0;
bottom: 0;
right: 0;
background: url(https://upload.wikimedia.org/wikipedia/en/e/e4/Blank_cd.png) no-repeat center;
background-size: cover;
}
#imgtext:hover:after {
-webkit-filter: blur(5px);
}
p {
font-size: 3em;
color: red;
text-align: center;
}
<div>
<div id="imgtext">
<p>Hello<p>
</div>
</div>
Another is outlined in this fiddle (the text only appears on hover, when the image fades)
.wrapper {
position: relative;
padding: 0;
width: 150px;
display: block;
}
.text {
position: absolute;
top: 0;
color: #f00;
background-color: rgba(255, 255, 255, 0.8);
width: 150px;
height: 150px;
line-height: 1em;
text-align: center;
z-index: 10;
opacity: 0;
-webkit-transition: all .5s ease;
-moz-transition: all .5s ease;
-o-transition: all .5s ease;
transition: all .5s ease;
}
.text img {
top: 20px;
position: relative;
}
.text:hover {
opacity: 1;
}
}
img {
z-index: 1;
}
<a href="#" class="wrapper">
<span class="text">
<img src="http://prologicit.com/wp-content/uploads/2012/07/160px-Infobox_info_icon_svg-150x150.png" width="30" height="30"><br><br><br>
"Photo"<br>
Made:1999<br>
By: A. Miller<br>
150x150px
</span>
<img src="http://lorempixel.com/150/150">
</a>
I like this animate opacity fiddle but it may be over what you're looking for...

:hover style on div with an image that doesnt trigger

I've been trying to make a overlay hover effect on a website with pure css
<div class="col-md-4 port-show">
<img class="img-responsive" alt="" src="">
<h2 class=""></h2>
<ul>
<li></li>
<li></li>
</ul>
</div>
But I've run into the problem when I hover over the img that the overlay will not trigger. I know I have to do it differently, but just don't now how.
CSS:
:hover:after{
content: "";
z-index: 10;
display: block;
position: absolute;
height: 100%;
top: 0;
left: 0;
right: 0;
background: red;
border-radius: 6px;
border-color: transparent;
}
You can look here. Maybe this solution will be helpfull for you.
http://codepen.io/anon/pen/VKmxZw
.img-holder {
position: relative;
z-index: 1;
display: block;
width: 350px;
}
.img-holder img {
display: block;
width: 100%;
}
.img-holder:after {
content: " ";
width: 100%;
height: 100%;
background-color: red;
z-index: 2;
left: 0;
top: 0;
position: absolute;
opacity: 0;
}
.img-holder:hover:after {
opacity: 1;
}
<div class="col-md-4 port-show">
<span class="img-holder">
<img src="http://placehold.it/350x150" />
</span>
<h2 class=""></h2>
<ul>
<li></li>
<li></li>
</ul>
</div>
You can follow this and make your overlay effect
#import url(http://fonts.googleapis.com/css?family=Open+Sans);
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: "Open Sans", sans-serif;
}
a {
color: inherit;
text-decoration: none;
}
h2 {
color: #c55;
padding: 50px 0;
text-align: center;
text-transform: uppercase;
}
.container {
background: url("http://static.pexels.com/wp-content/uploads/2015/03/fog-forest-haze-4827-524x350.jpeg");
background-size: cover;
background-position: center;
position: relative;
margin: auto;
width: 200px;
height: 200px;
cursor: pointer;
overflow: hidden;
}
.container:hover .overlay {
opacity: 1;
width: 100%;
height: 100%;
}
.container:hover .overlay span {
opacity: 1;
-webkit-transition: 1.3s ease;
transition: 1.3s ease;
}
.container .overlay {
background: rgba(0, 0, 0, 0.5);
position: absolute;
margin: auto;
width: 0px;
height: 0px;
top: 0;
right: 0;
bottom: 0;
left: 0;
opacity: 0;
-webkit-transition: .3s ease;
transition: .3s ease;
}
.container .overlay span {
color: #fff;
text-align: center;
position: absolute;
margin: auto;
width: 180px;
height: 30px;
line-height: 30px;
left: 0;
top: 0;
right: 0;
bottom: 0;
opacity: 0;
}
<h2>Image Overlay Hover Effect</h2>
<div class="container">
<a href="#">
<div class="overlay">
<span>Click to see more...</span>
</div>
</a>
</div>
Some easy overlay code:
<!DOCTYPE html>
<html >
<head>
<style type="text/css">
.pic{ width:190px;
height:190px; opacity: 1;
filter: alpha(opacity=100);
background: url(http://www.corelangs.com/css/box/img/duck.png) no-repeat; }
.pic:hover { opacity: 0.3; filter: alpha(opacity=30);}
</style>
</head>
<body>
<div class="pic">
</div>
</body>
</html>

How can I get the width of a link be only the width of the text's width?

I managed to get pretty far into coding a function where the link's info will fade-in upon hovering over the link's title, but whenever I hover my mouse over the remaining space of the title's text, it triggers the link's info to fade-in. Is there a way I can get the activation-width only the with of the link's title? I am also looking for a CSS-only solution. If you can think of a more efficient method of achieving this, please let me know :)
Fiddle here.
And here's a visualization of the problem since I'm not too familiar with code / developing terminology:
Many thanks in advance!
HTML:
<div class="page">
<div class="text">
<span class="title">Title (More)</span>
<span class="info">
Title Description over here
</span>
</div>
<div class="image">
<img src="..." alt="" />
</div>
</div>
CSS:
html, body {
margin: 0;
padding: 0;
width:100%;
height:100%;
}
body {
font-family: helvetica;
font-size: 18px;
line-height: 26px;
}
div.page {
width:80%;
max-width:960px;
margin:0 auto;
height:100%;
}
div.text {
width:80%;
max-width:960px;
margin:0 auto;
}
div.text span.title {
-webkit-transition: all .5s;
transition: all .5s;
}
div.text:hover span.title {
opacity: 0;
visibility: hidden;
}
div.text span.info {
position: absolute;
top: 0;
right: 0;
left: 0;
opacity: 0;
visibility: hidden;
-webkit-transition: all .5s;
transition: all .5s;
}
div.text:hover span.info {
opacity: 1;
visibility: visible;
}
.image img {
width: 100%;
height: auto;
padding-top: 10px;
}
.text{
position: absolute;
z-index: 9999;
}
.image{
position:relative;
top:20px;
}
Its because your hover event is on a div which is a block level (width:100%) element.
You need to set it to display inline, or width auto so it doesnt take up the full width, then revert on hover:
html,
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
body {
font-family: helvetica;
font-size: 18px;
line-height: 26px;
}
div.page {
width: 80%;
max-width: 960px;
margin: 0 auto;
height: 100%;
}
div.text {
display: inline-block;
max-width: 960px;
margin: 0 auto;
}
div.text:hover {
display: block;
width: 80%;
}
div.text span.title {
-webkit-transition: all .5s;
transition: all .5s;
}
div.text:hover span.title {
opacity: 0;
visibility: hidden;
}
div.text span.info {
position: absolute;
top: 0;
right: 0;
left: 0;
opacity: 0;
visibility: hidden;
-webkit-transition: all .5s;
transition: all .5s;
}
div.text:hover span.info {
opacity: 1;
visibility: visible;
}
.image img {
width: 100%;
height: auto;
padding-top: 10px;
}
.text {
position: absolute;
z-index: 9999;
}
.image {
position: relative;
top: 20px;
}
<div class="page">
<div class="text">
<span class="title">Title (More)</span>
<span class="info">
Title Description over here<br/>
More text<br/>
More text<br/>
And you know, text.<br/>
</span>
</div>
<div class="image">
<img src="http://www.graphicthoughtfacility.com/media/uploads/images/FW-SmallTray-WEB.jpg" alt="" />
<img src="http://www.graphicthoughtfacility.com/media/uploads/images/FW-LargeBox-WEB.jpg" alt="" />
<img src="http://www.graphicthoughtfacility.com/media/uploads/images/Formwork1_RGB-WEB.jpg" alt="" />
</div>
</div>
div.text is having 80% width and hence you can hover on the right side of it. Give the div.text a smaller width and give the span.info bigger width.
div.text {
width:15%;
max-width:960px;
margin:0 auto;
}
div.text span.info {
width: 250px;
position: absolute;
top: 0;
right: 0;
left: 0;
opacity: 0;
visibility: hidden;
-webkit-transition: all .5s;
transition: all .5s;
}
See Fiddle

Adding 3 different buttons on to a semi-transparent area

I need help to
How can I add two more image buttons inside the semi transparent area (<figcaption>) and all 3 buttons to be vertically centered and distributed equally inside the semi transparent area?
The button I've added seems to be semi-transparent (same as the <figcaption> opacity). I need the buttons to be opacity:1;?
This is the code:
<div class="container demo-2">
<ul class="grid cs-style-2">
<li>
<figure>
<gt_descA>Chair</gt_descA>
<gt_descC>$87.34</gt_descC>
<img src="imagez/designs/thumbs/0/01.jpg" alt="img02">
<figcaption>
<span class="hint hint--top hint--rounded" data-hint="Zoom Image"><img src="additional/buttons/bu_zoom.jpg"></img></span>
</figcaption>
</figure>
</li>
</ul>
</div>
CSS
.grid {
padding: 70px 20px 100px 20px;
max-width: 1300px;
margin: 0 auto;
list-style: none;
text-align: center;
}
.grid li {
display: inline-block;
width: 234px;
margin: 0;
padding: 20px;
text-align: left;
position: relative;
}
.grid figure {
margin: 0;
position: relative;
}
.grid figure img {
max-width: 100%;
display: block;
position: relative;
}
.grid figcaption {
position: absolute;
top: 0;
left: 0;
padding: 20px;
background: #000;
color: #ed4e6e;
opacity:0;
}
/*button*/
.grid figcaption a {
display: inline-block;
}
/* Caption Style*/
.cs-style-2 figure figcaption {
z-index: 10;
-webkit-transition: -webkit-transform 0.4s;
-moz-transition: -moz-transform 0.4s;
transition: transform 0.4s;
}
.no-touch .cs-style-2 figure:hover figcaption,
.cs-style-2 figure.cs-hover figcaption {
-webkit-transform: translateY(0px);
-moz-transform: translateY(0px);
-ms-transform: translateY(0px);
transform: translateY(0px);
opacity:0.6;
}
.cs-style-2 figcaption {
height: 24px;
width: 187px;
top: auto;
bottom: 0px;
}
.cs-style-2 figcaption a {
position: absolute;
right: 20px;
top: 30px;
}
#media screen and (max-width: 31.5em) {
.grid {
padding: 10px 10px 100px 10px;
}
.grid li {
width: 100%;
min-width: 234px;
}
}
You can add buttons, and to align them use
container {
text-align: center;
}
Or this:
container {
max-width: someval;
margin: 0 auto; // no vertical, auto horizontal
}
And the alpha filter (opacity) is added to child elements too! Try to setting value for them too; seperately..