Adding hover state to sibling - html

I have an image that has a hover state that changes two things: it darkens the image and it displays absolute positioned text over the image. The issue I'm having is when I hover over the text the image is no longer showing darkened. I've tried adding a hover state to the text as well. The image and hover text are siblings so I tried using the ~ following-sibling combinator like this: .hovertext:hover ~ .hoverimg but obviously I'm not using it correctly.
Code
.wrapper {
position: relative;
text-align: center;
}
.hovertext {
display: none;
color: white;
font-size: 30px;
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
}
.wrapper:hover .hovertext {
display: block;
}
.hoverimg:hover,
.hovertext:hover~.hoverimg {
filter: brightness(50%);
}
<div class="wrapper">
<img class="hoverimg" src="https://loremflickr.com/320/240">
<div class="hovertext">HOVER TEXT</div>
</div>
Is there a way to show both hover states with CSS only?

Use pointer-events:none to the text if there is no hover or click event on text
Stack Snippet
.wrapper {
position: relative;
text-align: center;
}
.hovertext {
display: none;
color: white;
font-size: 30px;
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
pointer-events: none;
}
.wrapper:hover .hovertext {
display: block;
}
.hoverimg:hover,
.hovertext:hover~.hoverimg {
filter: brightness(50%);
}
<div class="wrapper">
<img class="hoverimg" src="https://loremflickr.com/320/240">
<div class="hovertext">HOVER TEXT</div>
</div>
And if you are planning to add event on the text then you can change img and text properties on .wrapper class hover
.wrapper {
position: relative;
text-align: center;
}
.hovertext {
display: none;
color: white;
font-size: 30px;
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
}
.wrapper:hover .hovertext {
display: block;
}
.wrapper:hover .hoverimg {
filter: brightness(50%);
}
<div class="wrapper">
<img class="hoverimg" src="https://loremflickr.com/320/240">
<div class="hovertext">HOVER TEXT</div>
</div>

There is no way of selecting the previous element i.e. changing the styling of previous element using CSS as you are trying using sibling selector .hovertext:hover ~ .hoverimg, to make use of sibling selector here you need to make some changes in your html markup as below,
.wrapper {
position: relative;
text-align: center;
}
.hovertext {
display: none;
color: white;
font-size: 30px;
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
z-index: 9;
}
.wrapper:hover > .hovertext {
display: block;
}
.hoverimg:hover,
.hovertext:hover ~ .hoverimg {
filter: brightness(50%);
}
<div class="wrapper">
<div class="hovertext">HOVER TEXT</div>
<img class="hoverimg" src="https://loremflickr.com/320/240">
</div>
In the above codes I have changed your HTML markup, thus now .hoverimg is sibling of targeted element i.e. .hovertext, so now every-time when you hover .hovertext or .hoverimg it adds assigned styling.

Related

I can't get the hover effect right

I can't get the hover effect right. I have a block with a picture and text in it. When hovering over the picture - should change the fill, filter: brightness. It does, but if I hover over the text which is in the middle of the picture, the text overlaps the picture and the hover effect doesn't work. If I put filter: brightness on the whole block, not only on the image, then the text gets filled too and becomes black. And my task is to change only the color of the picture, but not the text. What am I doing wrong?
.blocks
h5
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display: inline-block
text-align: center;
color: white;
font-size: 18px;
img
filter: brightness(0.28)
&:hover
filter: brightness(0.7)
please make sure to put your relevant code (HTML and CSS) into an Stack-Snippet, so everyone here can easy reproduce the problem!
If you want to change the IMG-style when hovering the parent-element, use .parentElementClass:hover img{}
.blocks {
position: relative;
display: inline-block;
width: 150px;
}
.blocks h5 {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display: inline-block;
text-align: center;
color: white;
font-size: 18px;
z-index: 2;
}
.blocks img {
filter: brightness(0.28);
z-index: 1;
}
.blocks:hover img {
filter: brightness(0.7);
}
<div class="blocks">
<h5>headline 5</h5>
<img src="https://via.placeholder.com/150/000000/" />
</div>

Make a paragraph appear inside a picture on picture hover

basically all i want to do is: Have a picture and when someone hovers over it i want some text to appear in its position(in the middle to be exact). What I have done so far is make the picture disappear on hover but i cannot get the text to be appeared..
Html:
<div class="container">
<img id="atp "class="atp" src="atp.jpg">
<div class="center">Some text</div>
</div>
Css:
atp{
display:block;
margin-left:auto;
margin-right:auto;
width:27%;
height:50%;}
container{
position: relative;
text-align: center;
}
.center{
position: absolute;
top:50%;
left:50%;
opacity: 0;
}
So basically, what i seek to be done is .atp:hover{opacity:0;} and what I also want is on atp's hover the .center{opacity:1;] So is there a way to put the opacity of center's to 1 when I am in the atp:hover{} code block?
Hope everything looks fine, thanks in advance!
Here is the code. Hope it will help you. if any changes please let me know.
/********* Simple or original overlay *******/
/* Main container */
.overlay-image {
position: relative;
width: 100%;
}
/* Original image */
.overlay-image .image {
display: block;
width: 100%;
height: auto;
}
/* Original text overlay */
.overlay-image .text {
color: #fff;
font-size: 30px;
line-height: 1.5em;
text-shadow: 2px 2px 2px #000;
text-align: center;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 100%;
}
/********* Overlay on hover *******/
/* New overlay on hover */
.overlay-image .hover {
position: absolute;
top: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: .5s ease;
}
/* New overlay appearance on hover */
.overlay-image:hover .hover {
opacity: 1;
}
/********* Background and text only overlay on hover *******/
.overlay-image .normal {
transition: .5s ease;
}
.overlay-image:hover .normal {
opacity: 0;
}
.overlay-image .hover {
background-color: rgba(0,0,0,0.5);
}
<div class="overlay-image">
<a href="LINK_URL">
<div class="normal">
<div class="text">Image + text ORIGINAL</div>
</div>
<div class="hover">
<img class="image" src="https://dummyimage.com/200x150/00ccff/fff.png" alt="Alt text hover" />
</div>
</a>
</div>
#atp {
position: relative;
display: block;
margin-left: auto;
margin-right: auto;
width: 27%;
height: 50%;
z-index: 50;
}
.container {
position: relative;
text-align: center;
}
.center {
position: absolute;
top: 50%;
left: 50%;
z-index: 30;
}
#atp:hover {
opacity: 0;
}
try this, using z-index. It worked with me :)

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>

