Overlaying text on image not working - css

I have three images in a row. The images don't have text in them. That needs to be added with the code. I used the method described in posts here (using relative and absolute positions) and have it somewhat working. Here is the jsfiddle. There are two main problems.
First, the placement isn't consistent. If the window is resized, the text doesn't move with the image. On a smartphone, the text disappears completely.
Second, when the images are mousedover they expand. But if the mouse is moved over the text, the expanding stops. If I remove the z-index then the text disappears when the image is mousedover. I want to have the text always show but also to expand the image. My code is below. It is taken from the example here, except for the text overlay, which I added. Is it possible to add text to an image that changes and have it work as described?
<style>
.nav {margin:0; padding-top:5px;overflow:hidden}
.nav-items {border:1px solid black}
.nav-items {position:relative;margin-left:0px; display:inline-block; overflow: hidden;}
.nav-items:hover img {
box-shadow: 0px 0px 150px #000000;
z-index: 2;
-webkit-transition: all 500ms ease-in;
-webkit-transform: scale(2.1);
-ms-transition: all 500ms ease-in;
-ms-transform: scale(2.1);
-moz-transition: all 500ms ease-in;
-moz-transform: scale(2.1);
transition: all 500ms ease-in;
transform: scale(2.1);
}
.nav-items img {
-webkit-transition: all 200ms ease-in;
-webkit-transform: scale(1);
-ms-transition: all 200ms ease-in;
-ms-transform: scale(1);
-moz-transition: all 200ms ease-in;
-moz-transform: scale(1);
transition: all 200ms ease-in;
transform: scale(1);
}
#bannerText_0,
#bannerText_1,
#bannerText_2{
position:absolute;
font-size:20px;
line-height: 150%;
color:#fff;
top:60px;
background:transparent;
z-index:999;
}
#bannerText_0{left:10%;}
#bannerText_1{left:35%;}
#bannerText_2{left:60%;}
</style>
<div class="banner_set">
<ul class="nav">
<li id="0" class="nav-items"><img src="http://placehold.it/130x130"></li>
<div id="bannerText_0">Img 1</div>
<li id="1" class="nav-items"><img src="http://placehold.it/130x130"></li>
<div id="bannerText_1">Img 2</div>
<li id="2" class="nav-items"><img src="http://placehold.it/130x130"></li>
<div id="bannerText_2">Img 3</div>
</ul>
</div>

The main problem is that the structured your HTML incorrectly - you can't put <div>s in between the <li> elements in the <ul>.
As well as causing the broken layout, its also the reason the mouseover on the text affects the image doesn't work - the text isn't actually inside an nav-item element that has the hover effect.
You can solve all your problems by putting your div's inside the <li> elements!
Bonus Fix :), I've also fixed the space at the bottom under the image - just make the image display:block;.
Your Fiddle, updated with these changes: https://jsfiddle.net/pcpg7zww/2/
Working snippet:
.nav {margin:0; padding-top:5px;overflow:hidden}
.nav-items {border:1px solid black}
.nav-items {position:relative;margin-left:0px; display:inline-block; overflow: hidden;}
.nav-items:hover img {
box-shadow: 0px 0px 150px #000000;
z-index: 2;
-webkit-transition: all 500ms ease-in;
-webkit-transform: scale(2.1);
-ms-transition: all 500ms ease-in;
-ms-transform: scale(2.1);
-moz-transition: all 500ms ease-in;
-moz-transform: scale(2.1);
transition: all 500ms ease-in;
transform: scale(2.1);
}
.nav-items img {
-webkit-transition: all 200ms ease-in;
-webkit-transform: scale(1);
-ms-transition: all 200ms ease-in;
-ms-transform: scale(1);
-moz-transition: all 200ms ease-in;
-moz-transform: scale(1);
transition: all 200ms ease-in;
transform: scale(1);
display:block; /* this will remove the space at the bottom */
}
#bannerText_0,
#bannerText_1,
#bannerText_2{
position:absolute;
font-size:20px;
line-height: 150%;
color:#fff;
top:60px;
background:transparent;
z-index:999;
}
#bannerText_0{left:10%;}
#bannerText_1{left:35%;}
#bannerText_2{left:60%;}
<div class="banner_set">
<ul class="nav">
<li id="0" class="nav-items">
<a href="example.com">
<img src="http://placehold.it/130x130">
<div id="bannerText_0">Img 1</div>
</a>
</li>
<li id="1" class="nav-items">
<a href="example.com">
<img src="http://placehold.it/130x130">
<div id="bannerText_1">Img 2</div>
</a>
</li>
<li id="2" class="nav-items">
<a href="example.com">
<img src="http://placehold.it/130x130">
<div id="bannerText_2">Img 3</div>
</a>
</li>
</ul>
</div>

