Image hover overlay with text - text position - css

Good evening,
I am trying to make image hover overlay with text, but displayed text is always on the top of the image. I would like to display it in the middle of it.
I've tried padding etc, but it moved the overlay as well, that it covered also the free space below the image. Could you please help me, how to margin only text from top? Thank you!
EDIT: Or the best solution would be, if I could include another div with text only. Because I need to include text with html tags and it's not possible this way, via "content:"
http://jsfiddle.net/sL6bjebx/
Here is CSS code:
.image {
position:relative;
margin: 0 auto;
}
.image img {
width:100%;
vertical-align:;
}
.image:after {
content:'Here is some text..';
color:#fff;
position:absolute;
width:100%; height: 100%; max-height:100%;
top:0; left:0;
background:rgba(0,0,0,0.75);
opacity:0;
transition: all 0.5s;
-webkit-transition: all 0.5s;
}
.image:hover:after {
opacity:1;
}

If your overlay text is going to be a one liner then you can use line-height css property to make it vertically center aligned
.image:after {
content:'Here is some text..';
color:#fff;
position:absolute;
width:100%; height: 100%; max-height:100%;
top:0; left:0;
background:rgba(0,0,0,0.75);
opacity:0;
transition: all 0.5s;
-webkit-transition: all 0.5s;
line-height:300px;
}
and if not, then I will suggest to use another way to display the content.
such as :before just like this fiddle, Or you can use a <p> tag as well instead of the :before tag check this fiddle.
.image:after {
content:'';
color:#fff;
position:absolute;
width:100%; height: 100%; max-height:100%;
top:0; left:0;
background:rgba(0,0,0,0.75);
opacity:0;
transition: all 0.5s;
-webkit-transition: all 0.5s;
z-index:1
}
.image:before {
content:'Here is some text..';
color:#fff;
position:absolute;
width:100%; height: 20px;
top:0; left:0; right:0;
bottom:0;
opacity:0;
margin:auto;
z-index:2
}
.image:hover:after {
opacity:1;
}
.image:hover:before {
opacity:1;
}

Here is the solution with another div or something, where would be possible to insert text with HTML tags:
.image {
position:relative;
margin: 0 auto;
width: 317px;
height: 317px;
text-align: center;
}
.image img {
width:100%;
vertical-align:;
}
.image .overlayText {
color:#fff;
position:absolute;
width:100%; height: 100%; max-height:100%;
line-height:317px;
top:0; left:0;
background:rgba(0,0,0,0.75);
opacity:0;
transition: all 0.5s;
-webkit-transition: all 0.5s;
margin:0;
}
.image:hover > .overlayText {
opacity:1;
}
<div class="image">
<img src="http://media.tumblr.com/tumblr_m6v66lJ5K61r0taux.jpg">
<p class="overlayText"><b>Here</b> is some text</p>
</div>

You should simply add line-height
.image {
position:relative;
margin: 0 auto;
}
.image img {
width:100%;
vertical-align:;
}
.image:after {
content:'Here is some text..';
color:#fff;
position:absolute;
width:100%; height: 100%; max-height:100%;
line-height:317px;
top:0; left:0;
background:rgba(0,0,0,0.75);
opacity:0;
transition: all 0.5s;
-webkit-transition: all 0.5s;
}
.image:hover:after {
opacity:1;
}
<div class="image" style="width: 317px; height: 317px; text-align: center;">
<img src="http://media.tumblr.com/tumblr_m6v66lJ5K61r0taux.jpg">
</div>

Related

Transform Scale makes Div with border-radius Blurry

