I write navbar component in my Angular 6 app.
<div class="navbar">
<ul>
<li>tekst</li>
<li>tekst</li>
<div class="dropdown">
<button class="dropbtn">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
</ul>
</div>
and .css file:
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #e8d625;
position: fixed;
top: 0;
width: 100%
}
li {
float: left;
}
li a {
display: block;
color: #090909;
text-align: center;
font-weight: bold;
padding: 14px 16px;
text-decoration: none;
}
/* Change the link color to #111 (black) on hover */
li a:hover {
background-color: #f7e525;
}
.active {
background-color: #f7e525;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: #77ffb7;
padding: 14px 16px;
background-color: inherit;
font-family: inherit; /* Important for vertical align on mobile phones */
margin: 0; /* Important for vertical align on mobile phones */
}
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: #f7e525;
}
/* Dropdown content (hidden by default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #80f987;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
/* Add a grey background color to dropdown links on hover */
.dropdown-content a:hover {
background-color: #80f987;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
but it's not working.
When I use the mouse, dropdown does not display anything.. It's difficult to say but I use https://www.w3schools.com/css/css_dropdowns.asp .
Do not I have any libraries?
And other question. Will I be able to use in dropdown later? Beacuse I use RoutingModule.
Very thanks for all answers.
EDIT:
add left navbar
<ul>
<li>Calendar</li>
<li>info1</li>
<li>info2</li>
<li>info3</li>
<li>info4</li>
</ul>
ul {
list-style-type: none;
margin-top: 45px;
padding: 0;
width: 200px;
background-color: #f1f1f1;
height: 100%;
position: fixed;
}
li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
}
/* Change the link color on hover */
li a:hover {
background-color: #555;
color: white;
}
You need to remove the overflow: hidden rule from ul, otherwise the dropdown won't be visible.
ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: #e8d625;
position: fixed;
top: 0;
width: 100%;
z-index: 5;
}
li {
float: left;
}
li a {
display: block;
color: #090909;
text-align: center;
font-weight: bold;
padding: 14px 16px;
text-decoration: none;
}
/* Change the link color to #111 (black) on hover */
li a:hover {
background-color: #f7e525;
}
.active {
background-color: #f7e525;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: #77ffb7;
padding: 14px 16px;
background-color: inherit;
font-family: inherit; /* Important for vertical align on mobile phones */
margin: 0; /* Important for vertical align on mobile phones */
}
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: #f7e525;
}
/* Dropdown content (hidden by default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #80f987;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
/* Links inside the dropdown */
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
position: relative;
}
/* Add a grey background color to dropdown links on hover */
.dropdown-content a:hover {
background-color: #80f987;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
ul.side {
list-style-type: none;
margin-top: 45px;
padding: 0;
width: 200px;
background-color: #f1f1f1;
height: 100%;
position: fixed;
z-index: 1;
}
.side li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
}
/* Change the link color on hover */
.side li a:hover {
background-color: #555;
color: white;
}
<div class="navbar">
<ul>
<li>tekst</li>
<li>tekst</li>
<div class="dropdown">
<button class="dropbtn">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
</ul>
</div>
<ul class="side">
<li>Calendar</li>
<li>info1</li>
<li>info2</li>
<li>info3</li>
<li>info4</li>
</ul>
Related
I would like to resize my dropdown menu to match the link width. Here is a picture of what I currently have:
Here are the relevant parts of my HTML page:
<body>
<div class="navbar">
<nav>
<a href=index.html>About</a>
<a href=projects.html>Projects</a>
<a href=publications.html>Publications</a>
<div class="dropdown">
<button class="dropbtn">Writing
</button>
<div class="dropdown-content">
Why Write?
Dollops
Longforms
Technical/Science
Quotes
Words
Notes
</div>
</div>
</nav>
</div>
Here are the most relevant parts of my CSS stylesheet:
/* Navbar container */
*{
box-sizing: border-box;
}
.navbar {
overflow: hidden;
background-color: #272424;
font-family: Arial;
}
/* Links inside the navbar */
.navbar a {
float: left;
font: Arial;
font-size: 20px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
width: 25%;
}
/* The dropdown container */
.dropdown {
float: left;
overflow: hidden;
width: 25%;
}
/* Dropdown button */
.dropdown .dropbtn {
font-size: 20px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit; /* Important for vertical align on mobile phones */
margin: 0; /* Important for vertical align on mobile phones */
width: 100%;
}
/* Add a red background color to navbar links on hover */
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: #81A3A7;
}
/* Dropdown content (hidden by default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
/* Links inside the dropdown */
.navbar .dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
width:auto;
}
/* Add a grey background color to dropdown links on hover */
.dropdown-content a:hover {
background-color: #ddd;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
I tried adding width: 100% to .dropdown-content but the width was too large and extended beyond the writing link to the right.
EDIT: As an extra feature, I would like my links to be appear as "boxes" that are connected without any spaces between them instead of having them appear as on one continuous band of black. How can I do that?
I have make some modification related to the css.
/* Navbar container */
*{
box-sizing: border-box;
}
.navbar {
background-color: #272424;
font-family: Arial;
height:50px
}
/* Links inside the navbar */
.navbar a {
float: left;
font: Arial;
font-size: 20px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
width: 25%;
}
/* The dropdown container */
.dropdown {
float: left;
width: 25%;
position: relative;
}
/* Dropdown button */
.dropdown .dropbtn {
font-size: 20px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit; /* Important for vertical align on mobile phones */
margin: 0; /* Important for vertical align on mobile phones */
width: 100%;
}
/* Add a red background color to navbar links on hover */
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: #81A3A7;
}
/* Dropdown content (hidden by default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
width: 100%;
}
/* Links inside the dropdown */
.navbar .dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
width:auto;
}
/* Add a grey background color to dropdown links on hover */
.dropdown-content a:hover {
background-color: #ddd;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
<div class="navbar">
<nav>
<a href=index.html>About</a>
<a href=projects.html>Projects</a>
<a href=publications.html>Publications</a>
<div class="dropdown">
<button class="dropbtn">Writing
</button>
<div class="dropdown-content">
Why Write?
Dollops
Longforms
Technical/Science
Quotes
Words
Notes
</div>
</div>
</nav>
</div>
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
width: -webkit-fill-available;
}
Setting width to -webkit-fill-available will do the change.
If you want to appear as seperate boxes. You need to use flex feature.
Check out my code :I solved that .dropdown-content issue and Also added extra feature as per u r requirement i.e (spaces between them instead of having them appear as on one continuous band of black)
/* Navbar container */
*{
box-sizing: border-box;
}
.navbar {
overflow: hidden;
background-color: transparent;
font-family: Arial;
}
/* Links inside the navbar */
.navbar a {
float: left;
font: Arial;
font-size: 20px;
border-radius:4px;
margin:6.2px;
background-color: black;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
width: 23.5%;
}
/* The dropdown container */
.dropdown {
float: left;
margin:6px;
border-radius:4px;
overflow: hidden;
background-color: black;
width: 24%;
}
/* Dropdown button */
.dropdown .dropbtn {
font-size: 20px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit; /* Important for vertical align on mobile phones */
margin: 0; /* Important for vertical align on mobile phones */
width: 100%;
}
/* Add a red background color to navbar links on hover */
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: #81A3A7;
}
/* Dropdown content (hidden by default) */
.dropdown-content {
display: none;
width:23.5%;
position: absolute;
background-color: #f9f9f9;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
/* Links inside the dropdown */
.navbar .dropdown-content a {
float: none;
color: black;
background-color:white;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
width:auto;
}
/* Add a grey background color to dropdown links on hover */
.dropdown-content a:hover {
background-color: #ddd;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
#media only screen and (max-width: 900px){
.navbar a {
width:22%;
}
.dropdown{
width:22%;
}
.dropdown-content{
width:22%;
}
}
<div class="navbar">
<nav>
<a href=index.html>About</a>
<a href=projects.html>Projects</a>
<a href=publications.html>Publications</a>
<div class="dropdown">
<button class="dropbtn">Writing
</button>
<div class="dropdown-content">
Why Write?
Dollops
Longforms
Technical/ Science
Quotes
Words
Notes
</div>
</div>
</nav>
</div>
My drop down menu on my navigation bar is called "Works". I can't seem to figure out why "Works" has a grey box surrounding the word while the other words on the navigation bar do not.
My CSS for the nav bar and drop down menu:
nav {
list-style-type: none;
margin: 0;
padding: 0px;
overflow: hidden;
margin-bottom: 30px;
line-height: 1.5em;
text-decoration: none;
}
nav a, .dropbtn {
display: inline;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 25px;
font-family:"mrs-eaves";
}
.dropdown {
overflow: hidden;
}
.dropdown {
cursor: pointer;
font-size: 25px;
border: none;
outline: none;
color: black;
padding: 14px 16px;
background-color: inherit;
font-family: "mrs-eaves";
margin: 0;
text-decoration: none;
}
nav a:hover, .dropdown:hover .dropbtn, .dropbtn:focus
{
background-color: rgb(247, 219, 255);
}
.dropdown-content {
display: none;
position: relative;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: center;
}
.show {
display: block;
Here is my html:
<nav>
<center>
Home
<button class="dropbtn" onclick="myFunction()">Works
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content" id="myDropdown">
Drawing
Animation
Design
</div>
About
Contact
Links
</center>
</nav>
If it must be a button, add the following CSS
.dropbtn {
background: none;
border: none;
}
Then, in your CSS where you set the hover color, change it to this
nav a:hover, .dropdown:hover, .dropbtn:focus, .dropbtn:hover {
background-color: rgb(247, 219, 255);
}
Edit:
Added cursor change so its a pointer for consistency when the button is hovered.
JSFiddle
It is standard button style. So, if you want it to be transparent, you should add certain styling for your button class (.dropbtn).
It is,
.dropbtn {
background: none;
border: none;
}
How do I get the words in my nav bar to be centered? I want the Home, News and Dropdown to be centered.
/* Navbar container */
.navbar {
overflow: hidden;
background-color: #333;
font-family: Arial;
}
/* Links inside the navbar */
.navbar a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
/* The dropdown container */
.dropdown {
float: left;
overflow: hidden;
}
/* Dropdown button */
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
}
/* Add a red background color to navbar links on hover */
.navbar a:hover,
.dropdown:hover .dropbtn {
background-color: red;
}
/* Dropdown content (hidden by default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
/* Add a grey background color to dropdown links on hover */
.dropdown-content a:hover {
background-color: #ddd;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<h1>Simple Pure CSS Drop Down Menu</h1>
<div class="navbar">
Home
News
<div class="dropdown">
<button class="dropbtn">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
</div>
You can use flexbox as mentioned in another answer. Another option is removing float properties on the navbar items, and add display: inline-block. Then center everything by adding text-align: center to the navbar
/* Navbar container */
.navbar {
overflow: hidden;
background-color: #333;
font-family: Arial;
text-align: center;
}
/* Links inside the navbar */
.navbar a {
display: inline-block;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
/* The dropdown container */
.dropdown {
display: inline-block;
}
/* Dropdown button */
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
}
/* Add a red background color to navbar links on hover */
.navbar a:hover,
.dropdown:hover .dropbtn {
background-color: red;
}
/* Dropdown content (hidden by default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
/* Add a grey background color to dropdown links on hover */
.dropdown-content a:hover {
background-color: #ddd;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<h1>Simple Pure CSS Drop Down Menu</h1>
<div class="navbar">
Home
News
<div class="dropdown">
<button class="dropbtn">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
</div>
You could add the following to .navbar:
display: flex;
justify-content: center;
I'm trying to create a simple dropdown menu (with only CSS) in a navbar with multiple links, the problem is that the dropdown menu will show with every link and not only with the desired one. Here is the code:
HTML & CSS
ul {
list-style: none;
}
.topNav {
background-color: #ff66b3;
overflow: hidden;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
position: relative;
}
.topNav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 8px;
text-decoration: none;
font-size: 14px;
}
.topNav a:hover {
background-color: #ffb3d9;
color: black;
}
.dropDown {
display: none;
position: absolute;
z-index: 1;
min-width: 160px;
}
.dropDown a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.topNav .mainLink:hover .dropDown {
display: block;
}
<div class="topNav" id="myTopnav">
<a class="mainLink" id="menuIcon" href="#home">☰</a>
<a class="mainLink" href="#home">NerdBook</a>
<div class="dropDown">
News
Contact
About
</div>
</div>
The dropdown will have to show only on the "menuIcon" link.
You already have an ID on the <a> tag you want to trigger the :hover so use that as reference selector. Also your code doesn't work because .dropDown ins't a child of your link change it to match as sibling:
#menuIcon:hover ~ .dropDown
ul {
list-style: none;
}
.topNav {
background-color: #ff66b3;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
position: relative;
}
.topNav:after {
content:"";
display:table;
clear:both;
}
.topNav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 8px;
text-decoration: none;
font-size: 14px;
}
.topNav a:hover {
background-color: #ffb3d9;
color: black;
}
.dropDown {
background:#e1e1e1;
display: none;
position: absolute;
top:100%;
z-index: 1;
min-width: 160px;
}
.dropDown a {
float:none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
#menuIcon:hover ~ .dropDown, .dropDown:hover {
display: block;
}
<div class="topNav" id="myTopnav">
<a class="mainLink" id="menuIcon" href="#home">☰</a>
<a class="mainLink" href="#home">NerdBook</a>
<div class="dropDown">
News
Contact
About
</div>
</div>
Okay so I'm trying to learn CSS and I'm trying to insert a dropdown button in a navbar.
What's happening is that when I hover the button the whole navbar is expanding instead of only the content of the button. What am I doing wrong? Thanks!
ul {
list-style-type: none;
margin-left:8%;
margin-right: 8%;
padding-left: 0;
padding-right: 0;
overflow: hidden;
background-color: #f3f3f3;
}
.right{
float: right;
}
li a {
float: left;
display: block;
color: #6f6f6f;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #e0e0e0;
}
p {
color: red;
margin-left: 8%;
margin-right: 8%;
}
body{
font-family:Arial;
background-color:#f9f9f9;
}
.dropbtn {
background-color: #f3f3f3;
color: #6f6f6f;
padding: 14px 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: block;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
right: 0;
display: none;
position: relative;
background-color: #f9f9f9;
min-width: 10px;
}
/* Links inside the dropdown */
.dropdown-content a {
right: 0;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #e0e0e0}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
right:0;
}
/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
background-color: #e0e0e0;
}
<ul>
<li><img src="../../static/image/logoRect.png" width="25"> </li>
<li>Movies</li>
<li><a class="right" href="">Search</a></li>
<div class="right">
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
Links 1
Link 2
Link 3
</div>
</div>
</div>
<li><a class="right" href="#about">Explore</a></li>
</ul>
You have to specify a height and remove the overflow hidden.
ul {
list-style-type: none;
margin-left:8%;
margin-right: 8%;
padding-left: 0;
padding-right: 0;
// overflow: hidden; <---remove
height: 48px; // <---add
background-color: #f3f3f3;
}
If you don't, the ul defaults to as if it has:
height: auto;
Relevant fiddle
You should specify the height of the <ul> element, so it won't expand with the dropdown.