I'm trying to put a transition on my background-image on hover.
This is my Code so far:
HTML
<div class="item-one"></div>
CSS
.item-one {
height: 345px;
width: 256px;
background: url('http://placehold.it/256x345') scroll no-repeat center center;
background-size: cover;
-webkit-transition: background-size 1500ms linear;
-moz-transition: background-size 1500 linear;
-o-transition: background-size 1500 linear
-ms-transition: background-size 1500ms linear;
transition: background-size 1500ms linear;
}
.item-one:hover {
background-size: 150%;
}
See JSFIDDLE
But this doesn't work for me, tested in different browsers. Other transitions like background-color work as expected. Is there any restriction for transitions on this property?
I think the problem is with background-size: cover, when you change it to
background-size: 100%;
it will work
JSFiddle
There is some other question about background-size: cover alternative, that can help Is there an alternative to background-size:cover?
Or some different solution for problems like this:
CSS3 crossfade bg image when cover is used
You have a typo:
.item-one {
...
-o-transition: background-size 1500 linear
...
}
working version below:
.item-one {
background-size: 50%;
webkit-transition: background-size 1500ms linear;
-moz-transition: background-size 1500 linear;
-o-transition: background-size 1500 linear;
-ms-transition: background-size 1500ms linear;
transition: background-size 1500ms linear;
}
.item-one:hover {
background-size: 100%;
}
works fine, it didn't wokred before cuz of 'background-size:cover' :
**‘cover’:**
Scale the image, while preserving its intrinsic
aspect ratio (if any), to the smallest size such
that both its width and its height can completely
cover the background positioning area.
Try this using transform: scale(150%) for the item-one and the container set the specific size and overflow: hidden;
<div class=container>
<div class="item-one"></div>
</div>
css:
.container {
overflow: hidden;
}
.item-one:hover {
transform: scale(150%);
}
Related
This question already has answers here:
Background-size transitions in Chrome 51 - a bug or a feature?
(2 answers)
Closed 6 years ago.
I made a zoom-effect on a background of a div.
All worked till today when it stopped functioning for Chrome. For every other browser it works perfectly.
here is my code:
.project_image {
width: 582px;
height: 280px;
background-position: center;
-webkit-transition: all .2s linear;
-moz-transition: all .2s linear;
-o-transition: all .2s linear;
-ms-transition: all .2s linear;
transition: all .2s linear;
background-size: 100%;
}
.project_image:hover {
background-size: 110%;
}
On Chrome, when i hover the div, the background-size is set instantly to 110%, not slowly as wanted.
edit: fiddle https://jsfiddle.net/ca79cohz/
Add this changes,
.project_image {
background-size: 100% 100%;
}
.project_image:hover {
background-size: 110% 110%;
}
When you hover over image1div, it scales to 0.95 and fades to 80% opacity. It works in Chrome and Firefox but not Safari. It fades and scales instantly in Safari rather than smoothly in 0.5s.
.image1div {
width: 350px;
height: 350px;
margin-top: 0px;
float: right;
background-color: #5a89ad;
background-size: cover;
filter:alpha(opacity=100);
-webkit-transform: scale(1,1);
-ms-transform: scale(1,1);
transform: scale(1,1);
-webkit-transition: opacity 0.5s ease, transform 0.5s ease;
transition: opacity 0.5s ease, transform 0.5s ease;
}
.image1div:not(.no-hover):hover {
-webkit-transform: scale(0.95,0.95);
-ms-transform: scale(0.95,0.95);
transform: scale(0.95,0.95);
opacity:0.8;
filter:alpha(opacity=80);
}
I think it has to do with the filter property.
Transition is supported by safari: http://caniuse.com/#feat=css-transitions
Also the filter property, but you need to add a prefix: http://caniuse.com/#feat=css-filters
Let me know if it helps, if not, provide more details and we will find a workaround.
-- EDIT
Instead of transition: opacity, transform. Use all, or check out how you can add multiple properties CSS transition shorthand with multiple properties?
In web-site (www.kkbio.co.uk) i created a script that fix navigation bar on top and the logo is changing to smaller version by CSS3 transition. It's working in google chrome, but in firefox it isn't. Other transitions don't work too. I don't know why) Help me please:)
for example:
.navbar-brand {
margin-right: 0px;
padding: 0;
width: 342px;
height: 82px;
margin-left: 15px;
margin-top: 15px;
-moz-transition: height 0.5s, background-position 0.5s, margin-top 0.5s ease;
-webkit-transition: height 0.5s, background-position 0.5s, margin-top 0.5s ease;
-o-transition: height 0.5s, background-position 0.5s, margin-top 0.5s ease;
transition: height 0.5s, background-position 0.5s, margin-top 0.5s ease;
}
.fixed .navbar-brand {
height: 74px;
margin-top: -5px;
background-position: 0 -82px!important;
}
<a class="navbar-brand" href="http://kkbio.co.uk/" style="background-image:url(http://kkbio.co.uk/wp-content/uploads/2013/08/copy-logo1.png);"></a>
It looks like you've got a few problems here...
You've used !important which is almost always a bad sign, and you've used it twice on the same element, so which rule is more !important?
Avoid using !important whenever possible. In this case it looks like you can avoid it by using:
<a class="navbar-brand" href="http://kkbio.co.uk/" style="background-image: url(http://kkbio.co.uk/wp-content/uploads/2013/08/copy-logo1.png);"></a>
rather than:
<a class="navbar-brand" href="http://kkbio.co.uk/" style="background:url(http://kkbio.co.uk/wp-content/uploads/2013/08/copy-logo1.png);"></a>
Firefox will fill in default values if you use shorthands like background so, while Chrome reads:
background: url(http://kkbio.co.uk/wp-content/uploads/2013/08/copy-logo1.png);
Firefox reads:
background: url(http://kkbio.co.uk/wp-content/uploads/2013/08/copy-logo1.png) repeat scroll 0% 0% transparent;
Working Example
I have an element which does not have a background image when the app is loaded. Then when a button is clicked, a CSS class gets added to the element which sets the background-image for that element for a few seconds. Problem is - I can't get the background image to fade-in and fade-out.
What I tried:
.MyBox.formElementInput {
background-repeat: no-repeat;
background-position: 65px center;
background-size: "contain";
-webkit-transition:background-image 2s ease-in-out;
-moz-transition: background-image 2s ease-in-out;
-o-transition: background-image 2s ease-in-out;
transition: background-image 2s ease-in-out;
}
.MyBox.formElementInput.showSpinner {
background-image : url("/PmDojo/dojox/widget/Standby/images/loading.gif");
}
EDIT: JQuery is not an option.
CSS doesn't fade or animate background images without a javascript plugin. If you want an only CSS option, you need to wrap it in a div, and fade in/out the whole div.
If you don't mind the content of the div fading in/out as well:
#div-with-bg{
-webkit-transition: opacity 1.5s linear;
-moz-transition: opacity 1.5s linear;
transition: opacity 1.5s linear;
background: url(background.png);
opacity: 0;
}
#div-with-bg:hover{
opacity: 1;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Animating the background-size property doesn't seem to be working in Chrome or Safari.
div {
width: 161px;
height: 149px;
background: url(http://3.bp.blogspot.com/_HGPPifzMEZU/Rw4ujF12G3I/AAAAAAAAAWI/bc1ppSb6eKA/s320/estrelas_09.gif) no-repeat center center;
background-size: 50% 50%;
transition: background-size 2s ease-in;
-moz-transition: background-size 2s ease-in;
-web-kit-transition: background-size 2s ease-in
}
div:hover {
background-size: 100% 100%
}
<div>
hey
</div>
http://jsfiddle.net/ubcka/14/
You should check the browser version and whether it supports both background-size and transition. If the former, but not the latter use:
transition: background-size 2s ease-in;
-moz-transition: background-size 2s ease-in;
-ms-transition: background-size 2s ease-in;
-o-transition: background-size 2s ease-in;
-webkit-transition: background-size 2s ease-in;
It's not widely supported. See a complete list of CSS properties that support transition here. I would have a different approach. Wrap your element with background-color you wanted to do transition to, and do a scale transition for your element.
<div class="your-wrapper">
<div class="your-div">
</div>
</div>
also make sure to add proper styling
.your-wrapper {
overflow:hidden
}
.your-div {
transition: transform 0.5s;
-webkit-transition: -webkit-transform 0.5s
}
.your-wrapper:hover .your-div{
transform: scale(1.5); -webkit-transform: scale(1.5);
}
This should do.
you need to set the background-size on the div otherwise it dosn't have a set size to animate from.
.div {
background-size: 100%; //set the original size!!!!!!!!!!!!
-webkit-transition: background-size 2s ease-in;
transition: background-size 2s ease-in;
}
.div:hover {
background-size: 110%;
}
You just need to change:
-web-kit-transition: background-size 2s ease-in;
to:
-webkit-transition: background-size 2s ease-in;
Change -web-kit- to -webkit-.
Also, you should write original CSS property after properties with a vendor-prefix (it's very important). Otherwise, if browser has implemented that CSS3 property (like transition), then original property will be replaced by property with vendor-prefix — and that is not good.
WRONG order:
transition: ...;
-webkit-transition: ...;
RIGHT order:
-webkit-transition: ...;
transition: ...;
You can also just change all the transition declarations to read like this (it's not the background but the background-size that is changing:
transition: background-size .4s ease-in-out;
I know this is a old question, but using "all" works fine for me.
-webkit-transition: all 1s;
transition: all 1s;