css - HTML Text Appears Over Image On Hover -
i have been struggling while trying images animate when hover on them. have found lots of examples, have found them difficult implement , adjust how want them. here code:
#box { width: 100%; border-radius: 5px; box-shadow: inset 1px 1px 40px 0 rgba(0, 0, 0, .45); background: url("http://placehold.it/1080x720"); background-size: cover; } #cover { background: rgba(0, 0, 0, .75); text-align: center; opacity: 0; -webkit-transition: opacity 1s ease; -moz-transition: opacity 1s ease; } #box:hover #cover { opacity: 1; } #text { font-family: arial; font-weight: bold; color: rgba(255, 255, 255, .84); font-size: 60px; }
<div id="box"> <div id="cover"> <span id="text">comics</span> </div> </div>
my difficulty comes when setting height, website needs able re-size image; before set width of image (rather background image now) 100% , make height correct width. using background image cant set height automatically, takes size of text appears when hover over. hope makes sense, thanks!
edit suppose if isn't going work can point in way of method same thing?
update happened when tried example kaan.
#box { width: 100%; border-radius: 5px; box-shadow: inset 1px 1px 40px 0 rgba(0, 0, 0, .45); background: url("http://placehold.it/1080x720") no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; } #cover { background: rgba(0, 0, 0, .75); text-align: center; opacity: 0; -webkit-transition: opacity 1s ease; -moz-transition: opacity 1s ease; } #box:hover #cover { opacity: 1; } #text { font-family: arial; font-weight: bold; color: rgba(255, 255, 255, .84); font-size: 60px; } #containertest{ width: 40%; }
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script> <script> $(window).load(function(){ var height = $(window).height(); // returns height of browser viewport $("#box").css({'height': height + 'px'}); $("#cover").css({'height': height + 'px'}); }); function resizecontent(){ var newwidth = $(window).width(); var newheight = $(window).height(); $("#box").height(newheight).width(newwidth); $("#cover").height(newheight).width(newwidth); } $(window).resize(function() { resizecontent(); }); </script> <div id="containertest"> <div id="box"> <div id="cover"> <span id="text">comics</span> </div> </div> </div>
that did work, need make myself more clear want do. see have container 40% of screen, want image fit inside can see whole thing setting width of image 100%, , height adjust image still right height width, not distorted. when normal image automatically, setting width, background not.
by using jquery, can solve problem.
$(window).load(function(){ var height = $(window).height(); // returns height of browser viewport $("#box").css({'height': height + 'px'}); $("#cover").css({'height': height + 'px'}); }); function resizecontent(){ var newwidth = $(window).width(); var newheight = $(window).height(); $("#box").height(newheight).width(newwidth); $("#cover").height(newheight).width(newwidth); } $(window).resize(function() { resizecontent(); });
you need change #box css code below.
#box { width: 100%; border-radius: 5px; box-shadow: inset 1px 1px 40px 0 rgba(0, 0, 0, .45); background: url("http://placehold.it/1080x720") no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; }
it work. here jsfiddle link of solution: https://jsfiddle.net/fl0n3mcj/1/
Comments
Post a Comment