I have img inside the div with exact width and height. I would like to place image there like background-size: cover to fill entire div so HTML is:
<div class="cover">
<img class=active src="http://pixabay.com/static/uploads/photo/2015/02/26/17/56/clock-650753_640.jpg" alt="time">
</div>
And CSS is:
.cover {
width: 400px;
height: 180px;
position: relative;
}
.cover img {
visibility: hidden;
opacity: 0;
width: 100%;
height: 100%;
object-fit: cover;
-webkit-transition:visibility 0s linear 4.0s,opacity 2.0s linear 2.0;
transition:visibility 0s linear 4.0s,opacity 2.0s linear 2.0s;
}
.cover img.active {
visibility: visible;
opacity: 1;
}
Here is live example http://jsfiddle.net/sytwrd9L/1/ - 2 seconds after load the image disappears. In the Firefox 36 it resizes the img during transition but in other browsers it works well. Any idea how to fix not to resize img during transition in FF?
I know this is an old question, but today I found a workaround for this issue. I've tested it in Firefox 44.0 so far.
The Workaround:
<!-- Apply the transition to this element -->
<div class="transition">
<!-- Apply a 3D translate to this element -->
<div class="translate3d">
<!-- Apply object-fit to this img elemnt -->
<img src="path/to/img.jpg" class="object-fit" />
</div>
</div>
Wouldn't applying translate3d(0,0,0) to an element inside a transitioned element prevent the GPU from accelerating the transition itself?
It's the transitioning element you want to accelerate, right? But you're applying the translate3d hack that invokes GPU acceleration to an element without transitions.
Related
Hoping to get some input. I've been taking some online courses on web development and I've been currently playing around adding images to an html page (3x3 image grid).
First I used the transform property so I could scale the image with a .5s transition to 1.5 times the size after hovering over it with a .5 second delay.
There is an issue with the images on the left and right sides as part of them go out of the screen after scaling.
To correct this I first tried position: relative; left: 6%; but learned that position is not a property you can animate, so the image immediately moves position and then scales after .5 seconds.
My second try was with object-position: 90px; , this property does work with the delay but after it scales it seems like a big chunk of the image is cropped.
I've been trying different properties like transform-origin which kind of works but gives it a glitchy feeling as it scales and then finishes moving the image to the side losing fluidity.
Can't seem to find helpful documentation on object-position as to why part of the image crops, this property gives a fluid scaling and positioning effect but I have that cropping issue.
Here is a link to a codepen where I have this exercise:
https://codepen.io/superavd88/pen/PoqYoRJ
Any ideas would be really appreciated as I've been trying to fix this issue for the last couple of days without success.
Thanks in advance.
Welcome to SO!
You just need to wrap you images indie div and make it overflow
hidden and also i changed the grid, using flex now
.thumbnail img {
-webkit-transform: scale(1);
transform: scale(1);
-webkit-transition: .3s ease-in-out;
transition: .3s ease-in-out;
width:100%;
}
.thumbnail .img-parent:hover img {
-webkit-transform: scale(1.3);
transform: scale(1.3);
}
.img-parent{
overflow:hidden;
width: 33%;
}
img{
width: 100%}
.thumbnail {
display: flex;
flex-wrap: wrap;
}
<div class="thumbnail">
<div class="img-parent">
<img src="https://www.washingtonpost.com/wp-apps/imrs.php?src=https://arc-anglerfish-washpost-prod-washpost.s3.amazonaws.com/public/HB4AT3D3IMI6TMPTWIZ74WAR54.jpg&w=767" alt="deep house cleaning">
</div>
<div class="img-parent">
<img src="https://www.washingtonpost.com/wp-apps/imrs.php?src=https://arc-anglerfish-washpost-prod-washpost.s3.amazonaws.com/public/HB4AT3D3IMI6TMPTWIZ74WAR54.jpg&w=767" alt="deep house cleaning">
</div>
<div class="img-parent">
<img src="https://www.washingtonpost.com/wp-apps/imrs.php?src=https://arc-anglerfish-washpost-prod-washpost.s3.amazonaws.com/public/HB4AT3D3IMI6TMPTWIZ74WAR54.jpg&w=767" alt="deep house cleaning">
</div>
<div class="img-parent">
<img src="https://www.washingtonpost.com/wp-apps/imrs.php?src=https://arc-anglerfish-washpost-prod-washpost.s3.amazonaws.com/public/HB4AT3D3IMI6TMPTWIZ74WAR54.jpg&w=767" alt="deep house cleaning">
</div>
</div>
Reference for how scale property work
change CSS styles
img {
width: 30%;
float: initial;
margin: 1rem;
transition: all .5s ease-in-out;
}
.leftimg:hover{
transform: scale(1.5);
}
.rightimg:hover{
transform: scale(1.5);
}
.center:hover{
transform: scale(1.5);
}
I'm trying to create a transition for both an image and a pseudo element of its container, but for some reason, these transitions appear to be out of sync with each other, resulting in the pseudo element reaching a opacity: 0 state before the image does.
I've tried various combinations of style rules, but I never managed to accomplish an ease-in-out transition to work correctly.
Here's some context for the current code: the image is padded on purpose, to provide a better visual (centered) of its content. The images that'll be used are always guaranteed to have a white background. That's why I'm using a pseudo-element with a white background color to fill the vertical spaces that the image does not cover.
The background-image should take the full width/height of the container and there are not guarantees of its background being white.
The desired effect is for both the pseudo-element and image to reach opacity: 0 at the same making it look like its a single element.
I'm also considering using an ::after pseudo element to provide a "loading skeleton" while the image is not retrieved from the server, but that's not a concern for now.
Here's the code, and the respective fiddle: https://jsfiddle.net/rjk2z31d/1/
*,
*::before,
*::after {
box-sizing: border-box;
}
.box {
width: 248px;
height: 320px;
}
.image-box {
position: relative;
display: block;
background-repeat: no-repeat;
background-size: cover;
line-height: 0;
background-color: #FFFFFF;
&::before {
display: block;
content: "";
width: 100%;
padding-top: (100% + (100% / 3));
}
img {
z-index: 1;
position: absolute;
top: 50%;
left: 0;
width: 100%;
transform: translate3d(0, -50%, 0);
padding: 16px 16px;
}
&::before, img {
background-color: #FFFFFF;
opacity: 1;
transition: all 1.5s ease-in-out;
}
&:hover {
&::before, img {
opacity: 0;
}
}
}
<div class="box">
<div class="image-box" style="background-image: url('https://via.placeholder.com/248x320/FF0000/000000?text=Background')">
<img src="https://via.placeholder.com/248x320/FFFFFF/000000?text=Image">
</div>
</div>
Actually, they both fade at the same speed.
The out-of-sync effect you're talking about is an illusion due to the opacities overlapping.
If you set the initial opacity of both elements, it's easier to understand what's going on.
The image's faded white added to the pseudo element's faded white looks less transparent than the pseudo element's faded white alone.
See it in effect with the below image:
If you need to be convinced of their synchronization, add a transform rule to the :hover handler. the result is synced as it is supposed to be.
As a workaround, I would suggest you to try figuring out a better approach than fading overlapping elements.
You could use the <picture> tag with css object-fit property to get rid of those blank spaces.
picture doc
object-fit doc
I'm working on a masonry type layout where I have a container with a column-count property, and then items with rounded corners that hold images. I want the images to give a little transform: scale on hover, but with this combination of css the rounded borders disappear during the transition.
Is there any way around it?
.container {
column-count: 2;
}
.item {
width: 100%;
overflow: hidden;
border-radius: 10px;
}
img {
max-width: 100%;
max-height: 100%;
transition: all 0.2s;
}
img:hover {
transform: scale(1.1);
}
<div class="container">
<div class="item">
<img src="https://images.unsplash.com/photo-1558834643-1dacaf774843?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60">
</div>
</div>
Update: As of late, this seems to be happening again, but only in elements using CSS columns. I've filed it as a Chromium bug here.
Update 2: Filed bug was closed as "Won't fix" as it has already been somewhere between v75.* and v77.0.3833.0, there's currently no available info on what caused it and what fixed it.
By default, transform does not trigger a repaint on the parent node at each animation frame, which is precisely why it's the recommended method of animation, along with opacity (which has the same behavior).
But in your case, you want this repaint after each frame. So you need to apply a cheap transform on the parent as well.
In your case, a simple rotate(0) will suffice, but note there are cases when you want to keep the 3d engine running, in which case a good candidate is rotateZ(0).
Additionally, to make sure there's no space between the bottom of the image and it's wrapper, you could apply display:block to the <img>:
.container {
column-count: 2;
}
.item {
width: 100%;
overflow: hidden;
border-radius: 10px;
transform: rotate(0);
}
img {
max-width: 100%;
max-height: 100%;
transition: all 0.2s;
display: block;
min-width: 100%;
}
img:hover {
transform: scale(1.1);
}
<div class="container">
<div class="item">
<img src="https://images.unsplash.com/photo-1558834643-1dacaf774843?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60">
</div>
</div>
Suggestion: since this doesn't seem to be directly related with using column-count, I suggest removing it from title, to increase its indexing and help others, with the same problem but not using column-count, find it easier.
I'd say it's strictly about parent border-radius not being applied during child item transform.
Here's the html simplified sample
<div class="big">
<a href=".....">
<img src=".....">
</a>
</div>
css
.big {
width: 33.3333%;
}
img:hover {
opacity: 0.9;
transition: opacity 0.4s;
}
When I hover above img, the opacity transition works fine, but the image flickering on span 0.4s, it's like resizing, like the Chrome recalculate the percentage size again within 0.4s.
Tried webkit transition, not fixing anything. Transition all, still happening.
This problem only happen on Chrome, no problem at all on Firefox.
Only happen when using percentage, with fixed width works fine, but I need to use percentage on this one.
Thanks for any help
You can try this:
a {display: block }
a img {transition: opacity 0.4s; max-width: 100%;}
a:hover img {opacity: 0.9}
This is what fixed it for me:
max-width: calc(100% - 1px);
max-height I could leave at 100%; even though it was the height that was changing.
I am using the method outlined here to fade in a background image on hover of an element.
My codepen example:
http://codepen.io/anon/pen/vqtjf
HTML:
<div><span></span></div>
CSS:
div {
position: relative;
width: 219px;
height: 218px;
background: url(https://dl.dropboxusercontent.com/u/3454522/home-option-icon-off.png) no-repeat;
}
span {
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
background: url(https://dl.dropboxusercontent.com/u/3454522/home-option-icon-energy.png) no-repeat;
opacity: 0;
-webkit-transition: opacity 0.5s;
-moz-transition: opacity 0.5s;
-o-transition: opacity 0.5s;
}
div:hover span {
opacity: 1;
}
The problem I'm having is that in Firefox (Mac) the background image of the span is not quite aligned with the background image of the span so when it fades in you can see a small movement (In the codepen the shift is vertical but in my project where the code is amongst a whole lot of other junk I actually had a horizontal shift). If you resize the Firefox window the problem is fixed.
A screencast of the effect can be seen here:
https://dl.dropboxusercontent.com/u/3454522/firefox-fadebg-problem.mp4
(View at 100% to see the problem which is subtle).
Any idea on whats causing this or how to fix?
I think it's a regression in how Firefox renders images with opacity animation, especially when the images has been resized with HTML width/height attributes (usually by more than half).
The effect can be very subtle like a slight off-setting (~1 px) or a kind of antialiasing.
STR:
1. Open the testcase I joined
2. Move the mouse over the images to animate the opacity
3. Try at different zoom levels to observe the off-setting/antialiasing
WORKAROUND: adding "box-shadow: #000 0em 0em 0em;" to images fixes the bad rendering.
source: https://bugzilla.mozilla.org/show_bug.cgi?id=745549
I had the same problem. Solved it by adding the following to the images css.
transform: translate3d(0,0,0);