Setting drop-down menu - html

I have made a drop down menu and got some styling issues with it. My code for drop down menu is
<ul class="dd">
<li><a href="main.php" >Home</a>
</li>
<li>Profile
</li>
<li>Inbox' . $unread_numrows . '
</li>
<li>Management
<ul>
<li>Settings
</li>
<li>Logout
</li>
</ul>
</li>
</ul>';
CSS
#media screen and (max-width: 1280px) {
.dd {
background-color:#BF3B3D;
position: absolute;
right:0px;
top:0;
margin-right: 4%;
}
}
#media screen and (min-width: 1280px) {
.dd {
background-color:#BF3B3D;
position: absolute;
right:0px;
top:0;
margin-right: 10%;
}
}
#media screen and (min-width: 1920px) {
.dd {
background-color:#BF3B3D;
position: absolute;
right:0px;
top:0;
margin-right: 25%;
}
}
.dd li {
border-radius: 4px;
background-color: #BF3B3D;
float: left;
position: relative;
list-style: none;
top:8px;
padding: 10px 5px 10px 5px;
}
.dd li:hover{
background-color: #7A0709;
}
.dd li a{
font-size: 14px;
color:#ffffff;
text-decoration: none;
background-color: #BF3B3D;
}
.dd li ul li{
top: 14px;
border-radius: 4px;
}
.dd li:hover ul{
background-color: #BF3B3D;
}
.dd li:hover a{
background-color: #7A0709;
}
.dd li:hover li a{
background-color:#BF3B3D;
}
.dd li ul{
display: none;
}
.dd li:hover ul {
display: block;
position: absolute;
}
.dd li:hover li {
float: none;
}
.dd li ul li:hover a{
background-color: #7A0709;
}
Now when I hover over menu it's color changes and so for sub-menu's, but when sub-menu's are open under menu the color for that menu is also changed though I am rolling over sub-menu so when rolling over sub-menu I want only sub-menu's color changed and menu should go back to it's color. Also their appears to be some space between sub-menu and menu that took some of space inside menu I want to get rid of that space.

To fix that hover effect problem use immediate children selector for main menu hover css. like this.
.dd > li:hover{
background-color: #7A0709;
}
">" will only select immediate children only. so on sub menus will only effect by your other css hover.
to fix height problem you should provide top main menu a display:block and fixed height as you want. to help on that more, you better provide a fiddle.

Related

Collapsible nav bar not working as expected and creating unnecessary padding (Not using bootstrap)

I am trying to create a simple navigation bar which when resized (made smaller) will create a small menu which once clicked will show the links, much like Bootstrap.
Issue 1: When resized the links do disappear, as intended, but when I expand the mini menu (click show navigation), the links (link 1, link 2 etc) show up on my logo and just in general off place ( I want the links to show up neatly, like the image below).
Issue 2: As you will see with image below, the code I have, for some reason causes a gap to emerge between each <li>.
Here is a fiddle created to show the issues and the code used: https://jsfiddle.net/n00jg7xy/
FOr the second problem, about padding, the problem is here:
#nav > ul > li{
width: 25%; //Here you are forcing to 1/4 of the space, remove this line
height: 100%;
float: left;
}
I dont know if the snippet size is going to let you see the change, here is the fiddle.
#wrapper{
width: 100%;
height: auto;
}
#header{
height: auto;
border-bottom: 1px solid black;
}
#logo {
float: left;
}
/******************* Main Navigation ************************/
.mainNav{
margin: 10px;
}
#nav > a{
display: none;
}
#nav li{
position: relative;
}
#nav > ul{
height: 3.75em;
}
#nav > ul > li{
height: 100%;
float: left;
}
#nav li ul{
display: none;
position: absolute;
top: 100%;
}
#nav li:hover ul{
display: block;
}
#media only screen and ( max-width: 40em ){
#nav{
position: relative;
}
#nav > a{
}
#nav:not( :target ) > a:first-of-type,
#nav:target > a:last-of-type
{
display: block;
}
/* first level */
#nav > ul
{
height: auto;
display: none;
position: absolute;
left: 0;
right: 0;
}
#nav:target > ul
{
display: block;
}
#nav > ul > li
{
width: 100%;
float: none;
}
/* second level */
#nav li ul
{
position: static;
}
}
/***********************/
/* Remove margins and padding from the list*/
ul.topnav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
/* Float the list items side by side */
ul.topnav li {
float: left;
}
/* Style the links inside the list items */
ul.topnav li a {
display: inline-block;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
transition: 0.3s;
font-size: 14px;
}
/* Change background color of links on hover */
ul.topnav li a:hover {
background-color: #f9f9f9;
color: darkred;
transition: 0.3s;
}
/* Hide the list item that contains the link that should open and close the topnav on small screens */
ul.topnav li.icon {
display: none;
}
/* When the screen is less than 680 pixels wide, hide all list items, except for the first one ("Home"). Show the list item that contains the link to open and close the topnav (li.icon) */
#media screen and (max-width:680px) {
ul.topnav li:not(:first-child) {display: none;}
ul.topnav li.icon {
float: right;
display: inline-block;
}
}
/* The "responsive" class is added to the topnav with JavaScript when the user clicks on the icon. This class makes the topnav look good on small screens */
#media screen and (max-width:680px) {
ul.topnav.responsive {position: relative;}
ul.topnav.responsive li.icon {
position: absolute;
right: 0;
top: 0;
}
ul.topnav.responsive li {
float: none;
display: inline;
}
ul.topnav.responsive li a {
display: block;
text-align: left;
}
}
<div id="wrapper">
<p>
Resize the window
</p>
<div id="header">
<div><img id="logo" src="http://i65.tinypic.com/352i0jq.jpg" alt="logo" href="#"> </div>
<div class="mainNav">
<nav id="nav" role="navigation">
Show navigation
Hide navigation
<ul class="topnav" id="myTopnav">
<li>Home</li>
<li>Link 1</li>
<li>Link 2</li>
<li> Link 3</li>
<li> Link 4</li>
</ul>
</nav>
</div>
</div>
</div>
<!-- wrapper closed-->

