2

I am trying to create a drop-down menu in HTML/CSS, but I am not getting anywhere. I am trying to display the content div ONLY when the Channel button is being hovered on. I tried looking at w3schools, but it didn't help.

.NavWrapper {
	background: grey;
	max-width: 5000px;
	min-width: 500px;
	height: 50px;
	align: center;
	padding: 10px;
}
.Home {
	border: 0;
	color: black;
	background: white;
	height: 50px;
	width: 80px;
}
.Channel {
	border: 0;
	color: black;
	background: white;
	height: 50px;
	width: 80px;
	transition-duration: 0.3s;
	position: relative;
}
.Content-Wrapper {
	background: grey;
	max-width: 5000px;
	min-width: 500px;
	height: 50px;
	align: center;
	padding: 10px;
	display: none;
}
.Channel:hover, .Content-Wrapper {
	background: silver;
	color: black;
	display: inline-block;
}
<div class = "NavWrapper">
	<button class = "Home">
		Home
	</button>
	<button class = "Channel">
		Channel
	</button>
	<div class = "Content-Wrapper">
	<button class = "Content-Button">
		Hello World!
	</button>
</div>
</div>

1
  • @AgeDeO no, I just need help fixing a problem
    – EgMusic
    Commented Sep 28, 2017 at 15:39

3 Answers 3

2

Try changing .Channel:hover, .Content-Wrapper to .Channel:hover + .Content-Wrapper.

Personally I would look at your markup and use ul's and li's to create what you're trying to do, but that at least fixes your display issue.

0
2

I'm using mouseover and mouseleave events to show and hide the target. Here is the working example:

$('.Channel').on('mouseover', function() {
  $('.Content-Wrapper').show()
})

$('.Channel').on('mouseleave', function() {
  $('.Content-Wrapper').hide()
})
.NavWrapper {
	background: grey;
	max-width: 5000px;
	min-width: 500px;
	height: 50px;
	align: center;
	padding: 10px;
}
.Home {
	border: 0;
	color: black;
	background: white;
	height: 50px;
	width: 80px;
}
.Channel {
	border: 0;
	color: black;
	background: white;
	height: 50px;
	width: 80px;
	transition-duration: 0.3s;
	position: relative;
}
.Content-Wrapper {
	background: grey;
	max-width: 5000px;
	min-width: 500px;
	height: 50px;
	align: center;
	padding: 10px;
	display: none;
  background: silver;
	color: black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class = "NavWrapper">
	<button class = "Home">
		Home
	</button>
	<button class = "Channel">
		Channel
	</button>
	<div class = "Content-Wrapper">
	<button class = "Content-Button">
		Hello World!
	</button>
</div>
</div>

2
  • That is good, but I don't know how to use Javascript, and the other answers already beat you to it in CSS. I'll still upvote you though!
    – EgMusic
    Commented Sep 28, 2017 at 15:48
  • @ChaseBarnes, it's a matter of taste, what to use, JavaScript or pure CSS. In your case the CSS solution is more preferable, because you simply want to show/hide. You have to use JavaScript when you need more control and you goal is more complex. Thanks for the upvote!
    – P.S.
    Commented Sep 28, 2017 at 15:52
1

.NavWrapper {
	background: grey;
	max-width: 5000px;
	min-width: 500px;
	height: 50px;
	align: center;
	padding: 10px;
}
.Home {
	border: 0;
	color: black;
	background: white;
	height: 50px;
	width: 80px;
}
.Channel {
	border: 0;
	color: black;
	background: white;
	height: 50px;
	width: 80px;
	transition-duration: 0.3s;
	position: relative;
}
.Content-Wrapper {
	background: grey;
	max-width: 5000px;
	min-width: 500px;
	height: 50px;
	align: center;
	padding: 10px;
	display: none;
    margin-top: 10px;
}
.Channel:hover + .Content-Wrapper {
	background: silver;
	color: black;
	display: inline-block;
}
<div class = "NavWrapper">
	<button class = "Home">
		Home
	</button>
	<button class = "Channel">
		Channel
	</button>
	<div class = "Content-Wrapper">
	<button class = "Content-Button">
		Hello World!
	</button>
</div>
</div>

7
  • Okay, now how can I create it neat, to where it displays under the NavWrapper?
    – EgMusic
    Commented Sep 28, 2017 at 15:51
  • Thanks Young Kyun Jin
    – EgMusic
    Commented Sep 28, 2017 at 15:54
  • @ChaseBarnes Check my answer. Do you want to make like that?
    – kyun
    Commented Sep 28, 2017 at 15:55
  • It will be useful later on, so if I need to, yes!
    – EgMusic
    Commented Sep 28, 2017 at 15:55
  • @ChaseBarnes Clear?
    – kyun
    Commented Sep 28, 2017 at 15:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.