How do I make nested drop down menus in HTML? - css

I am working on my school's website, and I have already figured out how to make single drop downs. Here is the code for one menu item.
.navbar {
overflow: hidden;
background-color: #0a0a2a;
font-family: Arial;
border: 3px solid #C5B358;
margin-left: -75px;
margin-right: -75px;
}
.navbar a {
float: left;
font-size: 12px;
color: white;
text-align: center;
padding: 8px 8px;
text-decoration: none;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 12px;
border: none;
outline: none;
color: white;
padding: 8px 8px;
background-color: #0a0a2a;
}
.navbar a:hover,
.dropdown:hover .dropbtn {
background-color: #C5B358;
padding-top: 9px;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #0a0a2a;
min-width: 140px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
color: white;
}
.dropdown-content a {
float: none;
color: white;
padding: 10px 10px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #C5B358;
color: #0a0a2a;
}
.dropdown:hover .dropdown-content {
display: block;
}
<form action="index.html">
<div class="dropdown">
<button class="dropbtn">HOME </button>
<div class="dropdown-content">
</div>
</div>
</form>
<!--Modular code begins here-->
<form action="aboutus.html">
<div class="dropdown">
<button class="dropbtn">ABOUT US
</button>
<div class="dropdown-content">
Calendar 2017-2018
Inclement Weather Information
Directions to Tri-County
2016 Report Card
District Information
Business Office
Contact Us
Public Records
School Committee
</div>
</div>
</form>
This works just fine, but some of the menu options, in this case "Business Office", have a submenu that needs to be added. Ideally, this would be a hover drop down that appears to the right of the drop down menu.
However, I can not figure out how this should work, and nothing on the internet seems to answer my question fully. So, I decided to consult trusty old Stack Overflow.
I tried adding the following code to my HTML, making new classes in the CSS with the same properties as the original drop down menu. It did not work.
<div class="sub-dropdown">
<button class="sub-dropbtn"> District Information</button>
<div class="sub-dropdown-content">
School Committee
</div>
</div>
If anybody can help me figure out where I went wrong, that would be much appreciated.

Related

Display drop down menu is not working css

I have a weird problem with my project, I tried to create drop down menu,
and I used even simple example from 3 w school and it is not working, here is my code:
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
padding: 12px 16px;
z-index: 1;
}
.dropdown:hover .dropdown-content {
display: block;
}
<div class="dropdown">
<span className="span1">Mouse over me</span>
<div class="dropdown-content">
<p>Hello World!</p>
</div>
</div>
I saw solutions like .dropdown:hover +.dropdown-content and it is not working too.
This works & only hovers on text because your .dropdown has no padding to it.
In this example, I added padding & pushed down the open menu that you had position absolute on.
Now when space around is hovered, the dropdown is triggered. I also added the :focus-within tag to the css trigger, this allows a user to tab into the parent menu then the open menu.
.dropdown {
background-color:orange;
padding: 20px;
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
top:60px;
left:0;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
padding: 12px 16px;
z-index: 1;
}
.dropdown:hover .dropdown-content,
.dropdown:focus-within .dropdown-content
{
display: block;
}
<div class="dropdown">
<span className="span1">Mouse over me</span>
<div class="dropdown-content">
<p>Hello World!</p>
</div>
</div>

how to make a drop down content flow to right when overflowing

