Overlay image on hover using only css - html

I'm trying to overlay an image with a smaller image that has the background opacity of the first image when hover, using only css because i'm not able to edit the html.
Here is a sample HTML:
<div class="image">
<a href="http://www.example.com">
<img src="/uploads/2016/08/img1.png" class="rggclImgCenter">
</a>
</div>
Using CSS only, i thought would be something like this:
.image:hover {
background-image: url('https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-ios7-arrow-right-128.png');
}
But it will only replace the image.

You could use the css pseudo element if you wouldn't like to (or cannot) modify the html.
Please be awared you cannot use :before or :after on img element
Here is the CSS:
img { max-width: 300px; } /* the image dimension */
.image a { position: relative; display: inline-block; } /* allow :before element positioning easier */
.image a:hover:before {
position: absolute;
top: 30px; left: 40px; /* Where to put the overlay */
display: inline-block;
content: ''; /* must have */
width: 300px; height: 164px; /* size of the element */
background-image: url('https://i.kinja-img.com/gawker-media/image/upload/s--pEKSmwzm--/c_scale,fl_progressive,q_80,w_800/1414228815325188681.jpg'); /* URL of the image */
background-size: 300px 164px; /* Resize the image as this dimension */
opacity: .5; /* Transparent rate */
}
You can try it on https://jsfiddle.net/bq9khtz3/

Did not have the original image so used an image of my own. See if this helps.
You can also refer to this link: https://jsfiddle.net/askptx0y/
.image:hover {
background-image: url('https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-ios7-arrow-right-128.png');
background-repeat: no-repeat;
opacity: 1;
}
img:hover {
opacity: 0.5;
}
<div class="image">
<a href="http://www.example.com">
<img src="http://www.freedigitalphotos.net/images/img/homepage/87357.jpg" class="rggclImgCenter">
</a>
</div>

Try adding a :before element on :hover.
.image a:hover:before {
content: '';
display: block;
position: absolute;
background-image: url('https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-ios7-arrow-right-128.png');
}

Make image transparency:
.image a:hover image {
opacity: 0;
}
Show your background image
.image a:hover {
background-image: url('https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-ios7-arrow-right-128.png');
/* add other properties */
}

.image:hover img {
-webkit-filter: brightness(40%);
-moz-filter: brightness(40%);
-ms-filter: brightness(40%);
-o-filter: brightness(40%);
}
.image:hover img:after {
content: '';
display: block;
position: absolute;
background-image: url('https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-ios7-arrow-right-128.png');
}

Related

How to add an image as a text background using CSS pseudo element (:after)

