Hover over an Image and another image appears - html

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>

Related

Creating a option-choice landing page

I want to create a landing page like a game. The visitor gets the option either to chose "Professioneel" or "Speels".
Telling it is easy but programming it is hard for me, so this is what I want:
2 div's with 2 different background-image when someone hover over one of the divs I want the background-image to scale (ONLY THE IMAGE) and the opacity placed on the div to change from 50% to 80%.
And a really nice future would be to display a snow falling gif over the image.
This is what I want to create:
Before
After:
What I have achieved till now is making the 2 divs with a background-image and I'm not even sure if that is the right way.
Can someone please help me out?
This is what happens when I hover with my current code: (the whole div scales, not only the image)
As an user asked, here some code:
#containerEntree {
height: 100vh;
width: 1920px;
padding-left: 0;
padding-right: 0;
}
#professioneelContainer {
background-color: red;
text-align: center;
width: 1920px;
height: 475px;
}
#speelsContainer {
background: red;
width: 100%;
height: 475px;
text-align: center;
}
.entreeTekst:hover {
transform: scale(1.2);
}
.entreeTekst {
width: 100%;
height: 100%;
position: relative;
transition: all .5s;
margin: auto;
}
.entreeTekst > span {
color: white;
/* Good thing we set a fallback color! */
font-size: 70px;
position: absolute;
}
<div class="container" id="containerEntree">
<div id="professioneelContainer">
<div class="entreeTekst">
<span>professioneel</span>
<img src="img/professioneel.jpg" />
</div>
</div>
<div id="speelsContainer">
<div class="entreeTekst">
<span>Speels</span>
<img src="img/speels.jpg" />
</div>
</div>
</div>
Please note that I'm still working on it so don't say that this (of course) won't work.
You can do this by using 2 divs with background images and use padding on the div to replicate the aspect ratio of the background image. Scale the image using background-size on :hover. Then use a pseudo element to create the color overlay and transition the opacity on :hover, then use the other pseudo element on top of that with the text and the "snow" gif as a background.
body {
width: 600px;
max-width: 80%;
margin: auto;
}
div {
background: url('https://static.tripping.com/uploads/image/0/5240/towns-funny-names-us_hero.jpg') center center no-repeat / 100%;
height: 0;
padding-bottom: 33.33333%;
position: relative;
transition: background-size .25s;
}
.speel {
background-image: url('http://www.luketingley.com/images/large/The-Punchbowl-Web-Pano.jpg');
}
div::after, div::before {
content: '';
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
}
div::before {
opacity: .5;
transition: opacity .25s;
}
.pro::before {
background: blue;
}
.speel::before {
background: red;
}
div::after {
display: flex;
align-items: center;
justify-content: center;
color: #fff;
font-family: sans-serif;
font-size: 1.5em;
font-weight: bold;
}
.pro::after {
content: 'PROFESSIONEEL';
}
.speel::after {
content: "SPEELS";
}
div:hover::after {
background: url('https://media.giphy.com/media/26BRyql7J3iOx875u/giphy.gif') center center no-repeat / cover;
}
div:hover::before {
opacity: 0.8;
}
div:hover {
background-size: 150%;
}
<div class="pro">
</div>
<div class="speel">
</div>
You can simply increase the background-size: height width; and opacity: value; property when you hover over an element. You can, if you want to, add some transition to make it smooth. This only scales the background image, not the div itself.
#d {
background-image: url(https://cdn.pixabay.com/photo/2016/10/29/20/52/cincinnati-1781540_960_720.png);
height: 200px;
width: 200px;
background-size: 100px 100px;
background-repeat: no-repeat;
background-position: center;
/*To make the transistion smooth*/
-o-transition:.5s;
-ms-transition:.5s;
-moz-transition:.5s;
-webkit-transition:.5s;
transition:.5s;
opacity: 0.5;
}
#d:hover {
background-size: 110px 110px;
opacity: 0.8;
}
<div id='d'>
</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>

Overlay image on hover using only css

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');
}

Change an other divs css on hover of a div

