On hover, I am trying to swap a background image using a CSS transition. However, in Safari, the image shrinks during the animation and I'm not sure why.
.icon {
background-color: white;
width: 100px;
height: 100px;
background-image: url(data:image/svg+xml;base64,PHN2ZyBpZD0iTGF5ZXJfMSIgZGF0YS1uYW1lPSJMYXllciAxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA4MS44OSA4MS44OSI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOm5vbmU7c3Ryb2tlOiM4YjkyYTE7c3Ryb2tlLW1pdGVybGltaXQ6MTA7c3Ryb2tlLXdpZHRoOjZweDt9PC9zdHlsZT48L2RlZnM+PHRpdGxlPmljb24tc2VhcmNoPC90aXRsZT48Y2lyY2xlIGNsYXNzPSJjbHMtMSIgY3g9IjM1LjU5IiBjeT0iMzUuNTkiIHI9IjMyLjU5Ii8+PGxpbmUgY2xhc3M9ImNscy0xIiB4MT0iNTguNjMiIHkxPSI1OC42MyIgeDI9Ijc5Ljc3IiB5Mj0iNzkuNzciLz48L3N2Zz4=);
background-size: 14px 14px;
background-repeat: no-repeat;
transition: all 1s;
}
.icon:hover {
background-image: url(data:image/svg+xml;base64,PHN2ZyBpZD0iTGF5ZXJfMSIgZGF0YS1uYW1lPSJMYXllciAxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA4MS44OSA4MS44OSI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOm5vbmU7c3Ryb2tlOiM1YTJhODI7c3Ryb2tlLW1pdGVybGltaXQ6MTA7c3Ryb2tlLXdpZHRoOjZweDt9PC9zdHlsZT48L2RlZnM+PHRpdGxlPmljb24tc2VhcmNoLXB1cnBsZTwvdGl0bGU+PGNpcmNsZSBjbGFzcz0iY2xzLTEiIGN4PSIzNS41OSIgY3k9IjM1LjU5IiByPSIzMi41OSIvPjxsaW5lIGNsYXNzPSJjbHMtMSIgeDE9IjU4LjYzIiB5MT0iNTguNjMiIHgyPSI3OS43NyIgeTI9Ijc5Ljc3Ii8+PC9zdmc+);
}
https://jsfiddle.net/Lfqucz82/
Although I don't know why Safari behaves like this, if you're trying to crossfade icons on hover, work on :after and :before pseudo-selectors opacity. Try as in this fiddle: https://jsfiddle.net/ifthenelse/6s9g4kdn/
Your CSS would look like this:
/* Icon container */
.icon {
position: relative;
background-color: white;
width: 100px;
height: 100px;
}
/* Images containers */
.icon:before, .icon:after {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
content: "";
display: block;
background-size: 14px 14px;
background-repeat: no-repeat;
transition: opacity 1s;
}
/* Attach backgrounds */
.icon:before {
background-image: url(data:image/svg+xml;base64,PHN2ZyBpZD0iTGF5ZXJfMSIgZGF0YS1uYW1lPSJMYXllciAxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA4MS44OSA4MS44OSI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOm5vbmU7c3Ryb2tlOiM4YjkyYTE7c3Ryb2tlLW1pdGVybGltaXQ6MTA7c3Ryb2tlLXdpZHRoOjZweDt9PC9zdHlsZT48L2RlZnM+PHRpdGxlPmljb24tc2VhcmNoPC90aXRsZT48Y2lyY2xlIGNsYXNzPSJjbHMtMSIgY3g9IjM1LjU5IiBjeT0iMzUuNTkiIHI9IjMyLjU5Ii8+PGxpbmUgY2xhc3M9ImNscy0xIiB4MT0iNTguNjMiIHkxPSI1OC42MyIgeDI9Ijc5Ljc3IiB5Mj0iNzkuNzciLz48L3N2Zz4=);
opacity: 1;
}
.icon:after {
background-image: url(data:image/svg+xml;base64,PHN2ZyBpZD0iTGF5ZXJfMSIgZGF0YS1uYW1lPSJMYXllciAxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA4MS44OSA4MS44OSI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOm5vbmU7c3Ryb2tlOiM1YTJhODI7c3Ryb2tlLW1pdGVybGltaXQ6MTA7c3Ryb2tlLXdpZHRoOjZweDt9PC9zdHlsZT48L2RlZnM+PHRpdGxlPmljb24tc2VhcmNoLXB1cnBsZTwvdGl0bGU+PGNpcmNsZSBjbGFzcz0iY2xzLTEiIGN4PSIzNS41OSIgY3k9IjM1LjU5IiByPSIzMi41OSIvPjxsaW5lIGNsYXNzPSJjbHMtMSIgeDE9IjU4LjYzIiB5MT0iNTguNjMiIHgyPSI3OS43NyIgeTI9Ijc5Ljc3Ii8+PC9zdmc+);
opacity: 0;
}
/* Transition on hover */
.icon:hover:before {
opacity: 0;
}
.icon:hover:after {
opacity: 1;
}
If the images appearing (before and during hovering) are not of the same sizes, one image (in your case, the first image) will appear perfectly sized since you have set the background-size: 14px 14px; and the other image might look resized (expanded or shrinked).
So
1. Check the sizes of the images. (if they are not the same, then define the perfect css background-size property you need
2. For the transition to be perfect (in all browsers & browser version), do
.icon{
//other css codes;
transition: 1s;
-o-transition: 1s;
-ms-transition: 1s;
-moz-transition: 1s;
-webkit-transition: 1s;
}
Related
I have the following 2 classes:
.background-image {
background-image: url('../assets/artworkNight.png');
transition-property: background-image;
transition-duration: 10s;
}
.background-image:hover {
background-image: url('../assets/artworkDay.png');
}
So whenever I hover on .background-image the background begins to slowly change over the course of 10 seconds. The problem is that if I unhover the image during second 4-5, it abruptly transitions back to the original image. I was wondering if it's possible to make to transition out the same way it transitions in. For example:
If I hover for 4 seconds, then unhover, I'd want it to take 4 seconds before it completely reverts back to the original image. Is this possible with CSS?
Not really sure how to achieve this only with css other than including more elements.
Maybe this helps anyway.
.background {
position: relative;
}
.background,
.background-image1,
.background-image2 {
height: 400px;
}
.background-image1{
background-image: url('https://dummyimage.com/600x400/d92ed9/d92ed9');
position: relative;
transition: opacity 4s ease;
}
.background-image2 {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: url(https://dummyimage.com/600x400/000000/000000);
opacity: 0;
transition: opacity 4s ease;
}
.background .background-image1:hover {
opacity: 0;
}
.background .background-image2:hover {
opacity: 1;
}
<div class="background">
<div class="background-image1"></div>
<div class="background-image2"></div>
</div>
Try changing the transition-timing-function. These might give you what you're looking for.
transition-timing-function: ease-in-out;
Or
transition-timing-function: linear;
Try this
#pic1 {
position: absolute;
width: 500px;
height: 500px;
}
#pic2 {
position: absolute;
width: 500px;
height: 500px;
transition: opacity 10s;
}
#pic2:hover {
opacity: 0;
}
<img src='https://static.toiimg.com/photo/72975551.cms' alt="pic1" id="pic1"/>
<img src='https://neilpatel.com/wp-content/uploads/2017/09/image-editing-tools.jpg' alt="pic2" id="pic2"/>
background-image is not an animatable property. As a workaround, you can use ::before selector, and use opacity transition between the main element and its ::before selector.
Also, the requirement is satisfied with one single element.
* {
margin: 0;
}
.background-image {
background: url("https://cdn.pixabay.com/photo/2014/03/26/17/50/sunset-298850_960_720.jpg");
background-size: cover;
width: 100vw;
height: 100vh;
opacity: 1;
}
.background-image::before {
content: "";
width: inherit;
height: inherit;
background: url("https://cdn.pixabay.com/photo/2019/02/14/07/28/painting-3995999_960_720.jpg") no-repeat;
position: absolute;
transition-property: opacity;
transition-duration: 10s;
background-size: cover;
top: 0;
left: 0;
opacity: 0;
}
.background-image:hover::before {
opacity: 1;
}
<div class="background-image"></div>
I'm working on a small page, and one of the sections will contain an image gallery. The original pictures are squares, but on the page I set the border-radius to 50% and then when you hover above an image the radius goes to 0% and the image also scale to 1.4.
This works fine, but I also want to add a div with text in front of the image when you hover. This text will contain some information, and it will be a bit transparent so you can see the image behind it. The code below works fine in IE, Chrome and FF, but Safari just don't want to cooperate. When the scale property of the image works, the border-radius wont, and vice versa.
HTML
<div class="bild">
<img src="img/Soffa2.png">
<div class="bild_text">
<div class="bild_textrad">
<h2>SoffaDirekt</h2>
<p>Concept</p>
Read more!
</div>
</div>
</div>
CSS
.bild {
text-align: center;
overflow: hidden;
position: relative;
height: 95%;
width: 95%;
border-radius: 50%;
transition-property: border-radius;
transition-duration: .8s;
}
.bild:hover{
border-radius: 0%;
}
.bild > img {
max-width: 102%;
max-height: 102%;
transition-property: transform;
transition-duration: .8s;
}
.bild:hover > img{
transform: scale(1.3);
}
.bild_text{
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
text-align: center;
background: rgba(23 , 23, 23, 0.5);
color: white;
visibility: hidden;
opacity: 0;
transition: opacity .8s, visibility .8s;
}
.bild:hover > .bild_text{
visibility: visible;
opacity: 1;
}
There are several properties in CSS which need prefix -webkit- applied before them just like
.bild {
text-align: center;
overflow: hidden;
position: relative;
height: 95%;
width: 95%;
border-radius: 50%;
-webkit-transition-property: border-radius; /* Safari */
transition-property: border-radius;
-webkit-transition-duration: 0.8s; /* Safari */
transition-duration: 0.8s;
}
version 3.1 to 6.1 of safari support transition-property and transition-duration with -webkit- prefix.
I have a simple overlay of text over an image, darkening the background in the process. I used transition with ease-in-out, but it doesn't seem to ease out properly.
I know that the ease-in-out should be applied to the thing itself, and not its pseudo of :hover, but it doesn't seem to want to work. I have tried many ways, moving it around, deleting stuff, adding stuff, but nothing seems to make sense.
I notice the text do ease out fine, but the background with rgba opacity doesn't co-operate. It just snaps back :(
Do refer to a live version at http://g4stly.com/staff.html to know what I'm talking about, specifically.
Thanks in advance!
My code is as follows:
#g4stly
{
background-image: url('http://g4stly.com/images/users/g4stly.jpg');
}
.textFrame
{
text-align: center;
font-size: 20px;
color: #DDAA49;
text-decoration: none;
background-repeat: no-repeat;
background-size: cover;
border-radius: 30%;
width: 250px;
height: 250px;
}
.textFrame p
{
opacity: 0;
height: 75%;
margin: 0;
border-radius: 30%;
transition: opacity 300ms ease-in-out;
}
.textFrame p a
{
text-decoration: none;
color: #976649;
font-size: 25px;
}
.textFrame:hover p
{
opacity: 1;
background: rgba(0,0,0,0.8);
}
.textFrame p:first-child
{
padding: 25% 0 0 0;
}
<div id="g4stly" class="textFrame textFrameLeft">
<p>g4stly<br><br>
Owner of everything g4stly related<br>
Basically, the boss.</p>
</div>
I noticed you updated the code. Looks like your issue has already been solved.
The .textFrame p was only applying transition for opacity, so you couldn't see the background transition. I seed you added background .... to the transition, you could also do:
transition: all 1000ms ease-in-out;
Another option would be to move the rgba background to inside the .textFrame p, so the background wouldn't suddenly disappear, fading out along with the rest of the element.
Hopefully this helps you understand the cause :)
You had a comma where there should have been a semicolon.
transition: background 1000ms ease-in-out;
#g4stly {
background-image: url('http://g4stly.com/images/users/g4stly.jpg');
}
.textFrame {
text-align: center;
font-size: 20px;
color: #DDAA49;
text-decoration: none;
background-repeat: no-repeat;
background-size: cover;
border-radius: 30%;
width: 250px;
height: 250px;
}
.textFrame p {
opacity: 0;
height: 75%;
margin: 0;
border-radius: 30%;
transition: background 1000ms ease-in-out;
transition: opacity 1000ms ease-in-out;
}
.textFrame p a {
text-decoration: none;
color: #976649;
font-size: 25px;
}
.textFrame:hover p {
opacity: 1;
background: rgba(0, 0, 0, 0.8);
}
.textFrame p:first-child {
padding: 25% 0 0 0;
}
<div id="g4stly" class="textFrame textFrameLeft">
<p>g4stly<br><br> Owner of everything g4stly related<br> Basically, the boss.</p>
</div>
I am looking for a solution to create an image hover effect and keeping the image same height as widht at the same time. Here is my code right now:
.kozossegitag {
content: "";
display: block;
width: 100%;
padding-top: 100%;
background: url(http://www.kaptarcoworking.hu/wp-content/uploads/2015/07/koren_miklos.jpg);
background-size: cover;
overflow: hidden
}
.reszletek {
width: 100%;
padding-top: 100;
background-color: rgba(67,85,103,0.7);
opacity: 0;
}
.kozossegitag:hover .reszletek {
background-color: rgba(67,85,103,0.5);
opacity: 1;
-webkit-transition: opacity .25s ease;
-moz-transition: opacity .25s ease;
}
<div class="kozossegitag">
<div class="reszletek">
<span>Koren Miklós</span>
</div>
</div>
My goal is to cover the whole image with this pastel blue color and place the text right on the image. As you see here (the left one is without hover, the right one is with):
Many thanks for every help!
Cheers,
Pepe
Add this to your css:
.kozossegitag:hover{
background-color: lightblue;
background-blend-mode: multiply;
}
Here is the JSFiddle demo
It's not overly neat, as it's a little rushed but you get the idea. I used position absolute and some pseudo elements to create what I think you're trying to achieve.
CSS:
.kozossegitag {
display: block;
width: 100%;
background: url(http://www.kaptarcoworking.hu/wp-content/uploads/2015/07/koren_miklos.jpg);
background-size: cover;
overflow: hidden;
position: relative;
}
/* give the div the ratio height */
.kozossegitag::after {
content: "";
padding-top: 100%;
display: block;
}
.reszletek {
width: 100%;
opacity: 0;
}
.kozossegitag:hover .reszletek {
opacity: 1;
-webkit-transition: opacity .25s ease;
-moz-transition: opacity .25s ease;
z-index: 10;
position: relative;
}
/* overlay the light blue using ::before */
.kozossegitag:hover::before {
content: "";
background-color: rgba(67, 85, 103, 0.7);
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 0;
}
Working example.
By positioning .kozossegitag relatively, you can position .reszletek absolutely right over top of it by setting width and height to 100% and top and left to 0.
You also forgot to set height to 0 on .kozossegitag to keep it square;
.kozossegitag {
height:0;
position:relative;
content: "";
display: block;
width: 100%;
padding-top: 100%;
background: url(http://www.kaptarcoworking.hu/wp-content/uploads/2015/07/koren_miklos.jpg);
background-size: cover;
overflow: hidden
}
.reszletek {
position:absolute;
width: 100%;
height: 100%;
top:0;
left:0;
background-color: rgba(67,85,103,0.7);
opacity: 0;
}
.kozossegitag:hover .reszletek {
background-color: rgba(67,85,103,0.5);
opacity: 1;
-webkit-transition: opacity .25s ease;
-moz-transition: opacity .25s ease;
}
Fiddle: http://jsfiddle.net/trex005/rpafw24q/
Is it possible to choose a different timing/speed for transition in css so when mouse hover over a div it expands with different speed than the speed it retracts back to its original width not hover state.
I have tried declaring different transition speeds in :hoer and normal state styling, however, only normal state style seems to apply.
http://jsfiddle.net/tpf8mv51/3/
Problems:
1st) it goes with same speed it expanded with.
2nd) zindex takes effect after animation is completed for reasons i don't gte.
3rd) other images get affected even though they shouldn't, by affected i mean they 99% of time disappear till animation is done.
* {
margin: 0;
padding: 0;
}
body {
overflow: hidden;
}
.main {
font-size: 0;
height: 100%;
width: 100%;
display: none;
z-index: -1;
position: absolute;
top: 0;
left: 0;
overflow: hidden;
}
.main img {
-webkit-user-select: none;
width: 25%;
-webkit-transition: transform .5s;
transform-origin: left;
z-index: 0;
vertical-align: top;
}
.main img:hover {
transform: scale(1.3, 1);
z-index: 1;
}
Don't fret. try this (quick IN, slow OUT):
.main img {
width: 25%;
height: 100%;
transition: width 2s ease;
}
.main img:hover {
width: 50%;
transition: width .5s ease;
}
Your Fiddle as I can see it only has one transition. If you're only changing the width, tell it to change the width, which has full browser support, rather than calling transform with all the attendant prefixes.
CSS doesn't distinguish between mouseover and mouseout, but this thread seems like it may have the answer to your question:
MouseOver and MouseOut In CSS
Edit: I just realized you said you have tried this. Javascript may be your only option.
Update your css with the below (add transition)
.main img {
-webkit-user-select: none;
width: 25%;
-webkit-transition: transform .5s;
transform-origin: left;
z-index: 0;
vertical-align: top;
transition:all 1s;
}
.main img:hover {
transform: scale(1.3, 1);
z-index: 1;
transition:all 1s;
}