Trying to use hover on a element below an absolute positioned element - html

So I'm trying to use a hover effect on two elements. One of the elements has a absolute position, this element needs to have the background recolored once hovered. This seems to work, however I cannot get the back image to scale accordingly. I think it has to do with the absolute positioning of the first div. But I cannot find a way to fix this. I have been trying to fix it using pointer-event but I cannot get that to do the trick.
<style>
.master {
width: 1200px;
overflow: hidden;
position: relative;
}
img {
width: 100%;
}
img:hover {
transform: scale(1.2)
}
.hover {
position: absolute;
height: 100%;
width: 100%;
}
.hover:hover {
background: rgba(255, 0, 0, 0.5);
}
</style>
<div class="master">
<div class="hover">
</div>
<img src="http://via.placeholder.com/1200x350" alt="">
</div>
This is the code I have got right now

You can't get hover effects on two stacked elements at once. However, if you want to be able to hover the element which is behind another element, you can use pointer-events: none; on the element in the foreground, allowing mouse actions to "go through it" and therefore affect the element in the back:
<style>
.master {
width: 1200px;
overflow: hidden;
position: relative;
}
img {
width: 100%;
}
img:hover {
transform: scale(1.2)
}
.hover {
position: absolute;
height: 100%;
width: 100%;
pointer-events: none;
}
.hover:hover {
background: rgba(255, 0, 0, 0.5);
}
</style>
<div class="master">
<div class="hover">
</div>
<img src="http://via.placeholder.com/1200x350" alt="">
</div>

You can add pointers-event: none; to img and your pointer will ignore the image but that's only in case you don't have any plan to add some mouse actions on image later

You're correct that you're not getting the hover event on the image because the absolutely positioned <div> is covering it. But since the <img> element immediately follows the <div> element in your HTML hierarchy, you can use the Adjacent Sibling Combinator to target it.
<style>
.master {
width: 1200px;
overflow: hidden;
position: relative;
}
img {
width: 100%;
}
.hover {
position: absolute;
height: 100%;
width: 100%;
z-index: 1;
}
.hover:hover {
background: rgba(255, 0, 0, 0.5);
}
.hover:hover + img {
transform: scale(1.2)
}
</style>
<div class="master">
<div class="hover">
</div>
<img src="http://via.placeholder.com/1200x350" alt="">
</div>

I have found out that moving the hover effect to .master instead of the seperate elements seem to work.
<style>
.master {
width: 1200px;
overflow: hidden;
position: relative;
}
img {
width: 100%;
z-index: 50;
}
.master:hover img{
transform: scale(1.2)
}
.hover {
position: absolute;
height: 100%;
width: 100%;
opacity: 0.8;
z-index: 100;
}
.master:hover .hover {
background: rgba(255,0,0, 0.5);
}
</style>
<div class="master">
<div class="hover">
test
</div>
<img src="http://via.placeholder.com/1200x350" alt="">
</div>

There is no problem between hover and absolut position.
Your problem is that img tag is place on top on .hover cause of the absolute position.
So your hover div is behind your image and the mouse over is intercepted by image tag.
Hope it's help

You need to set the :hover to an absolute element with class .hover:
.hover:hover + img {
transform: scale(1.2);
}
Also, set z-index for the same class without a :hover.
In addition, I've added transition for transform and background.
.master {
width: 1200px;
overflow: hidden;
position: relative;
display: inline-flex;
}
.hover {
position: absolute;
height: 100%;
width: 100%;
z-index: 9999;
transition: background .5s;
}
img {
width: 100%;
transition: transform .5s;
}
.hover:hover + img {
transform: scale(1.2);
}
.hover:hover {
background: rgba(255, 0, 0, 0.5);
}
<div class="master">
<div class="hover"></div>
<img src="http://via.placeholder.com/1200x350" alt="">
</div>

Related

Placing two different images on hover with two center images side by side

