CSS Dropdown menu not falling under parent menu - html

I was just having a small problem and I'm not sure how to fix it. I'm still new to HTML and CSS and I was trying to get the drop down menu to work, but after looking at various tutorials I can only get it so when I hover over the parent folder the sub menu falls either to the left or right and not underneath the parent.
<ul id="navigate">
<li class="current"> Home</li>
<li> <a href = ".html" >A</a>
<ul>
<li>B </li>
</ul>
</li>
<li> C</li>
<li> D</li>
<li> E</li>
<li> F</li>
</ul>
Above is the portion of the code for my nav bar. html have files to them
This is my CSS:
/*
* Navigation Bar
*/
#navigate
{
padding: 0;
margin-top: 5px;
list-style-type: none;
color: black;
float:left;
width: 100%;
background-color: #00FF00;
}
#navigate li
{
display: inline;
position: relative;
}
#navigate li a
{
float: left;
width: 7em;
background-color: #00FF00;
text-decoration: none;
border-right: 1px solid #FFFFFF;
padding-left: 1em;
padding-right: 1em;
text-align: center;
}
#navigate li a:hover
{
background-color: #D1E751;
color: #26ADEA;
}
#navigate li.current a
{
background:#D1E751;
}
/*
* SubMenu
*/
#navigate li ul
{
display: none;
position: absolute;
padding: 0;
}
#navigate li:hover ul
{
display: inline-block;
}
Currently the code works in that it shows the submenu to the right of the parent menu. I'm not sure how to get it to fall directly below the parent menu, any help would be greatly appreciated. Or any tips if my code looks a little odd this is my first semester in HTML, CSS and programming in general.
Shows up like this (just showing the one menu
parent sub
----------------------------------
A | B |
----------------------------------
I would like it to look like this
------------
A | Parent
------------
B | sub
------------
Thanks in advance
------------------------------ UPDATE 5/9/2014 ----------------
So now I've managed to get it to work, but when it shows up it moves the content....
------------
A | Parent
------------
contentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontent
------------
A | Parent
------------
B | sub contentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontent
------------
contentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontent
i'm thinking it might have to do with the float:left and where I've cleared the float or the positioning. When I add a position: absolute it just moves the sub menu over the parent menu though.
Updated Code
/*
* Navigation Bar
*/
#navigate
{
padding: 0;
margin-top: 5px;
list-style-type: none;
color: black;
width: 100%;
background-color: #00FF00;
position: relative;
}
#navigate li
{
float: left;
padding-right: 2px;
}
#navigate li a
{
float: left;
width: 7em;
background-color: #32CD32;
text-decoration: none;
text-align: center;
border-radius: 25px;
padding: 5px;
margin: auto;
}
#navigate li a:hover
{
background-color: #D1E751;
color: #26ADEA;
}
#navigate li.current a
{
background:#D1E751;
}
/*
* SubMenu
*/
#navigate ul
{
display: none;
list-style: none;
padding: 0;
}
#navigate ul li
{
clear: left;
display: block;
}
#navigate li:hover ul
{
display: block;
}
Thanks in advance.
--------Update few mins later >.< -------
I ended up putting it all in a div and formatting the div now it works.
/*
* Navigation Bar
*/
#navigate
{
padding: 0;
margin-top: 5px;
list-style-type: none;
color: black;
width: 100%;
background-color: #00FF00;
position: relative;
}
#navigate li
{
float: left;
padding-right: 2px;
}
#navigate li a
{
float: left;
width: 7em;
background-color: #32CD32;
text-decoration: none;
text-align: center;
border-radius: 25px;
padding: 5px;
margin: auto;
}
#navigate li a:hover
{
background-color: #D1E751;
color: #26ADEA;
}
#navigate li.current a
{
background:#D1E751;
}
/*
* SubMenu
*/
#navigate ul
{
display: none;
list-style: none;
padding: 0;
}
#navigate ul li
{
clear: left;
display: block;
}
#navigate li:hover ul
{
display: block;
}
.nav
{
position: absolute;
z-index: 2;
margin: auto;
}
changes made to html just enclosed in

I understand that you have difficulty with your pop-up menu, if so, here's one of the solutions
DEMO

I think it will help if you add top position to ul element.
#navigate li ul
{
display: none;
position: absolute;
padding: 0;
top: 100%;
left: 0;
}
#navigate li:hover ul
{
display: block;
}
Here display: block; will be more apropriate, I think.

when you set
#navigate li
{
display: inline;
position: relative;
}
it applies it to all of the li elements including the submenu, which means all the li align horizontally including the submenu, because of the display: inline;
so change it to
#navigate > li
{
display: inline;
position: relative;
}
to target only direct li children
and give your submenu ul a class mySubmenu and set its position to absolute:
.mySubmenu
{
position:absolute;
}

Related

Is there any problem with this code? I'm new on html & css

I'm not sure this code is written in the shortest and most effective way. Please help me.. How can i write in the shortest way? And how can I improve myself?
* {margin: 0;padding: 0;box-sizing: border-box;}
body {
background: #fff;
font-family: Arial;
}
a {
text-decoration: none;
color: #333;
}
nav {background-color: #f7f7f7;
width: 100%;
display: block;
}
nav ul {
list-style-type: none;
}
nav ul li {
display: inline-block;
position: relative;
}
nav ul:not(.sub-menu) > li {
padding: 15px;
}
nav ul:not(.sub-menu) > li + li {
margin-left: 20px;
}
nav ul li.has-children:hover ul.sub-menu {display: block;
}
nav ul.sub-menu {
position: absolute;
display: none;
white-space: nowrap;
/* Her bir liste öğesinin tek bir satır olması için. */
top: 100%;
}
nav ul.sub-menu li {
display: block;
}
nav ul.sub-menu li a {
display: block;
padding: 12px 20px 12px 10px;
background-color: #f7f7f7;
border-with: 0 1px 1px 1px;
border-color: #eee;
border-style: solid;
}
nav ul.sub-menu li a:hover {background-color: #eee;}
nav ul.sub-menu li + li a {
border-top: 0;
}
Well the code itself is pretty short. You can take care of a few points which will help you in the long run.
Try to minimise the use of > and +. As these are strictly bound to the structure of the DOM. If the DOM changes, styles with > or + might break.
Minimise the hierarchy. nav ul li can be replaced with nav li. Lesser the hierarchy, faster will be the DOM painting.
Something more you should do is separate the css in 2 parts.
Part 1: All element level css (lets call it normalize.css) and
Part 2: All class based css(lets call it styles.css).
Normalize.css now has the global styles for the html elements, which is common throughout your web page.
* {margin: 0;padding: 0;box-sizing: border-box;}
body {
background: #fff;
font-family: Arial;
}
a {
text-decoration: none;
color: #333;
}
nav {
background-color: #f7f7f7;
width: 100%;
display: block;
}
nav ul {
list-style-type: none;
}
nav li { // note that the `ul` is removed
display: inline-block;
position: relative;
}
style.css now containes all class based css which is independent of its position in the DOM.
.menu-item { // add a class `menu-item` to the immediate UL items in the <nav/>
padding: 15px;
margin-left: 20px;
}
.menu-item:first-child {// remove margin-left from the first-child
margin-left: 0;
}
.menu-item:hover .sub-menu {
display: block;
}
.sub-menu { // removing the unnecessary qualifiers. `.sub-menu` is enough
position: absolute;
display: none;
white-space: nowrap;
top: 100%;
}
.sub-menu-item { // add class `sub-menu-item` to the <li> of `.sub-menu`
display: block;
}
.sub-menu-item a { // you can further go ahead to add a special class to the `<a/>` inside `.sub-menu-item`
display: block;
padding: 12px 20px 12px 10px;
background-color: #f7f7f7;
border-with: 0 1px 1px 1px;
border-color: #eee;
border-style: solid;
}
.sub-menu-item a:hover {
background-color: #eee;
}
nav ul.sub-menu li + li a { // similarly you can remove the `+` with `:first-child` or the suitable.
border-top: 0;
}
Your css is accurate which is a good point. But if you want to make your life easier and not repeat yourself, I advise you to use a css preprocessor like Sass to write css like this :
nav {
background-color: #f7f7f7;
width: 100%;
display: block;
ul {
list-style-type: none;
li {
display: inline-block;
position: relative;
}
}
}

How to add a dropdown link to an existing responsive navigation menu

I have an existing responsive nav menu that I want to add a sub-menu to (actually it is just one link under one of the top menu items). Sounds like it should be very EASY, but I cannot figure it out. As soon as I add the link, it either ends up just below the top item (making the whole nav grow down with it) or displaying "none" makes it disappear and not come back on hover. Is there a simple way to do this with CSS only? I hope my question is clear enough. I will include my necessary code. If you give me code, please tell me where to put it. I am a Newbie. Thanks so much for any help.
HTML:
<nav><a href="index.html">
<div id="logo"><img src="images/logo-text.png" alt="CBS Stuctures, Inc."></div>
</a>
<label for="drop" class="toggle">MENU</label>
<input type="checkbox" id="drop" />
<ul class="menu">
<li>HOME</li>
<li>COMPLETED PROJECTS</li>
<li>STRUCTURES
<ul>
<li>Video Presentation</li>
</ul>
</li>
<li>NEW PRODUCTS</li>
<li>CONTACT</li>
</ul>
</nav>
CSS:
nav {
height: auto;
margin: 0;
padding: 0;
background-color: #000;
}
#logo {
display: block;
float: left;
padding: 0;
}
nav:after {
content: "";
display: table;
clear: both;
}
nav ul {
display: inline-block;
font-size: 1.5em;
list-style: none;
float: right;
}
nav ul li {
display: inline-block;
float: left;
}
nav a {
display: block;
padding: 10px 20px;
color: #fff;
text-decoration: none;
}
.toggle, [id=drop] {
display: none;
}
nav a:hover {
color: #70E4FC;
}
nav ul li ul{
display: none;
}
nav ul li ul:hover{
display: block;
}
#media (max-width: 1024px) {
#logo {
display: block;
width: 100%;
text-align: center;
float: none;
padding: 0;
}
nav ul{
width: 100%;
padding:0;
margin:0;
float: none;
background-color: rgba(16,70,56,1.00);
}
nav ul li {
width: 100%;
box-sizing: border-box;
padding: 10px;
background-color:rgba(11,51,41,1.00);
}
nav ul li:hover{
background-color:#0F4739;
}
.toggle + a, .menu{
display: none;
}
.toggle{
display:block;
background-color: #333333;
padding: 14px;
font-size: 1.5em;
cursor: pointer;
}
.toggle:hover {
background-color:#515151;
}
[id^=drop]:checked + ul{
display: block;
}
}
You can simply add a hover selector on child ul inside li of a parent ul
ul li:hover ul {display: block;}
To apply it to specific element, you must add a class to your child ul:
ul li:hover ul.childul {display: block;}
check working example here