So i'm trying to change the hover effect of my divs. When I started the project I had it just the way I want it now, but back then I didn't want it that way, so I changed it. Now I can't seem to change it back.
so here is my code,
HTML
<div id="left">
<div id="leftImage">
//the background image
</div>
<div id="overlayLeft">
//a logo that goes on top of the image when not hovering over it
</div>
</div>
<div id="right">
<div id="rightImage">
//the background image
</div>
<div id="overlayRight">
//a logo that goes on top of the image when not hovering over it
</div>
</div>
CSS
body {
background: #ffff;
}
/* Setup for the halves of the screen */
#right
{
/* Set rules to fill background */
min-height: 100%;
min-width: 50%;
/* Set up proportionate scaling */
width: 50%;
height: auto;
position: fixed;
top: 0;
right: 0;
background: #389A7A;
background-size:cover;
}
#left
{
/* Set rules to fill background */
min-height: 100%;
min-width: 50%;
/* Set up proportionate scaling */
width: 50%;
height: auto;
position: fixed;
top: 0;
left: 0;
background: #0C4270;
background-size:cover;
}
/* Setup for the image containers */
#rightImage, #leftImage
{
opacity:1.00;
filter: alpha(opacity=100); /* For IE8 and earlier */
background: rgba(0, 0, 0, 0.15);
-webkit-transition: opacity 2.00s ease;
-moz-transition: opacity 2.00s ease;
transition: opacity 2.00s ease;
position: relative;
}
#rightImage:hover, #leftImage:hover
{
opacity:0.15;
filter: alpha(opacity=15); /* For IE8 and earlier */
background: rgba(0, 0, 0, 1.0);
}
#rightImage:hover + #overlayRight, #leftImage:hover + #overlayLeft
{
visibility: visible;
-webkit-transition: visibility 0.5s ease;
-moz-transition: visibility 0.5s ease;
transition: visibility 0.5s ease;
}
/* Setup for the images */
.rightImage
{
/* Set rules to fill background */
min-width: 50%;
min-height: 100%;
/* Set up proportionate scaling */
width: 50%;
height: auto;
/* Set up positioning */
position: fixed;
top: 0px;
right: 0;
}
.leftImage
{
/* Set rules to fill background */
min-width: 50%;
min-height: 100%;
/* Set up proportionate scaling */
width: 50%;
height: auto;
/* Set up positioning */
position: fixed;
top: 0px;
left: 0;
}
/* Setup for the logo image */
#overlayLeft
{
visibility: hidden;
}
.overlayLeft
{
/* Set rules to fill background */
min-width: 40%;
/* Set up proportionate scaling */
width: 40%;
/* Set up positioning */
position: absolute;
top: 35%;
left: 30%;
pointer-events: none;
}
#overlayRight
{
visibility: hidden;
}
.overlayRight
{
/* Set rules to fill background */
min-width: 40%;
/* Set up proportionate scaling */
width: 40%;
/* Set up positioning */
position: absolute;
top: 40%;
right: 30%;
pointer-events: none;
}
And here is the code in action: JsFiddle
So what i want to achieve is that when I hover the left div the effect that now happens ons the left div must happen on the right div. The piece of code that is making the hovering work is this the piece below:
#rightImage:hover, #leftImage:hover
{
opacity:0.15;
filter: alpha(opacity=15); /* For IE8 and earlier */
background: rgba(0, 0, 0, 1.0);
}
I tought one of the following operators would work: "+", "~", or just a simple space between :hover and the next div (#rightImage:hover #leftImage).
Yet I cannot seem to get it to work..
What am I doing wrong? Is it that the elements doesn't have the same parent? I tried adding a parent div arround the whole html. Yet that didn't work.
As far as I am aware hover states can only affect the element being hovered or child elements. You'll need to use some light javascript to achieve this.
You should be able to wrap both your #left and #right div's and then apply the hover style to that element. Like this:
<div class="wrapper">
<div id="left"> ... </div>
<div id="right"> ... </div>
</div>
and then ...
.wrapper:hover #rightImage, .wrapper:hover #leftImage{
opacity:1.00;
filter: alpha(opacity=100);
/* For IE8 and earlier */
background: rgba(0, 0, 0, 1.0);
}
See this fiddle.