SLM Productions
SLM Productions

Reputation: 41

Div doesn't appear on hover

the div doesn't appear when I hover on a button, but I've used the specified code in w3schools.com. What's wrong?

Here's my code:

html {
  scroll-behavior: smooth;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
}

.navbar {
  font-family: 'Raleway', sans-serif;
  overflow: hidden;
  background-color: rgba(84, 148, 251, 0.8);
  color: white;
  position: fixed;
  top: 0;
  width: 100%;
}

.navbar a {
  float: right;
  display: block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
  transition: 0.3s;
  font-weight: 600;
}

.navbar a:hover {
  color: rgb(239, 102, 84);
}

#calltoaction {
  font-family: 'Raleway', sans-serif;
  font-weight: 600;
  font-size: 17px;
  margin-top: -5px;
  cursor: pointer;
  background-color: rgb(239, 102, 84);
  color: black;
  border: rgb(239, 102, 84) solid 2px;
  transition: 0.3s;
  text-transform: uppercase;
}

#calltoaction:hover {
  background-color: black;
  color: rgb(239, 102, 84);
  border: black solid 2px
}

.welcome {
  background-color: #333;
  width: 100%;
  padding: 0px;
  color: white;
  height: 150px;
}

#aloha {
  font-family: 'Raleway', sans-serif;
  margin-top: 55px;
}

.myparallax {
  width: 100%;
  background-attachment: fixed;
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
  background-image: url('https://images.squarespace-cdn.com/content/v1/5a26f15fb1ffb6e7c318f710/1516126345399-VIK735PVAIS4BCH4COUE/ke17ZwdGBToddI8pDm48kFmfxoboNKufWj-55Bgmc-J7gQa3H78H3Y0txjaiv_0fDoOvxcdMmMKkDsyUqMSsMWxHk725yiiHCCLfrh8O1z4YTzHvnKhyp6Da-NYroOW3ZGjoBKy3azqku80C789l0iXS6XmVv7bUJ418E8Yoc1hjuviiiZmrL38w1ymUdqq4JaGeFUxjM-HeS7Oc-SSFcg/MalindaKathleenReese.jpg?format=1500w');
  height: 100px;
}

.why-you-join {
  background-color: #333;
  color: white;
  font-family: 'Raleway', sans-serif;
  margin-top: -50px;
}

#what-dyaget {
  background-color: #666;
  color: white;
  font-family: 'Raleway', sans-serif;
  margin-top: -21px;
  height: 600px;
}

.columns {
  float: left;
  width: 33.3%;
  padding: 8px;
}

.price {
  list-style-type: none;
  border: 1px solid #eee;
  margin: 0;
  padding: 0;
  -webkit-transition: 0.3s;
  transition: 0.3s;
}

.price:hover {
  box-shadow: 0 8px 12px 0 rgba(0, 0, 0, 0.2)
}

.price .header {
  background-color: #000;
  color: white;
  font-size: 25px;
  text-transform: uppercase;
}

.price li {
  border-bottom: 1px solid #eee;
  padding: 20px;
  text-align: center;
}

.price .grey {
  background-color: #eee;
  font-size: 20px;
  color: black;
}

.button {
  border: rgb(239, 102, 84) solid 2px;
  background-color: rgb(239, 102, 84);
  color: white;
  padding: 10px 25px;
  text-align: center;
  text-decoration: none;
  font-size: 18px;
  transition: 0.3s;
}

.button:hover {
  background-color: transparent;
  color: rgb(239, 102, 84);
}

.row {
  height: 520px;
}

.myparallax-2 {
  width: 100%;
  background-attachment: fixed;
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
  background-image: url('https://vignette.wikia.nocookie.net/malinda/images/f/f3/TranslatorFails.jpg/revision/latest/scale-to-width-down/350?cb=20190920184709');
  height: 100px;
  margin-top: 20px !important;
}

#faq {
  background-color: #333;
  color: white;
  font-family: 'Raleway', sans-serif;
  margin-top: -50px;
}

.question-column {
  float: left;
  width: 33.33%;
  padding: 10px;
}

.question-row:after {
  content: "";
  display: table;
  clear: both;
}

#q {
  text-transform: uppercase;
  font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif
}

#a {
  text-align: justify;
}

#trialrequest,
#trialrequest-2 {
  border: none
}

#trialrequest:hover {
  border: none;
  box-shadow: 5px 7px #5494fb;
  background-color: #666;
  color: white;
}

#trialrequest-3 {
  display: none;
}

@media only screen and (max-width: 992px) {
  #trialrequest-3 {
    display: none;
  }
  #what-dyaget {
    height: 2100px;
  }
  .row {
    height: 1440px;
  }
  .columns {
    width: 100%;
  }
  .question-column {
    width: 100%;
  }
  .myparallax-2 {
    margin-top: 80px !important;
  }
}