Make text in drop down menu white

This is my website.. when you hover over the nav items and a drop down list appears, i want the drop down list to have white text permanently, not turn white.
Also if anyone knows how to make it so when you hover over the menu items a black line appears under the word not the whole background of the word goes black?
http://opax.swin.edu.au/~9991042/DDM10001/brief_2/Amalfi%20Coast/www_root/
#nav {
padding: 50px;
width: 924px;
height: 100px;
float: none;
}
#nav ul {
list-style: none;
margin-left: 5px;
width: 1000px;
display: table;
}
#nav a {
text-decoration: none;
color: #161717;
}
/*hide sub menu*/
#nav li ul {
display: none;
}
/*show and position*/
#nav li:hover ul {
display: block;
position: absolute;
margin-left: 0px;
margin-top: 0px;
}
/*main nav*/
#nav li {
width: 140px;
font-size: 14px;
display: inline-block;
-webkit-transition: all ease 0.3s;
}
#nav li:hover {}
/*sub nav*/
#nav li li {
color: white;
display: block;
background-color: black;
font-size: 11px;
padding-top: 5px;
padding-left: 5px;
width: 100px;
}
#nav li li:hover {
background-color: #A83133;
}
#nav a:hover {
color: white;
}
<div id="nav">
<div id="firstnav">
<ul>
<span class="font4"><li>SIGN IN</li>
<li>SIGN UP</li>
<li>MY TRIP</li>
</ul></span>
</div>
<ul>
<li>DESTINATIONS
<ul>
<li>Popular Places
</li>
<li>Other places
</li>
</ul>
</li>
I'm unsure if your question is about your top-link turning black when not being hovered
The reason this is happening is you put your hover on your a-element.
a-tags are by default inline elements. Which means they will only take up as much space as the text.
This means that when you hover on your li-element the hover on your link is no longer in effect.
You could change the color of your link when you hover on your li-element instead.
#nav li:hover a {
color:white;
}
As for the black line.
You could just add a border bottom to either your li-elements(if you want it to be the full lenght) or your a-elements(if you want it to only be as long as your word)
#nav li:hover
{
border-bottom: 1px solid #000;
}
Edit: This is a sollution for your top menu-item turning black when hovering. Was this your issue or did you want to change the color of your sub-items?
If so you can just do the following
#nav li li a
{
color:white
}
so the submenu should always have white text?
#nav ul li ul a {
color:#ffffff;
}
but i would recommend to do it with classes... so you do not have such large selectors and you can easily use that styling on other pages.
furthermore if you need to change the html tree or instead of using a list perhaps a div it wont work anymore. so go for classes :).
greetings timotheus

Fixing width and other CSS problems in menu navigation