Basically, when I hover over a div, I increase the scale with transform. This div has a border-radius, and when it scales, it gets blurry. In the JSFIDDLE example, the blur happens then goes away, but on my localhost site, it stays blurry.
Is it just happening to my screen, or is anyone else seeing this too?
JSFIDDLE LINK https://jsfiddle.net/3arynm5v/
HTML
<div class="circle">
<div class="circle--inner"></div>
<div class="circle--outer"></div>
</div>
SCSS
.circle {
width:300px;
height:300px;
background:black;
position:relative;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
-webkit-perspective: 1000;
perspective: 1000;
&:hover &--inner {
transform:scale(7);
}
&--inner, &--outer {
position:absolute;
margin:0 auto;
left:0; right:0;
top:50%;
transform:translateY(-50%);
border-radius:200%;
}
&--inner {
width:10px;
height:10px;
background:white;
transition:transform 1s;
transform-origin: center center;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
-webkit-perspective: 1000;
perspective: 1000;
}
&--outer {
width:30px;
height:30px;
border:5px solid white;
}
}
What transform does is take the existing size and zoom in on it. What you can do is set the &--inner on width and height: 70px (10px times the zoom factor 7 from the transform). Position it in the middle and instead of zooming it 7x on hover, scale it down on the normal view.
I've updated the Fiddle to show you what I mean. https://jsfiddle.net/3arynm5v/3/
As you can see I've edited the outer and inner to to:
&:hover &--inner {
transform:translate(0, -50%)scale(1);
top: 50%;
}
&--inner, &--outer {
position:absolute;
margin:0 auto;
left:0; right:0;
top:50%;
border-radius:200%;
}
&--inner {
width:70px;
height:70px;
background:white;
transition:transform 1s;
transform-origin: center center;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
-webkit-perspective: 1000;
perspective: 1000;
transform: translate(0, -50%)scale(.1);
}
&--outer {
width:30px;
height:30px;
border:5px solid white;
transform:translateY(-50%);
}

Use CSS animation to reveal text from center and keep text visible at end

I'm using code from Reveal Text from center with CSS Animation to reveal text from the center. But I'd like to keep the text visible after the animation is over. Right now, the text disappears after the animation. I'm new to css animation, so I'm not sure what the right thing to do is.
css:
.text-div {
color:white;
text-align:center;
position: absolute;
left: 0;
right: 0;
margin: 0 auto;
top:45%;
font-weight: bold;
}
.text-div:before {
left:0;
}
.text-div:after {
right:0;
}
.text-div:after,.text-div:before {
position:absolute;
content:"";
height:100%;
width:50%;
background:black;
animation: revealText 3s;
}
.content {
background:black;
height:200px;
width:200px;
position:relative;
}
#keyframes revealText {
0% {
width:50%
}
100% {
width:0%
}
}
html:
<div class="content">
<div class="text-div">
The subculture
</div>
</div>
Add animation-fill-mode:forwards to .text-div:after,.text-div:before as below,
.text-div:after,.text-div:before {
position:absolute;
content:"";
height:100%;
width:50%;
background:black;
animation: revealText 3s;
animation-fill-mode:forwards; /*Add this*/
}
The animation-fill-mode CSS property specifies how a CSS animation
should apply styles to its target before and after it is executing.
.text-div {
color:white;
text-align:center;
position: absolute;
left: 0;
right: 0;
margin: 0 auto;
top:45%;
font-weight: bold;
}
.text-div:before {
left:0;
}
.text-div:after {
right:0;
}
.text-div:after,.text-div:before {
position:absolute;
content:"";
height:100%;
width:50%;
background:black;
animation: revealText 3s;
animation-fill-mode:forwards;
}
.content {
background:black;
height:200px;
width:200px;
position:relative;
}
#keyframes revealText {
0% {
width:50%
}
100% {
width:0%
}
}
<div class="content">
<div class="text-div">
The subculture
</div>
</div>

Text on image not displaying

Text on my image is not appearing. Its animating but i want text on my image on mouse hover. For text I used button with image that suppose to popup on mouse hover.let me know my mistake. I am using this code from youtube tutorial.
.main
{
border: 10px solid white;
width:378px;
height:250px;
margin:50px auto;
box-shadow:0px 0px 25px black;
overflow:hidden;
}
.main:hover img
{
-webkit-transform:scale(2,2) rotate(45deg);
}
.main:hover .content
{
-webkit-transform:translate(-311px);
}
img
{
-webkit-transition: -webkit-transform: 300ms;
}
.content
{
width:378px;
height:250px;
background: rgba(124,120,120,0.5);
-webkit-transition: -webkit-transform: 300ms;
}
button
{
width:100%;
height:50px;
margin-top:200px;
background:black;
border:0;
cursor:pointer;
color:white;
font:16px tahoma;
}
button:hover {
opacity: 0.5;
}
<div class="main"><img src="img/switch.jpg" height="250" width="378"><div class="content"><button>Pepe Kalvier Switches</button></div></div>
You can use the position absolute to move your text/button above the image when hover.
CSS
.main:hover .content{
z-index: 9999;
position: absolute;
top:0;
}
DEMO HERE
you can try this one:
<div class="main"><img src="http://dev.boutiquevalmont.com/media/wysiwyg/valmont/arts/thb3.jpg" height="250" width="378"><div class="content"><button>Pepe Kalvier Switches</button></div></div>
DEMO