@media only screen and (max-width: 375px) {
  .myparallax-2 {
    margin-top: 80px !important;
  }
  #trialrequest-3 {
    display: none;
  }
}

@media only screen and (max-width: 374px) {
  #trialrequest-2 {
    border-radius: 0;
    display: none;
  }
  #trialrequest-3 {
    display: block;
    border-radius: 25px;
    font-weight: 700;
  }
  #trialrequest-3:hover {
    background-color: rgba(249, 112, 110);
    color: white;
    border: solid 2px rgba(249, 112, 110)
  }
}

#moreinfo {
  color: white;
  transition: 0.3s;
}

#moreinfo:hover {
  color: rgba(255, 255, 255, 0.8) !important
}

q {
  font-size: 20px;
}

#trialrequest-2 {
  border-radius: 25px;
}

#trialrequest-2:hover {
  background-color: rgba(249, 112, 110);
  color: white;
}

.dropdown {
  display: none;
  z-index: 5;
  width: 130px;
  position: fixed;
  top: 50px;
  right: 19.5px;
  background: black;
  color: white;
  padding: 0;
}

.dropdown ul {
  list-style-type: none;
  margin-left: -40px;
}

.dropdown ul a {
  font-family: 'Raleway', sans-serif;
}

.dropdown ul li {
  padding-top: 5px !important;
  padding-bottom: 5px !important;
  padding-left: 10px;
  cursor: pointer;
}

.dropdown ul li:hover {
  background-color: rgb(25, 25, 25)
}

.myDIV:hover .hide {
  display: initial;
  color: red;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href="https://fonts.googleapis.com/css2?family=Raleway:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet">
  <title>MALINDA community</title>
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

<body>
  <div class="navbar">
    <a><button id="calltoaction">Join / Log In</button></a>
    <a href="#faq">FAQ</a>
    <a href="#what-dyaget">Pricing</a>
  </div>
  <div class="dropdown">
    <ul>
      <li><a>Login</a></li>
      <li><a>Join</a></li>
    </ul>
  </div>
</body>

</html>

As I've said before, this dropdown is made as said in the w3schools.com site.

Upvotes: 0

Views: 88

Answers (1)

WOUNDEDStevenJones
WOUNDEDStevenJones

Reputation: 5315

If you adjust your HTML to place the hidden dropdown directly after the nav item you want it to interact with, then you can add the CSS rule:

/* show the dropdown when hovering on the nav item */
.navbar a:hover + .dropdown, 
/* show the dropdown when hovering on the hidden items */ 
.dropdown:hover {
  display: initial;
}

to display the hidden dropdown when you hover over the nav item.

Note that this currently doesn't have keyboard/accessibility support, this is just a slightly modified version of the code you're currently working with that does work correctly with a mouse.

html {
  scroll-behavior: smooth;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
}

.navbar {
  font-family: 'Raleway', sans-serif;
  background-color: rgba(84, 148, 251, 0.8);
  color: white;
  position: fixed;
  top: 0;
  width: 100%;
}

.navbar a {
  float: right;
  display: block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
  transition: 0.3s;
  font-weight: 600;
}

.navbar a:hover {
  color: rgb(239, 102, 84);
}

#calltoaction {
  font-family: 'Raleway', sans-serif;
  font-weight: 600;
  font-size: 17px;
  margin-top: -5px;
  cursor: pointer;
  background-color: rgb(239, 102, 84);
  color: black;
  border: rgb(239, 102, 84) solid 2px;
  transition: 0.3s;
  text-transform: uppercase;
}

#calltoaction:hover {
  background-color: black;
  color: rgb(239, 102, 84);
  border: black solid 2px
}

.welcome {
  background-color: #333;
  width: 100%;
  padding: 0px;
  color: white;
  height: 150px;
}

#aloha {
  font-family: 'Raleway', sans-serif;
  margin-top: 55px;
}

.myparallax {
  width: 100%;
  background-attachment: fixed;
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
  background-image: url('https://images.squarespace-cdn.com/content/v1/5a26f15fb1ffb6e7c318f710/1516126345399-VIK735PVAIS4BCH4COUE/ke17ZwdGBToddI8pDm48kFmfxoboNKufWj-55Bgmc-J7gQa3H78H3Y0txjaiv_0fDoOvxcdMmMKkDsyUqMSsMWxHk725yiiHCCLfrh8O1z4YTzHvnKhyp6Da-NYroOW3ZGjoBKy3azqku80C789l0iXS6XmVv7bUJ418E8Yoc1hjuviiiZmrL38w1ymUdqq4JaGeFUxjM-HeS7Oc-SSFcg/MalindaKathleenReese.jpg?format=1500w');
  height: 100px;
}

