0

I need to show some <div> boxes after hovering on the <div> with codeyad class (.codeyad).

* {
  direction: rtl;
}

html,
body {
  display: flex;
  justify-content: center;
  align-items: center;
}

.container {
  text-align: center;
  font-size: 19px;
}

div :not(.container, .codeyad) {
  border: 1px solid rgb(59, 59, 59);
  padding: 5px;
  margin: 2px;
  position: relative;
  left: 120%;
  opacity: 0;
}

div.js {
  transition: left 0.3s 0.2s, opacity 0.3s 0.2s ease-in-out;
}

.python {
  transition: all 0.6s 0.2s ease-in-out;
}

.php {
  transition: all 0.9s 0.2s ease-in-out;
}

div.codeyad:hover div.js {
  left: 0%;
  opacity: 1;
}
<div class="container">
  <div class="codeyad">کدیاد</div>
  <div class="js">جاوا اسکریپت</div>
  <div class="python">پایتون</div>
  <div class="php">آموزش php</div>
</div>

The problem is with the following part:

div.codeyad:hover div.js{
  left: 0%;
  opacity: 1;
}

I need to show the <div> with class js by using animations after hovering on the <div> with class codeyad, but it doesn't execute! Can anyone help?

3 Answers 3

2

You need to modify your selector like below. It will select the class .js only when your hover on div.codeyad. learn about + - (Adjacent sibling combinators).

It will work until you keep the HTML structure as is. i.e. .codeyad is immediately followed by .js

div.codeyad:hover + div.js {
  left: 0%;
  opacity: 1;
}

Working code below (updated with comments)

* {
  direction: rtl;
}

html,
body {
  display: flex;
  justify-content: center;
  align-items: center;
}

.container {
  text-align: center;
  font-size: 19px;
}

div :not(.container, .codeyad) {
  border: 1px solid rgb(59, 59, 59);
  padding: 5px;
  margin: 2px;
  position: relative;
  left: 120%;
  opacity: 0;
}

div.js {
  transition: left 0.3s 0.2s, opacity 0.3s 0.2s ease-in-out;
}

.python {
  transition: all 0.6s 0.2s ease-in-out;
}

.php {
  transition: all 0.9s 0.2s ease-in-out;
}

div.codeyad:hover~div.js,
div.codeyad:hover~div.php,
div.codeyad:hover~div.python {
  left: 0%;
  opacity: 1;
}
<div class="container">
  <div class="codeyad">کدیاد</div>
  <div class="js">جاوا اسکریپت</div>
  <div class="python">پایتون</div>
  <div class="php">آموزش php</div>
</div>

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks. What if I want to use div.php, div.python, div.js at a time?
I did not get you, can you please explain?
I mean, assume that I need to show all the child <div>s within the <div> with container class aftaer hovering on the <div> with codeyad class. actually showing all the three <div>s brought in the following: <div class="js">جاوا اسکریپت</div> <div class="python">پایتون</div> <div class="php">آموزش php</div>
Then you can use div.codeyad:hover ~ div { left: 0%; opacity: 1; } . This will target all divs inside
Thanks but I need to choose them one each time, not in totally.
|
1

Yup, you can achieve this by using this: https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_combinator

A note to keep in mind when using this: It'll work "only if it immediately follows the first element, and both are children of the same parent"

So to get the desired result you would do something like this:

div.codeyad:hover+div.js {
  left: 0%;
  opacity: 1;
}

Comments

0

With this code (last snippet provided), you are accessing div with class "js", that is a child of div class "codeyad"

You are telling css something like this =>

<div class="codeyad"> // on hover, do something with child class js
   <div class="js"></div>
</div>

I think the easiest solution is with javascript, where you can access hover functions. Or you can also try with CSS, but it is a little tricky, you have to know CSS selectors. Link provided here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.