Nav dropdown menu issues

Having some trouble with my nav, i'm trying to create a dropdown menu when you hover over the "Match" link. every time I hover the mouse over the link, list will stay underneath said link and disrupt how the navigation bar looks. Any helps will be much appreciated
HTML
<nav>
<span class= "navbar-button"></span>
<ul class="navbar">
<li>Home</li>
<li>Gallery</li>
<li>Match
<ul>
<li>City</li>
<li>Coastal</li>
<li>Rural</li>
</ul>
</li>
</ul>
</nav>
CSS
body{
padding: 0;
margin: 0;
font-family: 'main';
}
.navbar {
list-style: none;
background-color: #333;
color: #fff;
margin: 0;
text-align: center;
}
.navbar > li {
display: inline-block;
padding: 1.3% 2%;
}
.navbar > li:hover {
background-color: #585858;
}
.navbar > li > a{
text-decoration: none;
font-size: 30px;
color: #fff;
}
.navbar li ul {
display: none;
}
.navbar li:hover ul{
display: block;
}
you need to use absolute positioning to break it out of the container so it won't disrupt the rest of the elements:
.navbar li ul {
position: absolute;
display: none;
background-color: #333;
margin: 0;
text-align: center;
padding: 20px;
}
Be sure to set its parent to position: relative or the body will become its container:
.navbar > li {
position: relative;
display: inline-block;
padding: 1.3% 2%;
}
JSFIDDLE
Add position absolute to: .navbar li ul