.why-you-join {
  background-color: #333;
  color: white;
  font-family: 'Raleway', sans-serif;
  margin-top: -50px;
}

#what-dyaget {
  background-color: #666;
  color: white;
  font-family: 'Raleway', sans-serif;
  margin-top: -21px;
  height: 600px;
}

.columns {
  float: left;
  width: 33.3%;
  padding: 8px;
}

.price {
  list-style-type: none;
  border: 1px solid #eee;
  margin: 0;
  padding: 0;
  -webkit-transition: 0.3s;
  transition: 0.3s;
}

.price:hover {
  box-shadow: 0 8px 12px 0 rgba(0, 0, 0, 0.2)
}

.price .header {
  background-color: #000;
  color: white;
  font-size: 25px;
  text-transform: uppercase;
}

.price li {
  border-bottom: 1px solid #eee;
  padding: 20px;
  text-align: center;
}

.price .grey {
  background-color: #eee;
  font-size: 20px;
  color: black;
}

.button {
  border: rgb(239, 102, 84) solid 2px;
  background-color: rgb(239, 102, 84);
  color: white;
  padding: 10px 25px;
  text-align: center;
  text-decoration: none;
  font-size: 18px;
  transition: 0.3s;
}

.button:hover {
  background-color: transparent;
  color: rgb(239, 102, 84);
}

.row {
  height: 520px;
}

.myparallax-2 {
  width: 100%;
  background-attachment: fixed;
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
  background-image: url('https://vignette.wikia.nocookie.net/malinda/images/f/f3/TranslatorFails.jpg/revision/latest/scale-to-width-down/350?cb=20190920184709');
  height: 100px;
  margin-top: 20px !important;
}

#faq {
  background-color: #333;
  color: white;
  font-family: 'Raleway', sans-serif;
  margin-top: -50px;
}

.question-column {
  float: left;
  width: 33.33%;
  padding: 10px;
}

.question-row:after {
  content: "";
  display: table;
  clear: both;
}

#q {
  text-transform: uppercase;
  font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif
}

#a {
  text-align: justify;
}

#trialrequest,
#trialrequest-2 {
  border: none
}

#trialrequest:hover {
  border: none;
  box-shadow: 5px 7px #5494fb;
  background-color: #666;
  color: white;
}

#trialrequest-3 {
  display: none;
}

@media only screen and (max-width: 992px) {
  #trialrequest-3 {
    display: none;
  }
  #what-dyaget {
    height: 2100px;
  }
  .row {
    height: 1440px;
  }
  .columns {
    width: 100%;
  }
  .question-column {
    width: 100%;
  }
  .myparallax-2 {
    margin-top: 80px !important;
  }
}

@media only screen and (max-width: 375px) {
  .myparallax-2 {
    margin-top: 80px !important;
  }
  #trialrequest-3 {
    display: none;
  }
}

@media only screen and (max-width: 374px) {
  #trialrequest-2 {
    border-radius: 0;
    display: none;
  }
  #trialrequest-3 {
    display: block;
    border-radius: 25px;
    font-weight: 700;
  }
  #trialrequest-3:hover {
    background-color: rgba(249, 112, 110);
    color: white;
    border: solid 2px rgba(249, 112, 110)
  }
}

#moreinfo {
  color: white;
  transition: 0.3s;
}

#moreinfo:hover {
  color: rgba(255, 255, 255, 0.8) !important
}

q {
  font-size: 20px;
}

#trialrequest-2 {
  border-radius: 25px;
}

#trialrequest-2:hover {
  background-color: rgba(249, 112, 110);
  color: white;
}

.dropdown {
  display: none;
  z-index: 5;
  width: 130px;
  position: fixed;
  top: 50px;
  right: 19.5px;
  background: black;
  color: white;
  padding: 0;
}

.dropdown ul {
  list-style-type: none;
  margin-left: -40px;
}

.dropdown ul a {
  font-family: 'Raleway', sans-serif;
}

.dropdown ul li {
  padding-top: 5px !important;
  padding-bottom: 5px !important;
  padding-left: 10px;
  cursor: pointer;
}

.dropdown ul li:hover {
  background-color: rgb(25, 25, 25)
}

.myDIV:hover .hide {
  display: initial;
  color: red;
}

.navbar a:hover+.dropdown,
.dropdown:hover {
  display: initial;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href="https://fonts.googleapis.com/css2?family=Raleway:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet">
  <title>MALINDA community</title>
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

<body>
  <div class="navbar">
    <a><button id="calltoaction">Join / Log In</button></a>
    <div class="dropdown">
      <ul>
        <li><a>Login</a></li>
        <li><a>Join</a></li>
      </ul>
    </div>
    <a href="#faq">FAQ</a>
    <a href="#what-dyaget">Pricing</a>
  </div>
</body>

</html>

Upvotes: 2

Related Questions