By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – How to make the text under my picture change when hovering over the picture (html, css, javascript)
JavaScript

Javascript – How to make the text under my picture change when hovering over the picture (html, css, javascript)

Admin
Last updated: 2023/12/20 at 4:36 PM
Admin
Share
4 Min Read
How to make the text under my picture change when hovering over the picture (html, css, javascript)

Problem:

I am making a site for a school project and for some nice interactions I want to make the text change color when hovering on the picture above the text.

Contents
Problem:Solution:

Example:
I have a picture of a pc. When I hover over the pc the name of the pc (which is under the pc) will change color to, for example, purple. If I hover over the name itself then it will also change color. The picture will only change color when hovering over it (already got that covered). Can someone maybe help me with a for this? Or an inbuild function? I’m kinda new to css and still need to learn a lot about it so anything that works will be greatly appreciated.
I have a container and in the container the div boxes for the picture and for the text are both separated
Here is the code:

HTML

<div class="item">
        <div class="container_pictures">
          <img src="Pc pics/Purple1.png" class="Pc-pics">
        </div>

        <div class="container_pc-info">
          <div class="pc-info">
            test 1 <!-- Only this text needs to be changed. Currently it's color is white -->
          </div> <!-- The new color doesn't matter since I can probably change that -->

          <div class="pc-info">
            test 2
          </div>

          <div class="pc-info">
            test 3
          </div>
          

CSS (The important parts are the item class and the containers inside of it. The padding size and display don’t matter, only the color needs to change of the text. Gave everything just in case)


.item {
  display: flex;                      /* All of the colors don't have to be variables, it's just handier */
  flex-direction: row;
  justify-content: center;
  flex-wrap: wrap;
  align-items: center;
  width: calc(15% + 60px);
  height: 100%;
  margin-top: 4%;
}

.container_pc-info {
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
  align-items: flex-start;

  
  height: 12vw;
  width: 100%;
  margin-top: 8%;
  background: black; /* just for testing, don't think it's important */
  
}

.pc-info {
  display: flex;
  flex-direction: column;
  background-color: var(--color-background-header); 
  font-size: 1vw;
  font-family: "priori-sans", sans-serif;
  color: white;
}

.pc-name {
  height: 0%;
  width: 0%;
  color: var(--color-pcInfo-text);
}

.container_pictures {
  height: 100%;
  width: 100%;
  padding: 1.6em;
  border: 3px solid var(--color-border-items);
  background-color: var(--color-test-background);
  transition: all 0.3s ease;
}

.container_pictures:hover {
  padding: 0.8em;
  background-color: var(--color-border-header-test);
  border-bottom: 3px solid var(--color-border-header-test);
}

.Pc-pics {
  height: 100%;
  width: 100%;
}

I tried searching for ways to make an if statement when the color of the picture changes or when the size of the picture changes, but I couldn’t find anything.
Then I tried making something with :hover but that didn’t work either

.pc-name {
  height: 0%;
  width: 0%;
  color: var(--color-pcInfo-text); /* This is white and it doesn't have to be a variable */
}

.pc-name.container-pictures:hover {
  color: aqua; /* This can also be a variable and the color doesn't matter */
/* The variables are in the :root */
}

Solution:

The element with class container_pictures and the element with class container_pc-info are siblings so you need to use the + selector. Also, to pick only the fist div with pc-info class you can use the :first-child pseudo class.

Putting it all together, you need something like this:

.container_pictures:hover + .container_pc-info .pc-info:first-child {
  color: red; /* you can change this as you please */
}

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article The pokédex that I can't continue [closed] Javascript – The pokédex that I can’t continue [closed]
Next Article Having a hard time using chrome.tabs.sendMessage Javascript – Having a hard time using chrome.tabs.sendMessage
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?