Related

CSS3: Hover effect with opacity on div's and text

I can't share a page on this, due to the page not being public. However, what I'm trying to do is create a hover effect on both a div and a H4 text element. Nothing of which seems to work for me. Here's my code:
HTML:
<div class="grid__item small--one-half medium-up--one-quarter">
<a href="/collections/hoop-earrings" class="collection-item collection-item--overlaid" data-aos="row-of-4"><div class="image-wrap">
<div class="collection-image collection-image--square lazyload" style='background-position: center center; background-image: url("https://cdn.shopify.com/s/files/1/1810/9951/collections/LDE42DSOS_Hexagon_Hoop_Earring_web_scroll_1200x_ad647924-a6b9-4c9a-b36a-7d6a3b0d0a6c_720x.jpg?v=1561755337");'>
</div>
</div>
<noscript>
<div
class="collection-image collection-image--square"
style="background-image: url(//cdn.shopify.com/s/files/1/1810/9951/collections/LDE42DSOS_Hexagon_Hoop_Earring_web_scroll_1200x_ad647924-a6b9-4c9a-b36a-7d6a3b0d0a6c_400x.jpg?v=1561755337); background-position: center center;">
</div>
</noscript>
<div class="collection-image--overlay-background"></div>
<span
class="collection-item__title collection-item__title--overlaid collection-item__title--heading collection-item__title--center">
<span>
Text Goes Here
</span>
</span>
</a>
</div>
CSS:
.collection-image--overlay-background {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: #fff;
opacity: 0;
-webkit-transition: opacity 1s linear;
-moz-transition: opacity 1s linear;
-ms-transition: opacity 1s linear;
-o-transition: opacity 1s linear;
transition: opacity 1s linear;
}
.collection-item__title.collection-item__title--overlaid.collection-item__title--heading.collection-item__title--center {
opacity: 0!important;
-moz-transition: opacity .25s linear;
-webkit-transition: opacity .25s linear;
-o-transition: opacity .25s linear;
transition: opacity .25s linear;
}
#media only screen and (min-width: 768px) {
.collection-image--overlay-background:hover {
z-index: 1;
opacity: .8;
}
.collection-item__title.collection-item__title--overlaid.collection-item__title--heading.collection-item__title--center:hover {
z-index: 1;
opacity: 1;
}
}
I tried making a small snippet. See if it´s what you´re looking for.
Since you´re using the !important flag in one of your opacity rules, all the other changes after the fact will not take effect unless you also use !important. However I would not advice using !important that freely because whenever they are present such errors are a bit harder to track.
.container {
background-color: blue;
height: 100px;
}
.onHover {
background-color: red;
width: 80%;
height: 100px;
margin: 0 auto;
opacity: 0.5;
/*opacity: 0.5 !important; In your code you have
a line like this.*/
}
.onHover:hover {
opacity: 1;
}
<div class="container">
<div class="onHover">
<h4>My title Here</h4>
</div>
</div>

Smooth effect CSS animation for hover out