center text vertically with css on jsfiddle

On my jsfiddle, I actually have a text on top when mouse is over images. I would like to know how can I center this text vertically with css.
here is my jsfiddle -> www.jsfiddle.net/kodjoe/4kurw7ap/
I know that I've to use display:inline-block with vertical-align:middle, but nothing change ;-/ perhaps I've forgotten something ??
HERE IS MY UDAPTED CODE:
HTML:
<div id="wrapper"><div id="list">
<div alt="text here" class="item"><img class="imggrid" src="http://s5.favim.com/orig/141028/coffee-fashion-girl-grunge-Favim.com-2187377.jpg" alt=""></div>
<div alt="text here" class="item"><img class="imggrid" src="http://images0.chictopia.com/photos/highglossfashion/5948104335/high-gloss-fashion-dress_400.jpg" alt=""></div>
</div></div>
UDAPTED CSS:
body { background: #f1f1f1; }
#wrapper { max-width: 1260px; margin: 0 auto; }
#list {
overflow: hidden;
margin-bottom: -30px;
-webkit-column-count: 3;
-webkit-column-gap: 30px;
-webkit-column-fill: auto;
-moz-column-count: 3;
-moz-column-gap: 30px;
-moz-column-fill: auto;
column-count: 3;
column-gap: 30px;
column-fill: auto;
}
.imggrid { width:100%; padding:10px;}
.imggrid:hover{ opacity: 0.9; }
.item { vertical-align: top; display: inline; margin-bottom: 30px; -webkit-column-break-inside: avoid; -moz-column-break-inside: avoid; column-break-inside: avoid; }
#media only screen and ( max-width: 1024px ) {
#wrapper { max-width: 860px; margin: 0 auto; }
#list { -webkit-column-count: 2; -webkit-column-gap: 15px; -moz-column-count: 2; -moz-column-gap: 15px; column-count: 2; column-gap: 15px; }
.item { margin: 0px auto 15px auto;}
}
#media only screen and ( max-width: 600px ){
#wrapper { max-width: 440px; margin: 0 auto; }
#list { -webkit-column-count: auto; -moz-column-count: auto; column-count: auto; }
}
div { position:relative; display:inline-block; }
div:after {
content: attr(alt);
height:80%;
width:80%;
margin: 0 auto;
position:absolute;
top:10%;
left:10%;
background:rgba(255, 255, 255, 1);
text-align:center;
color:black;
display:inline-block;
box-sizing:border-box;
opacity:0;
transition: 0.4s all;
-webkit-transition: 0.4s all;
-moz-transition: 0.4s all;
}
div:hover:after { opacity:1; }
Please Try this one:
div { position:relative; display:inline-block; vertical-align:middle; }
.centerDiv{
position:absolute;
left:50%;
top:50%;
}
.container
{
text-align:center;
}
.centerDiv {
content: attr(alt);
height:80%;
width:80%;
margin: 0 auto;
position:absolute;
top:10%;
left:10%;
background:rgba(255, 255, 255, 1);
text-align:center;
color:black;
display:inline-block;
box-sizing:border-box;
opacity:0;
transition: 0.4s all;
-webkit-transition: 0.4s all;
-moz-transition: 0.4s all;
}
.centerDiv:hover { opacity:1; }
.content{
top: 50%;
transform: translateY(-50%);
position:relative;
}
}
DEMO
Include padding-top for div:after
div:after {
padding-top:33%;
}
Could you use something like this?
Codepen http://codepen.io/noobskie/pen/WQvOYX?editors=110
All I've done is added the classes first-img & second-img and added padding to :after on the top of each one adjusted based on the image height.
I've actually never seen alt text used on a div like that i think it would probably be better to put the img and a p tag inside a div add position:relative; on the div and position:absolute on the p tag, it might be easier for you to work with.
here is some documentation on it https://css-tricks.com/design-considerations-text-images/
Try the following code
Add the following code in CSS.
CSS
#box{
text-align: center;
vertical-align: middle;
line-height: 80px; /*The same as your div height*/
}
Add the following code in div tag.
div tag
<div alt="text here" ID="box">
Use a flexbox and center vertically using justify-content and horizontally using align-items. In this way the text can be more the one line, and we don't need to know the height of the ::after (fiddle):
div::after {
display:flex; /** don't forget to remove the display: inline-block **/
justify-content: center;
align-items: center;
padding: 10%; /** not necessary but looks better with long text **/
content: attr(alt);
height:80%;
width:80%;
margin: 0 auto;
position:absolute;
top:10%;
left:10%;
background:rgba(255, 255, 255, 1);
color:black;
box-sizing:border-box;
opacity:0;
transition: 0.4s all;
-webkit-transition: 0.4s all;
-moz-transition: 0.4s all;
}
i would recommend solving this using some child elements. http://jsfiddle.net/4kurw7ap/1/
div { position:relative; display:inline-block; vertical-align:middle; }
.centeredDiv{
position:absolute;
left:50%;
top:50%;
}
.container
{
text-align:center;
}
.centeredDiv {
content: attr(alt);
height:80%;
width:80%;
margin: 0 auto;
position:absolute;
top:10%;
left:10%;
background:rgba(255, 255, 255, 1);
text-align:center;
color:black;
display:inline-block;
box-sizing:border-box;
opacity:0;
transition: 0.4s all;
-webkit-transition: 0.4s all;
-moz-transition: 0.4s all;
}
.centeredDiv:hover { opacity:1; }
.content{
top: 50%;
transform: translateY(-50%);
position:relative;
}
}
<div alt="text here">
<div class="centeredDiv">
<div class="content">text here</div>
</div>
<img src="http://s5.favim.com/orig/141028/coffee-fashion-girl-grunge-Favim.com-2187377.jpg" />
</div>
<div class="container" alt="text here">
<div class="centeredDiv">
<div class="content">text here</div>
</div>
<img src="http://images0.chictopia.com/photos/highglossfashion/5948104335/high-gloss-fashion-dress_400.jpg" />
</div>
div { position:relative; display:inline-block; vertical-align:middle; }
.centeredDiv{
position:absolute;
left:50%;
top:50%;
}
.container
{
text-align:center;
}
.centeredDiv {
content: attr(alt);
height:80%;
width:80%;
margin: 0 auto;
position:absolute;
top:10%;
left:10%;
background:rgba(255, 255, 255, 1);
text-align:center;
color:black;
display:inline-block;
box-sizing:border-box;
opacity:0;
transition: 0.4s all;
-webkit-transition: 0.4s all;
-moz-transition: 0.4s all;
}
.centeredDiv:hover { opacity:1; }
.content{
top: 50%;
transform: translateY(-50%);
position:relative;
}
}
You can do that by using
div::after {
line-height: 20em;
....
}

