Welcome to Dream.In.Code
Getting Help is Easy!

Join 109,536 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,261 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Pure CSS Dropdown menu

 
Reply to this topicStart new topic

> Pure CSS Dropdown menu

Rating  5
Shadow 1.2
Group Icon



post 12 Feb, 2008 - 08:52 AM
Post #1


So you want to make those cool drop down menus without having to use javascript well heres how.

First open up Notepad or any other text editor(like Notepad++ or Scite). Type in
CODE
#holder {
color: white;
background:blue;
font-size: 20px;
font-family: Arial;
height:20px;
left:32px;
line-height:20px;
overflow:hidden;
position:absolute;
text-align:center;
top:50px;
width:100px;
z-index:100;
}

The above code is ,in short, the design of the button that will always be displayed
Explanation of the code Starts Here
#holder{} this sets the id attribute that will be used in the html. the code goes in between the { and the }
color: white sets the color of the text
background : blue sets the color of the background of the button
font-size Sets the size of the font
font-family : Arial sets the font of the text in and on the menu
left:32px sets the distance from the left side of the page
line-height: 20px the size of the of the line
overflow:hidden hides the menu
position: absolute allow the menu to go over any pictures or text in the page
text-align: center makes the text stay in the center
top: 50px how far the menu is from the top of the screen
width:100pxthe width of the menu
z-index: 100 makes the menu the priority
Code Explanation Ends Here
ok now that we have the menu button we can move onto what shows up under the menu button when we move the mouse over it
CODE
#holder:hover {
background:darkblue none repeat scroll 0%;
cursor:pointer;
height:40px;
}

this is the code to create the drop down part of our drop down menu but it will look messy without the next piece of code
Explanation Of the Code Starts Here
#holder: hover{} this sets the id attribute that will be used in the HTML code and is activated when the mouse goes over the element.The code goes inside the { and the }.
background: darkblue the background of the whole menu when the mouse is over it
cursor: pointer this makes the cursor stay as a cursor and not turn into a hand when the mouse is hovering over it.
height: 40px this is what makes the dropdown box the drop down box, essentially it makes the box drop down
Code Explanation Ends Here
Now to make it look better

CODE
#holder a:visited, #holder a {
color:white;
display:block;
line-height:18px;
text-decoration:none;
width:100%;
}

this code takes away the text decoration such as the underline on hyperlinks and is used to add buttons
Explanation Of Code Starts Here
#holder a:visited, #holder a{}this is the same as the rest except this is for the buttons that are in the menu and is to format them
Color: whitethis sets the color of the buttons text
display: block this moves the text down to the buttons
line-height: 18px sets the height for the buttons
text-decoration: nonethis gets rid of all text decor of the hyperlinks
width:100% Self Explanatory I Guess
End Of Explanation
so now it looks all pretty and is ready to use but what if you want the buttons to change color if you mouse over them
CODE
#holder a:hover {
background:blue;
color: white;
}

Code explanation changes the background and the text when you mouse over them

Now Save The file as "Dropdown.css"

How To Use(how to use in html)

Open a new document in your text editor and type in this code:
HTML
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="holder">
Menu
<a href="#"> Item 1 </a>
</div>
</body>
</html>

Then Save this in the same Folder that you saved your style sheet.
now double click the icon and view itIPB Image
now say you wanted two menus or three menus i havent quite figured out how to do it but i do have one method that might work but would take a bit of work. Just Copy The Code 5 times and then change the #holder to say #holder1 or #holder2 etc. And then just add div tags with those id's
It would look like this
IPB Image
The code
HTML
HTML
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="holder">
Menu
<a href="#"> Item 1 </a>
</div>
<div id="holder1">
Menu
<a href="#"> Item 1 </a>
</div>
</div>
<div id="holder2">
Menu
<a href="#"> Item 1 </a>
</div>
</body>
</html>

CSS
CODE

#holder {
color: white;
background:blue;
font-size: 20px;
font-family: Arial;
height:20px;
left:32px;
line-height:18px;
overflow:hidden;
position:absolute;
text-align:center;
top:50px;
width:100px;
z-index:100;
}
#holder:hover {
background:darkblue
cursor:pointer;
height:40px;
}
#holder a:visited, #holder a {
color:white;
display:block;
line-height:18px;
text-decoration:none;
width:100%
}
#holder a:hover {
background:white
color: blue;
}
#holder1 {
color: white;
background:blue
font-size: 20px;
font-family: Arial;
height:20px;
left:132px;
line-height:18px;
overflow:hidden;
position:absolute;
text-align:center;
top:50px;
width:100px;
z-index:100;
}
#holder1:hover {
background:darkblue
cursor:pointer;
height:40px;
}
#holder1 a:visited, #holder1 a {
color:white;
display:block;
line-height:18px;
text-decoration:none;
width:100%;
}
#holder1 a:hover {
background:white
color: blue;
}




#holder2 {
color: white;
background:blue
font-size: 20px;
font-family: Arial;
height:20px;
left:232px;
line-height:18px;
overflow:hidden;
position:absolute;
text-align:center;
top:50px;
width:100px;
z-index:100;
}
#holder2:hover {
background:darkblue
cursor:pointer;
height:40px;
}
#holder2 a:visited, #holder2 a {
color:white;
display:block;
line-height:18px;
text-decoration:none;
width:100%;
}
#holder2 a:hover {
background:white
color: blue;
}


if you want to add more buttons to the menu just increase the Height property by 20 or whatever the line-height property is for each extra button.

Try editing the code and add images to the background of the buttons if you want
if you want to email me. send it to[email] josh@amazingafrica.co.za[/email]

I Hope This Taught Some Beginners Of Css some things and everyone else something aswell cool.gif



Attached File(s)
Attached File  CSSExample.zip ( 689bytes ) Number of downloads: 184
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

hervens
*



post 18 Feb, 2008 - 10:55 AM
Post #2
Wow, interesting
Go to the top of the page
+Quote Post

Shadow 1.2
Group Icon



post 20 Feb, 2008 - 07:43 AM
Post #3
I Forgot To Add That this wont work in IE because it doesnt support :hover as far as i know. One more reason for everyone to have firefox
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 9/7/08 09:42PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month