center align a floated nav bar and dropdown from left to right

i want to center align whats in the nav bar( so that the img is the center of the nav bar. I know that i have float: left; but if i dont, the(left thing, right thing) will drop down to the bottom edge of the img. So what i want is to keep float left, but be able to display: inline-block, or something equal. I also want the dropdown bar for the (left thing) to start at the img's left side, and then build out to the right side.
demo fiddle
HTML
<div id="nav">
<div id="container">
<ul>
<li>
Left thing
<ul>
<li><----- want it to go this way
</li>
<li>i want this to start under left thing
</li>
</ul>
</li>
<li>
<img src="http://www.jonathanjeter.com/images/Square_200x200.png" style="height:70px" />
</li>
<li>
Right thing
<ul>
<li>This starts right
</li>
<li>And this is right
</li>
</ul>
</li>
</ul>
</div>
</div>
CSS
* {
padding: 0;
margin: 0;
}
#body {
padding: 0;
margin: 0;
font-family: Arial;
font-size: 17px;
}
#nav {
background-color: 72776A;
width: 100%;
position:fixed;
height:50px;
}
#nav ul {
list-style-type: none;
padding: 0;
margin: 0;
position: relative;
display:block;
}
#nav ul li {
float:left;
}
#container {
text-align:;
}
#nav ul li:hover {
background-color: #333;
}
#nav ul li a, visited {
color: ACD661;
display: block;
padding: 15px;
text-decoration: none;
}
#nav ul li:hover ul {
display: block;
}
#nav ul ul {
display: none;
position:absolute;
color: red;
border: 1px solid black;
}
#nav ul ul li {
display: block;
}
#nav ul ul li a:hover {
color: #699;
}
replace
#container {
width: 270px;
margin: 0px auto;
}
Check this fiddle
Before talk about solution let's talk about your CSS, i removed repeats for margin:0, padding:0, highlighted forgetted selectors or mistyping. You need to precise a width to your container div and add margin-left and margin-right auto to achieve what you want :
/* {
padding: 0;
margin: 0;
} not the best solution, everybody don't need this rule */
/* instead here a group of selectors */
body,
#nav ul {
padding: 0;
margin: 0;
}
/*#*/body { /* you mean just body right ? */
font-family: Arial;
font-size: 17px;
}
#nav {
background-color: #72776A; /* you forgot # here */
width: 100%;
position:fixed;
height:50px; /*it's smaller than your image*/
}
#nav ul {
list-style-type: none;
position: relative;
/*display:block; don't need */
}
#nav li {
float:left;
}
#container {
/*text-align:; seems it's missing something here ? */
margin: 0 auto;
width: 300px;
}
#nav ul li:hover {
background-color: #333;
}
#nav ul li a,
#nav ul li a:hover,
#nav ul li a:visited {/ { /* you mean a:visited ? */
color: #ACD661; /* here again # forgot */
display: block;
padding: 15px;
text-decoration: none;
}
#nav li:hover ul { /*ul don't need (i mean #nav ul li...) */
display: block;
}
#nav ul ul {
display: none;
position:absolute;
color: red;
border: 1px solid black;
}
#nav li li { /* shorter then ul ul li */
display: block;
}
#nav a li a:hover {
color: #699;
}
You can see code here