I'm trying to get a smooth "hover out" animation when you stop hovering the object. The font-awesome icon is rotating when you hover the whole button.
I'm using it:
#keyframes roll {
from {
transform: rotateY(0);
}
to {
transform: rotateY(360deg);
}
}
.p-navEl a:first-child:hover::before {
animation: roll .5s linear infinite;
}
HTML for the first button of the nav bar
<ul class="p-nav-list js-offCanvasNavSource">
<li>
<div class="p-navEl is-selected" data-has-children="true">
Forums
<a data-xf-key="1" data-xf-click="menu" data-menu-pos-ref="< .p-navEl" data-arrow-pos-ref="< .p-navEl" class="p-navEl-splitTrigger" role="button" tabindex="0" aria-label="Basculer en mode étendu" aria-expanded="false" aria-haspopup="true"></a>
<div class="menu menu--structural" data-menu="menu" aria-hidden="true">
<div class="menu-content">
Nouveaux messages
Trouver des discussions
Vos discussions
Discussions avec vos messages
Discussions sans réponse
<hr class="menu-separator">
Suivies
Discussions suivies
Forums suivis
<hr class="menu-separator">
Rechercher dans les forums
Marquer les forums comme lus
</div>
</div>
</div>
</li>
</ul>
PREVIEW
The bubble before "FORUMS" is rotating when you hover it. But I want the animation smooth when you stop hovering it
but when you don't hover the link anymore, the animation is brutaly stopped.
Any idea?
I can't share the original CSS since it's a userscript for a public/private website I'm not the owner!
Please do try transitions like below and let me know fiddle!.
HTML
<body bgcolor="#333">
<div class="display-center">
<span>Rotate</span>
</div>
</body>
Css
.display-center{
display:flex;
justify-content: center;
align-items: center;
height: 400px;
width: 100%;
background-color: white;
}
span {
background-color: gray;
padding: 10px;
border-radius: 8px;
color: #222;
text-align: center;
transition: 0.70s;
-webkit-transition: 0.70s;
-moz-transition: 0.70s;
-ms-transition: 0.70s;
-o-transition: 0.70s;
display: block;
margin-right: auto;
margin-left: auto;
}
span:hover {
transition: 0.70s;
-webkit-transition: 0.70s;
-moz-transition: 0.70s;
-ms-transition: 0.70s;
-o-transition: 0.70s;
-webkit-transform: rotate(360deg);
-moz-transform: rotate(360deg);
-o-transform: rotate(360deg);
-ms-transform: rotate(360deg);
transform: rotate(360deg);
}

Can't stop animation fro cycling

I have three images in a row. I want to enlarge the one that the mouse is over until the mouse is moved off of it. This sort of works in my jsfiddle but, as you can see, the animation doesn't stop after enlarging. The other threads on this problem say to set the iteration count and forwards options, which I've done. The only other solution I could find was to use javascript to control it. Is there a way to do this with just css? Here's my code:
<style>
#set2 {margin-left:40px; display:inline-block}
#set2:hover {
-webkit-animation: enlarge 5s;
animation: enlarge 5s;
animation-fill-mode: forwards;
animation-iteration-count: 1;
}
#-webkit-keyframes enlarge {
25% {
-webkit-transform: scale(1.5);
transform: scale(1.5);
}
}
</style>
<div class="banner_set">
<ul class="nav">
<li id="set2" class="nav-items"><img src="example1.jpg"></li>
<li id="set2" class="nav-items"><img src="example2.jpg"></li>
<li id="set2" class="nav-items"><img src="example3.jpg"></li>
</ul>
</div>
Your keyframe set at 25% means your element will be scaled 1/4 of the way through the animation, then not scaled at the end. If you just want a smooth scale up and that's all, use 100% (and I would suggest, reduce the duration!).
I updated your fiddle. The weird image styling is so we can see your images.
#set2 {margin-left:40px; display:inline-block}
#set2:hover {
-webkit-animation: enlarge 2s;
animation: enlarge 2s 1 forwards;
}
#-webkit-keyframes enlarge {
100% {
-webkit-transform: scale(1.5);
transform: scale(1.5);
}
}
img {
min-height: 30px;
min-width: 30px;
border: 3px solid red;
}
<div class="banner_set">
<ul class="nav">
<li id="set2" class="nav-items"><img src="example1.jpg"></li>
<li id="set2" class="nav-items"><img src="example2.jpg"></li>
<li id="set2" class="nav-items"><img src="example3.jpg"></li>
</ul>
</div>
Is there a specific reason you're using the animation approach rather than a transition?
Since the desired behavior is to toggle between two animated states, perhaps the transition is an easier way to approach it.
Using your example code:
.nav {margin:0; padding-top:5px;overflow:hidden}
.nav-items {border:1px solid black}
.nav-items {margin-left:0px; display:inline-block; overflow: hidden;}
.nav-items:hover img {
box-shadow: 0px 0px 150px #000000;
z-index: 2;
-webkit-transition: all 500ms ease-in;
-webkit-transform: scale(2.1);
-ms-transition: all 500ms ease-in;
-ms-transform: scale(2.1);
-moz-transition: all 500ms ease-in;
-moz-transform: scale(2.1);
transition: all 500ms ease-in;
transform: scale(2.1);
}
.nav-items img {
-webkit-transition: all 200ms ease-in;
-webkit-transform: scale(1);
-ms-transition: all 200ms ease-in;
-ms-transform: scale(1);
-moz-transition: all 200ms ease-in;
-moz-transform: scale(1);
transition: all 200ms ease-in;
transform: scale(1);
}
<div class="banner_set">
<ul class="nav">
<li id="0" class="nav-items small_0"><img src="http://placehold.it/200x200"></li>
<li id="1" class="nav-items small_1"><img src="http://placehold.it/200x200"></li>
<li id="2" class="nav-items small_2"><img src="http://placehold.it/200x200"></li>
</ul>
</div>