Prevent new line after every word in span

We are using some code to make a tooltip appear while hovering over text or an icon. Using a transform we are then able to center the tooltip, no matter what size it is, so we can have the size scale with the content of the tooltip: http://jsfiddle.net/z8wxdjzu/4/
.position-me {
margin: 10%;
}
.abbr {
position: relative;
}
.tooltip {
background: orange;
display: none;
text-align: center;
max-width: 100px;
padding: 3px;
position: absolute;
top: 20px;
left: 50%;
right: auto;
-webkit-transform: translate(-50%, 0);
-ms-transform: translate(-50%, 0);
transform: translate(-50%, 0%);
}
.abbr:hover .tooltip {
display: inline-block;
}
<div class="position-me">
<span class="abbr">
Text
<span class="tooltip">Lorem ipsum dolor sit amet.</span>
</span>
</div>
The problem is that even when providing a larger maximum width, a new line is added in the tooltip after every word. Instead we want the tooltip to fill the entire width (provided with max-width) before moving to a second line.
We don't want to set a fixed width, because some tooltips are rather short and we don't want a lot of empty space in the tooltip. How can we prevent the line breaks from happening?
EDIT: The max-width is actually important, since some of the tooltips are long enough to span several lines, so just removing it and using a whitespace: nowrap is going to be insufficient, unless we do manual line breaks, which I'd rather prevent.
The problem is that your tooltip span wants to have it's width be less or equal to the parent's width, in this case the span.abbr.
So to fix it we can add another div around the tooltip that is a bit larger but outside the flow of the other text, and it works.
Edit: I edited the snippet to center the tooltip. I also left in the red background for the surrounding div to show the trick that I apply here.
Example snippet:
.position-me {
margin: 10%;
}
.abbr {
position: relative;
}
.abbr div {
position: absolute;
top: 0;
left: 50%;
background-color: red;
width: 200px;
height: 2px;
}
.tooltip {
background: orange;
display: none;
text-align: center;
max-width: 100px;
padding: 3px;
position: absolute;
top: 20px;
-webkit-transform: translate(-50%, 0);
-ms-transform: translate(-50%, 0);
transform: translate(-50%, 0);
}
.abbr:hover .tooltip {
display: inline-block;
}
<div class="position-me">
Bla bla bla
<span class="abbr">
HOVER ME
<div><span class="tooltip">Lorem ipsum dolor sit amet.</span>
</div></span>
some more text here.
</div>
Here the fixed fiddle: Fiddle
You need to remove position:absolute and then add width:auto on .tooltip class and add display:inline-block; to .abbr:hover .tooltip
You can't prevent breaking (which you can do) without losing the max-width.
Just adding white-space:nowrap will stop the text breaking but will cause overflow of the span. So remove the max-width and all is good.
.position-me {
margin: 10%;
}
.abbr {
position: relative;
}
.tooltip {
background: orange;
/*display: none; */
/* disabled for demo */
text-align: center;
white-space: nowrap;
padding: 3px;
position: absolute;
top: 20px;
left: 50%;
-webkit-transform: translate(-50%, 0);
-ms-transform: translate(-50%, 0);
transform: translate(-50%, 0);
}
.abbr:hover .tooltip {
display: block;
}
<div class="position-me"> <span class="abbr">
Text
<span class="tooltip">Lorem ipsum dolor sit amet.</span>
</span>
</div>
i changed your css file this way and it worked !
.position-me {
margin: 10%;
}
.abbr {
//position: relative;//notice here
}
.tooltip {
background: orange;
display: none;
text-align: center;
//max-width: 100px;
padding: 3px;
position: absolute;
top: 20px;
left: 50%;
-webkit-transform: translate(-50%, 0);
-ms-transform: translate(-50%, 0);
transform: translate(-100%, 200%);//notice here !!!
}
.abbr:hover .tooltip {
display: block;
}
.position-me {
margin: 10%;
}
.abbr {
position: relative;
}
.tooltip {
max-width: 200px;
padding: 3px 8px;
color: #ffffff;
text-align: center;
text-decoration: none;
background-color: #000000;
border-radius: 4px;
display:none;
}
.abbr:hover .tooltip {
display:block;
}
<div class="position-me"> <span class="abbr">
Text
<span class="tooltip">Lorem ipsum dolor sit amet. rerterte rtertertert</span>
</span>
</div>
This should be the solution: http://jsfiddle.net/z8wxdjzu/7/