I'm trying to make a drop down component (in Reactjs), and I am having trouble on how to make it not pop up outside the browser width.
I have tried to set right: 0px, it works fine when the dropdown is at the rightmost of the panel, but if the dropdown is not then it gets messed up.
here is the codepen for the sample.
here is a sample css
.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;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown:hover .dropdown-content {
display: block;
}
here is a sample html
<li class="dropdown">
Dropdown
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</li>
, you can notice that there is a scrollbar there.
What I want to achieve is that I can right align the drop down content, relative to the drop down header.
Thanks
You just need to adjust right side dropdown contents. One possible way is as follow
<div class="dropdown-content d2">
Link 1
Link 2
Link 3
</div>
.d2 {
right:0;
}
Ping me if you have any query.
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
float: right;
}
li {
float: left;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
li.dropdown {
display: inline-block;
}
div.dropbtn {
display: inline-block;
color: black;
}
.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;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display: block;
}
.d2 {
right:0;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<ul>
<li>Home</li>
<li>News</li>
<li class="dropdown">
Dropdown
<div class="dropdown-content d2">
Link 1
Link 2
Link 3
</div>
</li>
</ul>
<h3>Dropdown Menu inside a Navigation Bar</h3>
<p>Hover over the "Dropdown" link to see the dropdown menu.</p>
<div class="dropdown">
some other dropdown
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
</body>
</html>

form not vertically centered in flexbox

I have two bars:
1.Top bar - Name, Search bar, Account, Cart
2.Navigation Bar (with dropdowns)
The problem is with the top bar. Im trying to have all the elements be on the same line, with Account and Cart on the right side. I tried using flex but now the search bar is not in line with the other elements. Event Account and Cart are kind of off line. When I don't use flex and just try to float Account and Cart to the right, they are swapped(Cart comes first, then Account), and also they don't become in line, they're kind of floating on the top right.
HTML:
<div class="top">
<h1>TDX</h1>
<form>
<div>
<input type="search" id="mySearch" name="q"
placeholder="Search the site..." size="50">
<button>Search</button>
</div>
</form>
<h4>Account</h4>
<h4>Cart</h4>
</div>
<ul class="topnav">
<li><a class="active" href="index.html">Home</a></li>
<li class="dropdown">Products
<div class="dropdown-content">
Computers
Tablets
Cell Phones
Wearable Technologies
Accessories
</div>
</li>
<li class="dropdown">Brands
<div class="dropdown-content">
Apple
Samsung
Lenovo
Dell
HP
Sony
Panasonic
Motorola
HTC
</div>
</li>
<li>Deals</li>
</ul>
CSS:
.topnav{
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
margin:0 auto;
text-align:center;
}
.topnav li {
float: left;
}
.topnav li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
width:120px;
}
.topnav li a:hover, .dropdown:hover .dropbtn {
background-color: #111;
}
li.dropdown {
display: inline-block;
}
.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;
}
.topnav .dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.topnav .dropdown-content a:hover {background-color: #f1f1f1;}
.dropdown:hover .dropdown-content {
display: block;
}
.top{
display: flex;
align-content: space-between;
}
.top a{
margin: 0 5px;
}
.top h1, .top a{
flex: 0 1 auto;
}
.top form{
flex: 1 1 auto;
vertical-align:middle;
}
This is a screenshot of how it looks
Add align-items: center; to your .top element in CSS.
Your updated .top CSS element:
.top {
display: flex;
align-content: space-between;
align-items: center;
}
Here's a demo: https://jsfiddle.net/7p1phnd8/3/

Why is dropdown moved to the left in IE 11 but not in Chrome or FF?

I have created a dropdown menu that works great in Chrome and Firefox but when trying it in IE 11 the dropdown content ends up on the side.
I've googled and search but cannot find a solution.
note that it should go into a Django Administration page header and that's why there are a few !important. It also means that the dropdown should be on top of all other elements on the page.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1 style="text-align:left;float:left;">Test</h1>
<style type="text/css">
.dropbtn {
background-color: #417690;
color: #f5dd5d !important;
padding: 6px;
font-size: 16px;
border: none;
cursor: pointer;
}
.droplink {
background-color: #417690;
color: #f5dd5d !important;
padding: 4px;
font-size: 16px;
border: none;
cursor: pointer;
}
/* The container <div> - needed to position the dropdown content */
.dropdowncontainer {
display: inline-block;
padding-top: 10px;
}
.dropdown {
float: left;
display: inline-block;
}
/* 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: 1000;
}
/* Links inside the dropdown */
.dropdown-content a {
color: black !important;
padding: 6px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {
background-color: #f1f1f1;
color: black;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
background-color: #376880;
}
.droplink:hover {
background-color: #376880;
}
</style>
<div class="dropdowncontainer">
<div class="dropdown">
<a class="dropbtn" href="">XXX</a>
<div class="dropdown-content">
Installations
Files
Tasks
Releases
Copy
</div>
</div>
<a class="droplink" href="node.html">Nodes</a>
</div>
</body>
</html>
I've created a JSFiddle with the code. Any help would be greatly appreciated.
JSfiddle
Try giving position: relative to .dropdown class.
The behaviour of position: absolute elements when no explicit position: relative element has been specified varies from browser to browser. Hence, the anomaly.
Also add top: 0 & left: 0 to .dropdown-content
.dropdown-content {
top: 0;
left: 0;
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1000;
}
Try set for .dropdown-content left and top values.

Display:Inline-Block Ignored

This is driving me insane, and I just can't figure out the cause. I have a JSP application that is connected to bootstrap. First of all, I am by no means an expert in CSS, and am green in that area. My issue is that inline-block just will not work. My bootstrap is used for the collapse plugin that allows me to use a menu button instead of the regular menu in smartphones.
When I use a desktop-size, the menu items are displayed in block format (vertical), even though I have used display: inline-block. Here's my code, and if anyone has an idea why inline-block is not working please let me know.
The html:
<ul id="menu-items" class="navbar-collapse collapse menubar-items">
<li>
<a class="menuItems" href="home.jsp">Home</a>
</li>
<li>
<a class="menuItems" href="business.jsp">Business</a>
</li>
<li>
<a class="menuItems" href="new-form.jsp">New Order</a>
</li>
<li>
<a class="menuItems" href="logout.do">Logout</a>
</li>
</ul>
The css:
#menu-items {
min-width: 250px;
background: #CFC3F8;
box-shadow: 5px 5px 2px #888888;
padding: 0;
float: right;
}
#menu-items li {
height: 100%;
width: 100%;
margin-right: 10px;
text-decoration: none;
vertical-align: middle;
text-align: center;
line-height: 60px;
font-size: 16px;
border-left: solid white 2px;
color: #332419;
//The attempt
display: inline-block;
}
#menu-items li a.menuItems {
height: 100%;
width: 100%;
}
#menu-items li a.menuItems:hover {
color: #332419;
background-color: #f1f1f1;
}
//ensuring that bootstrap doesn't override it
.navbar-collapse li {
display: inline-block;
text-decoration: none;
}
.collapse li {
display: inline-block;
text-decoration: none;
}
ul li {
display: inline-block;
text-decoration: none;
}

Resources