Cant' Position Caption Div Appropriately

Can you please take a look at JSfiddle and let me know how I can position the div with .caption at the bottom of the div with class .wrapper.
I already tried to add position:absolute; bottom:5px; rules to .caption but it positioned the caption div out of the .wrapper !
<div class="wrapper">
<div class="caption">Test</div>
<div class="main"></div>
</div>
here is the css rules
.wrapper{
height:300px;
width:300px;
background-color:yellow;
}
.main{
height:100%;
width:100%;
}
.caption{
height:10%;
width:100%;
background: rgb(0, 0, 0) ; opacity: 0.7;>
margin-bottom:5px;
color:white;
}
The position: absolute should work but you'll need to set .wrapper as:
.wrapper {
position: relative;
}
Demo here: http://jsfiddle.net/sBxP2/4/
try this
http://jsfiddle.net/shall1987/MwJsG/
CSS
.wrapper{
height:300px;
width:300px;
background-color:yellow;
position: relative;
}
.main{
height:100%;
width:100%;
}
.caption{
height:10%;
width:100%;
background: rgb(0, 0, 0) ; opacity: 0.7;>
margin-bottom:5px;
color:white;
position: absolute;
bottom: 0;
}
you can update your css like this
http://jsfiddle.net/shall1987/MwJsG/
.wrapper{
height:300px;
width:300px;
background-color:yellow;
position:relative;
}
.main{
height:100%;
width:100%;
}
.caption{
height:10%;
width:100%;
background: rgb(0, 0, 0) ; opacity: 0.7;
margin-bottom:5px;
color:white;
position:absolute;
bottom:0;
}

Resources