I am fairly new to HTML in the past month. I cannot for the life of me, figure out how to change the second image on hover to be a different image when the mouse hovers over it. I know some of the code probably looks dumb with how I tried to guess how I could possibly alter the second hover image. But I am quite confused. If anyone could help that would be great. The only progress I made so far is finally getting them perfectly aligned the way I would want them in the center and also the smooth transition to the hover. All that is left is being stumped on how to change the image to a different one when you hover over the second image. I do not want both hover images to be the same.
* {
background-color: coral;
}
.container {
position: relative;
width: 50%;
overflow: hidden;
display: table-cell;
border: 1px solid transparent;
/* a way to add a space around */
}
#media screen and (max-width:480px) {
.container {
/* make them full-width and one-a-row */
width: 100%;
display: block;
}
}
.image {
display: table-cell;
width: 100%;
height: auto;
transition: all .4s ease-in;
}
.overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
transition: .5s ease;
background-image: url("sketchcollage.JPG");
color: white;
text-align: center;
padding-top: 40%;
}
.overlay .overlay2 {
background-image: url("digitalartcollage.JPG");
}
a {
color: white;
}
.container:hover .overlay {
opacity: 1;
}
.container:hover .image {
transform: scale(1.2);
-webkit-transform: scale(1.2);
}
h1 {
text-align: center;
font-size: 72px;
background: -webkit-linear-gradient(rgb(12, 215, 230), rgb(170, 9, 130));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
<h1> Who is Rosalyn? </h1>
<div class="container">
<a href="https://trezoro.co">
<img src="https://via.placeholder.com/500" alt="Le Tricolore Smartwatch" class="image">
<div class="overlay">
<p>Entire element is the link here</p>
</div>
</a>
</div>
<div class="container">
<img src="https://via.placeholder.com/500" alt="Le Tricolore Smartwatch" class="image">
<div class="overlay">
<a href="https://trezoro.co">
</a>
</div>
<div class="overlay2">
<p>Only the text is a link </p>
</div>
</div>
I don't know what is p tags are for, so I removed those. Also, I used a div with background-image instead img tag. when you hover on the container, the image changes.
* {
background-color: coral;
}
.flex{
display: flex;
flex-wrap: wrap;
justify-content: space-between;
height: 50vh;
}
.container {
position: relative;
width: 48%;
overflow: hidden;
}
#media screen and (max-width:480px) {
.container {
width: 100%;
margin-top: 20px;
}
.flex{
height: 100vh;
}
}
.img{
background-size: 100% 100%;
transition: all .4s ease-in;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
opacity: 1;
}
.img1{
background-image: url('https://s4.uupload.ir/files/5c29cf910a706_8m.jpg');
}
.img2{
background-image: url('https://s4.uupload.ir/files/717195_346_g0du.jpg');
}
a {
color: white;
}
.container:hover .img {
transform: scale(1.2);
-webkit-transform: scale(1.2);
opacity: 0.5;
}
.container:hover .img1{
background-image: url('https://s4.uupload.ir/files/0.270967001322580170_jazzaab_ir_ajvv.jpg');
}
.container:hover .img2{
background-image: url('https://s4.uupload.ir/files/7560b48482bfae5c-02b97ffc647f-3822363654_tji3.jpg');
}
h1 {
text-align: center;
font-size: 72px;
background: -webkit-linear-gradient(rgb(12, 215, 230), rgb(170, 9, 130));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
<h1> Who is Rosalyn? </h1>
<div class="flex">
<div class="container">
<a href="https://trezoro.co">
<div class="img img1"></div>
</a>
</div>
<div class="container">
<div class="img img2"></div>
</div>
</div>
QUESTION
How to change the second image on hover to be a different image when the mouse hovers over it?
ANSWER
The approach of this question is to change an image when the user hovering the mouse over it. This task can be simply done by using the CSS background-image property in combination with the :hover pseudo-class to replace or change the image on mouseover.
.changeImg:hover {
background-image:
url("https://images.app.goo.gl/gfRnCCBPH6r4v3kp6");
}

CSS: image with bigger size than the parent but cut only on horizontal sides

I have simple CSS code which scale image inside of parent element.
Image when scaling itself is bigger than a parent. Now I need to cut bigger horizontal sides but the top side will be outside of the parent. For a better understanding look at an image.
In this image is a hover statement which I need to get:
IMAGE
I already tried on parent overflow: hidden but the top side will be cut too.
Like I said I need to get a hover statement like is in image preview instead of my in example code. Is there any option on how I can get it?
.home-treneri {
padding: 56px;
}
.home-treneri-container {
max-width: 1200px;
margin: 0 auto;
display: flex;
flex-flow: row wrap;
}
img {
width: 100%;
transition: all 0.3s ease-in-out;
position: absolute;
left: 50%;
transform: translateX(-50%);
bottom: 0;
}
.background {
position: relative;
background-color: rgba(0, 0, 0, 0.5);
width: 300px;
height: 285px;
}
.trener-card {
position: relative;
cursor: pointer;
}
.trener-card:hover img {
width: 110%;
}
<section class="home-treneri">
<div class="home-treneri-container">
<div class="trener-card">
<div class="background">
<img src="https://lh3.googleusercontent.com/proxy/VpiwIPSxe7FnIAm7aWS7GiB76GDhXeTjqbIst6g0dHYaXWZEyaQ6hfbUqVEuLFqZwG7lsygIjEgf1SQ338Z0djShjmotcVgw5sTQg0Ltf638227HVN7ok3UlIiaUYycmTnJ27hAB055TWk0">
</div>
</div>
</div>
</section>
Adding clip-path: inset(-50px 0 0 0); to your trener-card class will give you the desired result.
What it basically does is clipping the image. On the top you allow the image to grow up to 50px (-50px), while on the other 3 sides you are saying that the image will be clipped (0 0 0)
.home-treneri {
padding: 56px;
}
.home-treneri-container {
max-width: 1200px;
margin: 0 auto;
display: flex;
flex-flow: row wrap;
}
img {
width: 100%;
transition: all 0.3s ease-in-out;
position: absolute;
left: 50%;
transform: translateX(-50%);
bottom: 0;
}
.background {
position: relative;
background-color: rgba(0, 0, 0, 0.5);
width: 300px;
height: 285px;
}
.trener-card {
position: relative;
cursor: pointer;
clip-path: inset(-50px 0 0 0);
}
.trener-card:hover img {
width: 110%;
}
<section class="home-treneri">
<div class="home-treneri-container">
<div class="trener-card">
<div class="background">
<img src="https://lh3.googleusercontent.com/proxy/VpiwIPSxe7FnIAm7aWS7GiB76GDhXeTjqbIst6g0dHYaXWZEyaQ6hfbUqVEuLFqZwG7lsygIjEgf1SQ338Z0djShjmotcVgw5sTQg0Ltf638227HVN7ok3UlIiaUYycmTnJ27hAB055TWk0">
</div>
</div>
</div>
</section>
You should try overflow-x: hidden on parent.

CSS Image not scaling on hover

I've made a responsive image grid and am trying to add a hover effect to it so that the image gets a dark overlay and some text fades in on it. However, I've been having a tough time implementing it.
Here's my HTML structure.
<div class="tile">
<img src="some_image" class="animate">
<div class="overlay">
<p>Mahatma Gandhi</p>
</div>
And here's my CSS
.gallery .row .tile:hover ~ .tile img {
transform: scale(1.2);
}
However upon hovering over the image, it does not have the expected behaviour.
What's wrong?
EDIT
I got the hover effect to work and I can now fade in text.
Here's my code for that:
<div class="tile">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d6/Tagore_Gandhi.jpg/220px-Tagore_Gandhi.jpg" class="animate">
<div class="overlay">
<div class="text">Mahatma Gandhi</div>
</div>
</div>
CSS
.tile {
position: relative;
width: 100%;
}
.image {
display: block;
width: 100%;
height: auto;
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: .5s ease;
background-color: rgba(0, 0, 0, 0.8);
}
.tile:hover .overlay {
opacity: 1;
}
This seems to work but I think it doesnt have a certain "feel" to it. So I need to add a scale effect to the image. How can I do that
Here is a jsFiddle that i think will help you to resolve your issue: https://jsfiddle.net/mcs3yn1x/
HTML
<div class="tile">
<img src="https://picsum.photos/200/300" class="animate">
<div class="overlay">
<p>Mahatma Gandhi</p>
</div>
CSS
.tile {
border: 2px solid black;
}
.tile:hover img {
transform: scale(1.2);
}
Edit
After hearing alittle more about your issue I have created the following jsFiddle: https://jsfiddle.net/f1gzonjr/4/
HTML
<div class="tile">
<div class="container">
<img src="https://picsum.photos/200/300" class="animate">
<div class="overlay">
<p>Mahatma Gandhi</p>
</div>
</div>
CSS
.tile {
position: relative;
top: 0px;
left: 0px;
height: 300px;
width: 200px;
overflow: hidden;
border: 2px solid black;
}
.container:hover img{
transform: scale(1.2);
}
.overlay{
position: absolute;
display: none;
top: 0px;
left: 0px;
height: 100%;
width: 100%;
background-color: rgba(0,0,0,0.5);
}
.overlay p {
position: relative;
text-align: center;
color: #fff;
}
.tile:hover .overlay{
display: block;
}
Here is an alternate solution. Not sure if its what you wanted.
.tile:hover img, .tile.hover img {transform: scale(1.2);}
Here is the original answer that I adapted: Change background color of child div on hover of parent div?
-----EDIT-----
To stop it scaling and breaking responsiveness you will need to add a container around the image and then set overflow to none.
HTML:
<div class="tile">
<div class="img-container"><img src="https://ichef.bbci.co.uk/news/660/cpsprodpb/16C0E/production/_109089139_928b0174-4b3f-48ff-8366-d118afa1ed56.jpg" class="animate"></div>
<div class="overlay">
<p>Mahatma Gandhi</p>
CSS:
.img-container{
width: 500px;
height: 500px;
overflow: hidden;
}
.tile:hover img, .tile.hover img {
transform: scale(1.2);
}
See the codepen below for an example
https://codepen.io/jamesCyrius/pen/pooqwwv
Here is a code
.zoom {
padding: 50px;
background-color: green;
transition: transform .2s; /* Animation */
width: 15px;
height: 15px;
margin: 0 auto;
}
.zoom:hover {
transform: scale(1.5); /* (150% zoom - Note: if the zoom is too large, it will go outside of the viewport) */
}
<div class="zoom"></div>

HTML and CSS irregular triangle image gallery

I need to create an image gallery, in which the individual images are irregular triangles (emphasis on irregular).
I found limited examples on how to achieve triangle images via html and css, without modifying the images themselves. One example I found in this CodePen https://codepen.io/thebabydino/pen/liDCz was a step in the right direction, but looking at it, I can't find a way to make the images irregular triangles.
The result I am trying to achieve is this:
<div class='pageOption'>
<a href='#' class='option'>
<img src='~/images/team/pic_paggas/A.png'>
</a>
<a href='#' class='option'>
<img src='~/images/team/pic_paggas/D.png'>
</a>
</div>
This is the basic HTML I will be using and the CSS is:
.pageOption {
overflow: hidden;
position: relative;
margin: 0 auto;
width: 40em;
height: 27em;
}
.option, .option img {
width: 100%;
height: 100%;
}
.option {
overflow: hidden;
position: absolute;
transform: skewX(-55.98deg);
}
.option:first-child {
left: -.25em;
transform-origin: 100% 0;
}
.option:last-child {
right: -.25em;
transform-origin: 0 100%;
}
.option img {
opacity: .75;
transition: .5s;
}
.option img:hover {
opacity: 1;
}
.option img, .option:after {
transform: skewX(55.98deg);
transform-origin: inherit;
}
Mind that the HTML and CSS I have may not be the optimal for my problem. I think the shape of the images I am using (rectangular) have something to do with this.
Would be better if the solution is better supported across browsers.
You can do it with skew like below if you cannot use clip-path:
.box {
overflow: hidden;
width: 200px;
height: 200px;
display:inline-block;
}
.triangle {
width: 100%;
height: 100%;
transform: skewX(-20deg) skewY(45deg); /* 27deg instead of 20deg to have a regular triangle */
transform-origin: bottom left;
overflow: hidden;
background-size:0 0;
}
.triangle.bottom {
transform-origin: top right;
}
.triangle:before {
content: "";
display: block;
width: inherit;
height: inherit;
background-image: inherit;
background-size:cover;
background-position:center;
transform: skewY(-45deg) skewX(20deg); /* We invert order AND signs*/
transform-origin: inherit;
}
.triangle:hover {
filter:grayscale(100%);
}
.adjust {
margin-left:-120px;
}
body {
background:#f2f2f2;
}
<div class="box">
<div class="triangle" style="background-image:url(https://picsum.photos/id/155/1000/800)"></div>
</div>
<div class="box adjust">
<div class="triangle bottom" style="background-image:url(https://picsum.photos/id/159/1000/800)"></div>
</div>

Higher z-index does not change stacking order

What I am trying to do is, when the user hover on the image the image should reposition along the x-axis and it should reveal the .content. i have set z-index: 10 to image and z-index: 1 to .content to make .content to be underneath the image. but .content still remains on top of the image. Please help me..
Here is my code:
html
<div class="holder">
<img src="http://placehold.it/350x150" />
<div class="content">
<h3>hello there</h3>
view more
<div/>
</div>
css
.holder {
margin-top: 130px;
position: relative;
}
img {
display: block;
transition: -webkit-transform 0.5s;
transition: -moz-transform 0.5s;
z-index: 10;
}
.content {
background: blue;
height: 100%;
color: white;
z-index: 1;
position: absolute;
top: auto;
bottom: 0;
}
a {
color: white;
background: red;
padding: 10px;
}
.holder:hover img {
-webkit-transform: translateX(90px);
-moz-transform: translateX(90px);
}
Here I corrected issue of my code thanks to Jones G. Drange. As he pointed out in his comment
"z-index can only be modified in elements with a position other than static. Your img has position: static by default"
jsfiddle
img {
position: relative;
}