responsive tab menu with centered dropdown area

This is what it should look like:
There is a tab-menu and a dropdown area. This should always have the same position (but different content and the respective tab choosen) as in the picture. Meaning it should always be as wide as the tabs(-menu).
But I can not figure out how:
to get this responsive
how to have the dropdown area stay where it is
how to style the subitems (in the dropdown area)
Here is what I got so far (sorry for the huge css it is not cleaned yet!), the menu starts at line 1559.
http://jsfiddle.net/pxpHw/
How do I do this properly?
THANKS!
code:
// css
nav {
cursor: default;
}
a {
text-decoration: none;
color: #000000;
}
#menu ul {
margin: 0px;
padding: 0px;
left: 0;
list-style-type: none;
background:green;
z-index: 100;
max-width: 60em;
}
#menu li {
float: left;
width: 20%;
text-align: center;
}
#menu li ul {
display: none;
/*display: block;*/
padding-top: 3px;
}
#menu li:hover ul {
display: block;
}
#menu li ul li {
background-color: #2F2D49;
border-bottom: 1px solid #FFFFFF;
width: 100%;
max-width: 60em;
min-height: 30em;
position: absolute;
}
#menu li ul li a {
color: #FFFFFF;
}
#menu li ul li:hover {
background-color: #232323;
}
Use media queries to get the responsive design
Check the following links
http://mobile.smashingmagazine.com/2010/07/19/how-to-use-css3-media-queries-to-create-a-mobile-version-of-your-website/
http://mediaqueri.es/
Thanks
AB
This is what I was looking for: (Columns and Layout tab)
http://codecanyon.net/item/css3-full-responsive-dropdown-menu/full_screen_preview/4528828