0

I'm trying to make a figure that changes the background color of the contained image when i hover over it with my mouse. I was able to do as much and then trying to go up a level by trying to change the color of the caption and the background using a class but when i hover over the image it isn't working fully.

What i tried to do was to place the class with the background and text color in the figure and thought that it would just make the caption inherit the text color but that didn't happen.

I also tried, alternatively, to give the caption its own hover property, and it does work, but not properly.

You see if I give the figure a background color and text color then only the background changes color as seen below

Before hovering enter image description here After hovering enter image description here

The text is still blue and i have written it to be white.

and when i apply the code directly on the caption this happens

Not hovering on the caption enter image description here When hovering on the caption enter image description here

So as you can see, the caption only lights up when my cursor is on it, but i want it to light up when my cursor is hovering over any part of the image, so what i really want to know is, if there even is a way to do this, any help would be really helpful.

Here is the code i used for this

figcaption {
    color: #3a7cc4;
}

.change {
    box-shadow: 0px 0px 30px lightgray;
    transition: all 0.2s linear;
}

.change:hover{
    background-color: #3a7cc4;
    transition: all 0.2s linear;
    color: white;
}

And then i changed the caption color separately, to make it change color

figcaption:hover {
    color: white;
}

Edit-

Here's a working snippet as well for the code:

.change {
  box-shadow: 0px 0px 30px lightgray;
  transition: all 0.2s ease-in;
}

.change:hover {
  background-color: #3a7cc4;
  transition: all 0.2s ease-in;
  color: white;
}

figcaption:hover {
  color: white;
}
<div class="column change">
<figure>
  <img src="https://i.imgur.com/N7vSN08.jpeg">
  <figcaption>Tree Fort</figcaption>
  </figure>
  </div>
  • Please add your HTML - we don't know which element has the change class. – A Haworth Jan 20 '22 at 05:53
  • I cant do that because i am using wordpress, there is no html. Only premade blocks that you can apply css to. but if you want to know the block, then i am using a column block to act as a background behind the image – knowledge seeker Jan 20 '22 at 06:15
  • Hi, there's lots of HTML! Go into your browser's dev tools inspect facility and see how the HTML is laid out for that figure. You can then copy the relevant bit to show us here. – A Haworth Jan 20 '22 at 06:24
  • here's the code:
    Tree Fort
    – knowledge seeker Jan 20 '22 at 06:42
  • What element has the change class? Incidentally, best to put code into your question (you can edit your question) and even more useful if you make it into a working snippet which shows the problem. – A Haworth Jan 20 '22 at 06:50

1 Answers1

0

Assuming it is the figure that has the change class then it can set the color for the text and it can be inherited by the caption.

In the CSS given in the question however the figcaption overrides any inherited color by setting it itself. Hence it doesn't change when the figure is hovered.

So this snippet removes that and instead places the blueish color setting in the figure. This then gets inherited when there is no hovering. When there is hovering the setting of color as white gets inherited.

<style>
  .change {
    box-shadow: 0px 0px 30px lightgray;
    transition: all 0.2s linear;
    color: #3a7cc4;
  }
  
  .change:hover {
    background-color: #3a7cc4;
    transition: all 0.2s linear;
    color: white;
  }
</style>

<figure class="aligncenter size-full change"><img loading="lazy" width="477" height="587" src="https://cid17970jan2022.kinsta.cloud/wp-content/uploads/2022/01/image.jpg" alt="" class="wp-image-6889" srcset="https://cid17970jan2022.kinsta.cloud/wp-content/uploads/2022/01/image.jpg 477w, https://cid17970jan2022.kinsta.cloud/wp-content/uploads/2022/01/image-244x300.jpg"
    sizes="(max-width: 477px) 100vw, 477px">
  <figcaption>Tree Fort</figcaption>
</figure>

Note: if it is an ancestor element rather than the figure that has the change class the CSS selectors will need changing to .change figure

A Haworth
  • 30,908
  • 4
  • 11
  • 14
  • i have uploaded the snippet, you can check that – knowledge seeker Jan 20 '22 at 07:25
  • The snippet seems to work - is there still a problem? It works because the element with the change class surrounds the whole figure (but isn't affecting anything else) - it's not the same as your actual site. – A Haworth Jan 20 '22 at 07:35
  • i know right, wordpress just loves to mess things up. just like there i put the class in a column but its working here and broken back there, i have no idea what to do at this point. – knowledge seeker Jan 20 '22 at 07:38
  • speaking of the actual site, how did you find the original image? – knowledge seeker Jan 20 '22 at 07:40
  • I still don't know where you put the change class in your original - that is important to know. How are you telling Wordpress how to change the CSS? Sorry I don't understand your question about finding the original image - I just used the links you gave which I assume you found from the original page. – A Haworth Jan 20 '22 at 07:44
  • ohhh, i forgot about the links in the comment, sorry about that, wait 1 minute, i'll send you a screenshot of the page i'm working on – knowledge seeker Jan 20 '22 at 07:50
  • heres the image of the page: https://imgur.com/3ximaeT | and here's the column i applied the class on: https://imgur.com/a/6B9VNsE – knowledge seeker Jan 20 '22 at 07:53
  • Sorry, I still don't know what element has the change class. – A Haworth Jan 20 '22 at 07:58
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/241236/discussion-between-knowledge-seeker-and-a-haworth). – knowledge seeker Jan 20 '22 at 08:11