I am coding a very simple CSS navigation menu. I’m trying to stretch the nav to 100% width across the page and set the last menu option to orange background color and white text color to no avail.
Can someone have a look at my CSS code and see where my problem is?
body {
background: #282828;
}
#nav ul {
margin: 0;
padding: 0;
list-style: none;
}
#nav ul li {
margin-right: 5px;
padding: 10px 20px;
position: relative;
height: 20px;
line-height: 20px;
background-color: #282c2b;
color: #fff;
}
#nav > ul > li {
float: left;
height: 30px;
line-height: 30px;
background-color: #282c2b;
border-left: 4px solid #282c2b;
}
#nav li > ul {
visibility: hidden;
position: absolute;
top: 0px;
color: #fff;
}
#nav > ul > li > ul {
width: 100%;
top: 50px;
margin-bottom: 10px;
left: -4px;
}
#nav li:hover {
background-color: #ffffff;
color: #282c2b;
border-left: 4px solid #ff3d00;
}
#nav li:hover > ul {
visibility: visible;
}
#nav ul li .navOrange {
background-color: #ff3d00;
}
Here’s a CodePen
I know it might be hard to achieve this with pure CSS but is it possible to make the menu drop down upon clicking or is it just set to rollover without JavaScript?
so i decided to change it up a little and use elements because it suits me better.
so i now have the following;
http://codepen.io/anon/pen/waKENz
when i add around the div elements it doesnt use the style setup in css, why is it doing this?
and is it possible to perhaps have menu option 4 perform a dropdown on rollover as before or not with elements.
You might have to target each navigation item seperately by setting a percentage width for the item and probably a percentage margin also. Make sure they all add up to 100%.
#nav > ul > li {
margin: 0 1%;
}
.home,
.level-1,
.support,
.sign-up {
width: 18%;
}
.info {
width: 20%;
}
.home {
margin-left: 0;
}
.sign-up {
margin-right: 0;
}
<div id="nav">
<ul>
<li class="home">Home</li>
<li class="info">Information</li>
<li class="level-1">Level 1</li>
<li class="support">Support</li>
<li class="sign-up">SIGN-UP!</li>
</ul>
</div>
Here's a demo jsFiddle (not full code).
The last item in the navigation is not turning orange because the selector is incorrect. You have:
#nav ul li .navOrange { background-color: #ff3d00;}
Which says (working right to left), select any element with the class of .navOrange that is a child of any li that is a child of any ul that is a child of #nav. .navOrange is an not a child of an li but on class on an li and also a child of a ul.
Remove li from the selector and it will work.
#nav ul .navOrange { background-color: #ff3d00;}
About the orange background color: you need to remove the space between "li" and ".navOrange" in the last definition. This will make it more specific than the other definitions and be applied later.
Full width can be achieved relatively simply if you know how many options you'll have in the menu with resizing the buttons to an adequate percentage. Though be careful with this - you generally want something less than 20% with 5 buttons because of the margins etc.
The hover menu that you already have is pure CSS, I don't know of a way to make it onclick without JavaScript.
Please check this code snippet.
body {background: #282828;}
#nav ul{ margin:0; padding:0; list-style:none; }
#nav ul li{ margin-right:5px; padding:10px 20px; position:relative; height:20px; line-height:20px; background-color:#282c2b; color:#fff; }
#nav > ul > li { float: left; height:30px; line-height:30px; background-color:#282c2b; border-left:4px solid #282c2b; }
#nav li > ul{ visibility:hidden; position: absolute; top:70px; color:#fff;
transition: all 0.2s ease-in;
opacity: 0;
}
#nav li.have-item:hover ul{
visibility:visible;
top:50px;
opacity: 1;
}
#nav > ul > li > ul{ width:100%; margin-bottom:10px; left:-4px; }
#nav > ul > li > ul li{
width:100%;
}
#nav li:hover{ background-color:#ffffff; color:#282c2b; border-left:4px solid #ff3d00; }
#nav li:hover > ul{visibility:visible;}
#nav > ul > li:last-child { background-color:#ff3d00 !important; }
<div id="nav">
<ul>
<li>Home</li>
<li>Information</li>
<li>Level 1</li>
<li class="have-item">Support
<ul>
<li>FAQ</li>
<li>Contact</li>
</ul>
</li>
<li class="navOrange">SIGN-UP!</li>
</ul>
</div>

Converting an existing menu into responsive menu