CSS3: Columns and -webkit-transform effect

I've been trying to apply a CSS3 -webkit-transform effect on a image inside a columns scheme. Currently I've the following demo:
<html>
<head>
<meta charset="UTF-8" />
<title>Demo</title>
<style>
div.colSection article {
display: inline-block;
column-break-after: always;
column-break-inside:avoid;
break-inside:avoid-column;
-webkit-column-break-after: always;
-moz-column-break-after: always;
-webkit-column-break-inside:avoid;
-moz-column-break-inside:avoid;
}
div.colSection.noticias {
column-count: 3;
-webkit-column-count:3;
-moz-column-count:3;
}
img.scale {
-webkit-transition: all 0.5s ease-in;
-moz-transition: all 0.5s ease-in-out;
-o-transition: all 0.5s ease-in-out;
-ms-transition: all 0.5s ease-in-out;
transition: all 0.5s ease-in-out;
}
img.scale:hover {
-moz-transform: scale(1.1);
-webkit-transform: scale(1.1);
-o-transform: scale(1.1);
transform: scale(1.1);
}
</style>
</head>
<body>
<div class="section main" id="noticias">
<h1>News</h1>
<div class="colSection noticias">
<article>
<img src="http://i.imgur.com/ysmA3VD.jpg" alt="" class="scale" />
<h2>N1</h2>
<p>T1</p>
</article>
<article>
<img src="http://i.imgur.com/ysmA3VD.jpg" alt="" class="scale" />
<h2>N2</h2>
<p>T2</p>
</article>
<article>
<img src="http://i.imgur.com/ysmA3VD.jpg" alt="" class="scale" />
<h2>N3</h2>
<p>T3</p>
</article>
</div>
</div>
</body>
</html>
The problem with the code is that the animation tend to end up in strange behaviors, for instance:
Safari
When I place the mouse over the second image, no animation happens but the image just disappears. The first one works as indented and nothing happens on the last one.
Chrome
The middle image goes down and the last one flickers a bit.

CSS3 div move on hover not ease-out

Ok guys, here's the problem: I made this simple menu with three menu-items, and I want to move each div to the right every time I hover on it (simple, right? Unfortunately not...)
While it does the ease-in animation, it won't do at all the ease-out one, the result being not fluid, but blocky and not cool at all.
I searched online and on StackOverflow, too, and applied all fixes/suggestions made, but I wasn't able to get it to work.
Here's the code (to try, for example, on jsFiddle)
HTML:
<div id="menu-container">
<div class="menu1">Menu 01</div>
<div class="menu2">Menu 02</div>
<div class="menu3">Menu 03</div>
</div>
CSS:
#menu-container div{
height: 30px;
width: 200px;
border:1px solid #999;
background-color:#222;
color:#ccc;
left: 0;
-webkit-transition: all 0.5s ease-in-out;
-moz-transition: all 0.5s ease-in-out;
-o-transition: all 0.5s ease-in-out;
transition: all 0.5s ease-in-out;
}
#menu-container div:hover{
position: relative;
color:#fff;
background-color:#333333;
left: 20px;
padding-left: -20px;
}
#menu-container div.menu1:hover{
border-color: red;
}
#menu-container div.menu2:hover{
border-color: blue;
}
#menu-container div.menu3:hover{
border-color: green;
}
What am I doing wrong? Is there a way to fix it?
Thanks in advance
This is because the divs are only position: relative on hover, which is not animatable. The animation effect on left is lost when it switches back to position: static. Simply add position: relative to the un-hovered style.
http://jsfiddle.net/Pre5p/
Depending on your browser support for animations like that you should use transform: translate. You get a much smoother animation...
#menu-container div:hover{
color:#fff;
background-color:#333333;
-webkit-transform: translateX(20px);
-moz-transform: translateX(20px);
-ms-transform: translateX(20px);
-o-transform: translateX(20px);
transform: translateX(20px);
}

Resources