html - CSS color transition behavior for images with transparent backgrounds -
i've noticed unanticipated effect of using css color transitions on image transparent background. here's example:
:root { --size: 4em; --duration: 5s; } html, body { margin: 0; background: slategray; color: white; } .main-menu { overflow: hidden; background: black; } .main-menu *:hover { background: skyblue; -webkit-transition-duration: 5s; transition-duration: var(--duration); } .image-div { float: right; padding: calc(var(--size) / 2); -webkit-transition-duration: 5s; transition-duration: var(--duration); } .image { max-width: var(--size); } <div class="main-menu"> <div class="image-div"> <img class="image" src="https://s4.postimg.org/5zy6kjqcd/maximize.png"/> </div> </div> to summarize, issue this. if hover on image-div div's padding, background color of div , contained image div execute color transition @ same rate, expected. however, if hover on image div, color appears transition faster image-div div's color.
given fact able reproduce exact behavior on firefox, chrome, safari , edge, feeling expected behavior, understand why happening.
when hover on img two hover events triggered - 1 on img , 1 on parent image-div when use * in .main-menu *:hover selector:
instead use hover only on image-div below:
.main-menu .image-div:hover { background: skyblue; } and difference in transition not there - see demo below:
html, body { margin: 0; background: slategray; color: white; } .main-menu { overflow: hidden; background: black; } .main-menu .image-div:hover { background: skyblue; } .image-div { float: right; padding: calc(4em / 2); -webkit-transition-duration: 5s; transition-duration: 5s; } .image { max-width: 4em; } <div class="main-menu"> <div class="image-div"> <img class="image" src="https://s4.postimg.org/5zy6kjqcd/maximize.png"/> </div> </div> wiki
Comments
Post a Comment