My site navigation menu is coded as follows:
The HTML Code:
<div class="menu">
<ul>
<li class="active">Home</li>
<li class="inactive">About</li>
<li class="inactive">Post Your Ad</li>
<li class="inactive">Tenders</li>
<li class="inactive">Contact Us</li>
<li class="inactive">FAQ</li>
</ul>
</div>
The CSS Code:
.menu {
height: 100px;
z-index: 999;
}
.menu li {
display: inline;
float: left;
width: auto;
}
.menu ul {
margin-top: 2px;
}
.menu a {
text-decoration : none;
font-size: 15px;
color: #ffffff;
}
.menu li:hover {
background-color: #16a085;
}
.active , .inactive {
width: 83%;
}
.active {
background-color: #16a085;
}
.inactive {
background-color: #34495e;
}
/* Navigation Button */
.btn {
-webkit-border-radius: 0;
-moz-border-radius: 0;
border-radius: 0px;
font-size: 22px;
background: #34495e;
padding: 48px 40px 31px 40px;
text-decoration: none;
}
.btn:hover {
background: #16a085;
text-decoration: none;
}
The JSFiddle is at: http://jsfiddle.net/VCKwN/
PROBLEM DEFINITION:
How can i convert the existing menu, keeping its all styles as is, into a responsive menu for a mobile site?
When the screen size reduces, the menu should get hidden, and a button should appear. clicking on the button the menu should reappear.
Is this possible?
You could do something like this - JSFiddle Demo
Javascript
var menu = document.getElementById('menu');
document.getElementById('open-menu').onclick = function() {
menu.classList.toggle('show');
}
CSS:
We hide the mobile menu button initially. And there's a media-query that overrides a few of the menu styles when it's below 480px wide. (Setting the menu items to display block etc;)
.mobile-menu { display:none; }
#media only screen and (max-width: 480px) {
.mobile-menu { display:inline-block; padding:5px; background:#ccc; border:1px solid #000;}
.menu { display:none; }
.menu ul { height:auto;}
.menu ul li,
.menu ul li a { float:none; display:block }
.menu ul li a { padding:15px 6px;}
.show { display:block; }
}
div with width:100%
use the media queries to control the size of the screen
create a button and put it hidden, then with media queries display it and hide the div-menu
take a look at responsive bootstrap menu , so you can make an idea
navbar
navbar-static-top/
navbar-fixed-top/

Adding drop down sub menu to code

I have been trying to add a drop down menu to this code but always seem to get turned around. I just want a basic look to the subnav with a simple rollover effect. Every time i try different code it uses home image in the drop down menu and will not disappear when it is not hovered over. Ideas?
HTML:
<ul class="navbar">
<li class="navbar1">Home
<ul class="subnav">
<li>Menu 1 </li>
<li>Menu 2 </li>
</ul>
</li>
</ul>
CSS:
ul.navbar {
width:1000px;
list-style:none;
height:40px;
}
ul.navbar li {
display:inline;
}
ul.navbar li a {
height:40px;
float:left;
text-indent:-9999px;
}
/* Home 0 */
ul.navbar li.navbar1 a {
width:86px;
background:url(../pelican%20outfitters/navbar2.fw.png)
no-repeat 0 0;
}
ul.navbar li.navbar1 a:hover {
background-position:0 -40px;
}
ul.navbar li.navbar1 a.current {
background-position:0 -80px;
}
HTML
<nav>
<ul class="navbar">
<li class="navbar1">Home
<ul class="subnav">
<li>Menu 1
</li>
<li>Menu 2
</li>
</ul>
</li>
</ul>
</nav>
CSS
nav {
margin: 20px auto;
}
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
}
nav ul li
{
width:100px;
}
ul li ul li
{ width:200px;
}
nav ul {
font-size: 25px;
background: white;
padding: 0px;
border-radius: 10px;
border-style: solid;
list-style: none;
position: relative;
display: inline-table;
}
nav ul:after {
content:"";
clear: both;
display: block;
}
nav ul li {
float: left;
}
nav ul li:hover {
background: black;
position:relative;
z-index:1;
}
nav ul li:hover a {
color: white;
position:relative;
z-index:1;
}
nav ul li a {
display: block;
padding: 15px 20px;
color: black;
text-decoration: none;
}
nav ul ul {
background: #000000;
border-radius: 0px 0px 10px 10px;
padding: 0;
position: absolute;
top: 100%;
}
nav ul ul li {
float: none;
}
nav ul ul li a {
padding: 15px 20px;
}
nav ul ul li a:hover {
background: #2E2E2E;
border-radius: 10px;
}
Output:
Working Fiddle
You should delete the text-indent:-9999px and add this to your css
Delete row:
ul.navbar li a { text-indent:-9999px }
Css:
.navbar li ul {display:none;}
.navbar li:hover ul {display:block;}
Than you have a basic navbar with hidden subnavs.
From here you can try it with your image.
The demo is your code with the new code..
DEMO
More like you want is, dont delete the css.. but only add those 2 lines and this 1:
.navbar li:hover li a{ text-indent:1px; background:white; }
DEMO 2 (without your img (don't know what it is)).
Latest update after the fiddle comment:
You should specify your html and css.. a just added a class to the first link class="home" and to accomodations class="accomodations"
And changed it in the css..
/* Home */
ul.navbar li.navbar1 a.home {
width:86px;
background:url(http://s12.postimg.org/rszejjscd/navbar2_fw.png)
no-repeat 0 0;
}
/* Accomodations */
ul.navbar li.navbar2 a.accomodations {
width:220px;
background: url(http://s12.postimg.org/rszejjscd/navbar2_fw.png) no-repeat -86px 0;
}
DEMO 3