Is it possible to use a pseudo-element as a background for an HTML text element (h1)?
I know that the simple way of using an image as a text background is using the background-clip: text; property directly, but in my case, I need to apply some additional css properties to the background image (rotation and maybe some filter properties as well), and because of this, I cannot apply those css properties directly, as they will be applied to the text element as well.
I think that :after pseudo element can do this, but I don't have any success.
Let's say that this is my h1 wrapped in a div:
<div class="wrapper">
<h1>My title that should have an image background</h1>
</div>
And this is the css I've tried:
.wrapper {
width: 100vw;
height: 400px;
}
.wrapper h1 {
position: relative;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.wrapper h1:after {
content: '';
position: absolute;
background-image: url(https://picsum.photos/200);
background-position: 50% 50%;
background-repeat: no-repeat;
transform: rotate(-3deg); /* the image is required to be rotated */
background-size: cover;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
https://jsfiddle.net/8ztcuv12/
The expected resultL
Is there a better way of achieving this? Any solutions are welcome.
You can use backround-image on wrapper so that if you want to rotate heading.
It don't rotate image too.
.wrapper {
width: 100vw;
height: 100px;
position: relative;
}
.wrapper h1 {
transform: rotate(-3deg);
}
.wrapper:after {
content: '';
position: absolute;
background-image: url(https://picsum.photos/200);
background-position: 50% 50%;
background-repeat: no-repeat;
transform: rotate(-3deg);
/* the image is required to be rotated */
background-size: cover;
left: 0;
top: 0;
width: 100%;
height: 100%;
z-index: -1;
}
<div class="wrapper">
<h1>My title that should have an image background</h1>
</div>

How to "tint" image with css

I try to tint an image with the background attribute like this:
.image-holder:hover {
opacity: 1;
transition: opacity 1s, background 1s;
background: #EBEFF7;
}
.image-holder {
height: 250px;
width: 200px;
opacity: 0.5;
transition: opacity 1s, background 1s;
}
<div class="image-holder">
<img src="https://dummyimage.com/200x200/fff/000000.png" />
</div>
http://jsfiddle.net/6ELSF/1047/
But the image is not "tinted" like expected.
On hover it looks like this:
but I want it to look like this:
I tried to test some solution I found regarding overlay of images but neither worked in my example.
How do accomplish this in the least complicated manner?
I used some combined filters for tinting an image completely. Tinting is not possible directly (with filters), but you can paint it sepia, adapt saturation and brightness, and get the desired color by using hue-rotate... I think it was something like this ...
img {
filter: sepia(100%) saturate(300%) brightness(70%) hue-rotate(180deg);
}
You will need to adapt it to your needs.
Depending on your browser support use filter, many options at your disposal, caniuse.com looks promising http://caniuse.com/#search=css%20filter :-
filter: blur(5px);
filter: brightness(0.4);
filter: contrast(200%);
filter: drop-shadow(16px 16px 20px blue);
filter: grayscale(50%);
filter: hue-rotate(90deg);
filter: invert(75%);
filter: opacity(25%);
filter: saturate(30%);
filter: sepia(60%);
Using :before selector you can tint images with different colors
.image-holder {
height: 200px;
width: 200px;
position:relative;
}
.image-holder:before {
content: "";
display: block;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: rgba(0,255,255, 0.5);
transition: all .3s linear;
}
.image-holder:hover:before {
background: none;
}
<div class="image-holder">
<img src="http://lorempixel.com/200/200" />
</div>
You can achieve this using mix-blend-mode which currently has ~88% support. You can use the same markup as before.
<div class="image-holder">
<img src="https://dummyimage.com/200x200/fff/000000.png" />
</div>
But use this css:
div.image-holder {
transition: background-color .2s;
width: min-content;
}
div.image-holder:hover {
background-color: #EBEFF7;
}
img {
display: block;
/* Blend with parents background: */
mix-blend-mode: multiply;
}
For this demo you are wanting to tint whites towards your chosen color so you want to use the multiply blend mode. If you are wanting to tint blacks then use the screen blend mode.
Codepen Demo
Changing the opacity of the parent container changes all children. make a separate div to control your tint. I hammered something together, but the essentials are there.
.image-holder {
position: relative;
max-height: 250px;
max-width: 200px;
}
.image-holder img {
display: block;
opacity: 0.5;
max-width: 100%;
max-height: inherit;
}
.tint {
position: absolute;
max-height: 250px;
max-width: 200px;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: 0;
opacity: 0;
background: #00f;
transition: opacity 1s;
}
.image-holder:hover .tint {
opacity: 1;
}
<div class="image-holder">
<div class='tint'></div>
<img src="http://lorempixel.com/200/200" />
</div>
It's not exactly a real tint but this is the way I find easier to achieve a color layer over an image. The trick is to use an absolute layer with rgba color over an image. It works perfectly for my general cases.
Have a go!
.mycontainer{
position:relative;
width:50%;
margin:auto;
}
.overtint {
position:absolute;
background-color: rgba(255,0,0,0.6);
width:100%; height:100%;
}
img{ width:100%;}
a:hover .overtint {
background-color: rgba(0,255,0,0.6);
transition-duration: .5s;
}
<div class="mycontainer">
<a href="#">
<div class="overtint"></div>
<img src="https://via.placeholder.com/300x150">
</a>
</div>

how does one put a background on a floating element in css3 and change it's opacity?

In the css I have a few images in the style folder so ignore the other images that don't show up. The floating element leftpara and joinbutton(eventhough it's a para not the button) have background white and i want to give it an opacity of .4.
#stuff:before{
display: block; content:""; position: absolute; z-index:-1;
background: url(blah.jpg);
opacity:.3;
top:10%; left: 0; right: 0;
height: 50%;
}
fiddle
You can try this code:
withhsla(0,0%,100%,0.70) or rgba you use a white background with whatever percentage saturation or opacity to get the look you desire.
and you can use filter: alpha(opacity=50); to get the opacity
background-attachment: fixed;
background-image: url(blah.jpg);
display: block;
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
z-index: 1;
background-color: hsla(0,0%,100%,0.70);
background-blend-mode: overlay;
background-repeat: no-repeat;
filter: alpha(opacity=50);
Your question is slightly vague, however the following code applies "a background on a floating element in css3 and changes its opacity?
div{
float: right;
display:block;
width: 350px;
height: 150px;
background-image: url(http://placehold.it/350x150/ff0000/ffffff);
opacity: 0.5;
}
<div></div>

Hover over an Image and another image appears

Hi i need help with some css/html
i want to hover over an image and then another image/text appears with the background image becoming opacity :0.5
<style>
.your-img {
width: 344px; /* your image width and height here */
height: 857px;
background-image: url('images/men.png');
}
.your-hover {
width: 341px;
height: 225px;
opacity: 0;
filter: alpha(opacity = 0);
background-image: url('images/men2.png');
align:bottom;
}
.your-hover:hover {
opacity: 0.5;
filter: alpha(opacity = 50);
}
</style>
<div class="your-img">
<div class="your-hover"></div>
</div>
this is the code i am using. men.png is the main image and the men2.png is the image that should appear when i hover over men.png.
but when i hover over men.png its opacity is 1 and the hovered image is 0.5
how do i make the background image 0.5 and the hovered image 1
As far as I know, there isn't a CSS only way to lower the opacity of the background image only. Also, the hover will have to be on the parent as the child cannot affect the parent's styling.
The tricky part is that if you lower the opacity of the parent, the child will also have its opacity lowered. To get around this, you can play with :after and applying a background: rgba() doing something like:
JS Fiddle
.your-img {
width: 344px;
/* your image width and height here */
height: 857px;
background-image: url('images/men.png');
position: relative;
}
.your-img:after {
content: '';
top: 0;
bottom: 0;
width: 100%;
display: inline-block;
background: rgba(0, 0, 0, 0);
position: absolute;
}
.your-hover {
width: 341px;
height: 225px;
opacity: 0;
filter: alpha(opacity=0);
background-image: url('images/men2.png');
align: bottom;
display: none;
}
.your-img:hover::after {
background: rgba(0, 0, 0, .5);
}
.your-img:hover .your-hover {
display: block;
opacity: 1;
filter: alpha(opacity=50);
}
if i understand the question correctly, you want hovering on the outer div to show the inner?
you just need to add a css rule for outer:hover > inner
.your-img:hover > .your-hover {
opacity: 0.5;
filter: alpha(opacity = 50);
}
Example
Hello you can simplify your code a lot but just having this css. There is no need to create a separate hover div. When you apply the pseudo class :hover to the .your-img class all you need to do is set the opacity there and the new url to your other background image.
.your-img {
width: 344px; /* your image width and height here */
height: 857px;
background-image: url('http://placehold.it/350x150');
}
.your-img:hover {
width: 341px;
height: 225px;
background-image: url('http://placehold.it/350x150');
opacity: 0.5;
Here is a fiddle to illustrate https://jsfiddle.net/gward90/25bcmmhb/
Your code has quite a few errors, particularly where the hover is. You need to have the hover on the parent. Also, in your code youu are trying to make the parent go to 0.5 opacity and the child to 1. This isn't possible, the child is inside the parent, and it will already be 0.5 opacity because the parent is, so you will never get it back to 1.
I have left 2 scripts for you to try below, the first is a modified version of yours which should get the hover working, but you will have issues with the opacity as i described above, the second is a modified html and style to give you an example of how it should be laid out.
Example
.your-img {
width: 344px; /* your image width and height here */
height: 857px;
background-image: url('images/men.png');
}
.your-img:hover .your-hover{
opacity: 0.5;
filter: alpha(opacity = 50);
}
.your-hover {
width: 341px;
height: 225px;
opacity: 0;
filter: alpha(opacity = 0);
background-image: url('images/men2.png');
align:bottom;
}
</style>
<div class="your-img">
<div class="your-hover"></div>
</div>
<style>
.parent {
width: 344px; /* your image width and height here */
height: 857px;
position: relative;
background-image: url('images/men.png');
}
.parent:hover .img-1{
opacity: 0.5;
filter: alpha(opacity = 50);
}
.parent:hover .img-2{
opacity: 0.5;
filter: alpha(opacity = 50);
}
.img-1 {
background-image: url('images/men.png');
position: absolute;
height: 100%;
width: 100%;
}
.img-2 {
background-image: url('images/men2.png');
position: absolute;
opacity: 0;
filter: alpha(opacity = 0);
width: 341px;
height: 225px;
bottom: 0;
}
</style>
<div class="parent">
<div class="img-1"></div>
<div class="img-2"></div>
</div>

Changing image on hover without using an extra div

I'm trying to make my footer img change to a different img on hover, I've accomplished this by creating each img as a div, however, I'm trying to avoid this, is there any other possible way to do this?
Current CSS:
.footerLeft {
float:left;
width: 50%;
height: 100px;
color: #fff;
background-color: #33383b;
}
.footerLeft p {
margin-left: 25px;
}
.footerLeft img {
width: 175px;
height: 50px;
padding-left: 25px;
margin-top: 10px;
}
.footerRight {
float:right;
width: 50%;
height: 100px;
color: #fff;
background-color: #33383b;
}
.footerRight img {
width: 35px;
height: 35px;
}
.footerRight p {
text-align: right;
position:relative;
margin-top: -20px;
margin-right: 20px;
}
Current HTML:
<div class="footerLeft">
<img src="img/logo.png">
<p>Sharpturn Network© 2016</p>
</div>
<div class="footerRight">
<ul id="menu">
<li><img src="img/footer/facebook.png"></li>
<li><img src="img/footer/twitter.png"></li>
<li><img src="img/footer/youtube.png"></li>
</ul>
<p>Designed by Ryan Williams</p>
</div>
Set one img as the background img, and the other as the background img on hover.
footer {
background-image: url(...);
}
footer:hover {
background-image: url(...);
}
The only alternative I can think of to using a div (or some other element with background image) is to use JavaScript. This will allow you to change the 'src' of the image on hover.
element.setAttribute('src', 'http://www.example.com/image.jpg');
As #partians said, just added "background-size" as cover, I assumed that you need that.
footer {
width: 900px;
height: 600px;
background-image: url("http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg");
background-size: cover
}
footer:hover {
background-image: url("http://image2.redbull.com/rbcom/010/2013-07-25/1331603705670_2/0010/1/900/600/2/red-bull-illume.jpg");
}
<footer> </footer>
try this.just copy and pastea and try it.only have to replace your two images.
<!DOCTYPE html>
<html>
<head>
<title>hover</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<style type="text/css">
body{
margin:0;
padding:0;
}
.imageOne{
width: 500px;
height: 500px;
box-shadow: 1px 2px 1px black;
background-image: url(http://www.ron-gatepain.com/images/Golden_Gate_Bridge.JPG?306);
background-repeat: no-repeat;
}
.imageTwo{
width: 500px;
height: 500px;
box-shadow: 1px 2px 1px black;
background-image: url(http://gym1526-english.narod.ru/images/Statue.jpg);
background-repeat: no-repeat;
}
</style>
</head>
<body>
<div id="myimage" class="imageOne">
</div>
<script type="text/javascript">
$(document).ready(function(){
$("#myimage").mouseenter(function(){
$(this).addClass("imageTwo").removeClass("imageOne");
});
$("#myimage").mouseleave(function(){
$(this).addClass("imageOne").removeClass("imageTwo");
});
});
</script>
</body>
</html>
EDIT: Note that some of the other answers will change the image when hovering any part of the whole footer container. This method changes the image only when hovering the image's region.
You could declare a pseudo-element on the footer container in the same position as the image to replace it on hover. Basically, using a combination of opacity and z-index you can hide the original image and show the pseudo element in the same place.
Here is an example of how to accomplish that:
.footerLeft:before {
/* this creates the pseudo-element */
width: 175px; /* Same as img */
height: 50px; /* Same as img */
margin-left: 25px; /* Same as img */
margin-top: 10px; /* Same as img */
background: url(http://lorempixel.com/175/50/cats); /* your replacement img */
position: absolute;
z-index: 0;
content: '';
}
.footerLeft img {
width: 175px;
height: 50px;
margin-left: 25px; /* I used margin-left instead of padding-left */
margin-top: 10px;
position: relative; /* necessary for the z-index to work */
z-index: 1; /* puts the img above the pseudo-element by default */
}
.footerLeft img:hover {
opacity: 0; /* hides the original image on hover */
}
EDIT 2: If you want the social media icons to change images on hover, the process is similar. Create pseudo elements on each a tag with CSS, with the same dimensions as the original img. Configure a different replacement image for each icon:
.footerRight img {
width: 35px;
height: 35px;
position: relative; /* enables the use of z-index */
z-index: 1; /* puts the image on top by default */
transition: opacity .2s; /* remove this for no smooth transtion */
}
.footerRight a:before {
width: 35px; /* same a img */
height: 35px; /* same a img */
z-index: 0;
content: '';
position: absolute;
}
.footerRight li:nth-child(1) a:before {
/* facebook icon */
background: url(http://lorempixel.com/35/35/cats);
}
.footerRight li:nth-child(2) a:before {
/* twitter icon */
background: url(http://lorempixel.com/35/36/cats);
}
.footerRight li:nth-child(3) a:before {
/* youtube icon */
background: url(http://lorempixel.com/36/35/cats);
}
.footerRight img:hover {
opacity: 0; /* hides the original image */
}
Note: This method has the advantage of loading the replacement images immediately. The same might not be true for other methods.
You can see the working example here: http://codepen.io/wilman/pen/mVZVzg