Nov 8, 2009

CSS Rollover Menu Code - for < a > tags used on HTML pages

free web hosting
Open Discussion > MODERATED AREA > Computers > Programming Languages > CSS (Cascading Style Sheets)

CSS Rollover Menu Code - for < a > tags used on HTML pages

jlhaslip
QUOTE(moldboy @ Nov 18 2005, 03:23 PM)
...  I am wondering if you have a particular site in mind that would show how?
*



Hate to add this to a topic about unordered lists, but here goes. Maybe this part will get split into its own topic later. I'll ask another mod how to do that. ( new guy comment)

My Webpage, or click on the bottom link of my signature to go to the same site which has css rollover effects.
This isn't the exact code that performs the rollover on that site, but is from another (handier to acquire) html page on my computer.

CODE
a:link, a:visited {
 background-color: #ffffff;
 color: #66ff66;
 font-weight: bold;
 }
a:hover {
 background-color: #66ff66;
 color: #ffffff;
 }


Notice that the back-ground-color and the foreground colours get switched on rollover?
It is that simple. Also, the font-weight is inherited by the on-hover state, because I don't specify any change in font-weight. The "cascade" does that for you.

Hope this helps

 

 

 


Comment/Reply (w/o sign-up)

moldboy
QUOTE
Hate to add this to a topic about unordered lists, but here goes.

That's what I was thinking when I wrote the post but, oh well. The code you just demonstrated would give a rollover link(I think), I was thinking something more along the lines of a menu. The kind that drops down on rollover

Comment/Reply (w/o sign-up)

Tyssen
If you want a good site with tutorials on how to create horizontal and vertical menus using lists, check out Listamatic.

Comment/Reply (w/o sign-up)

Lozbo
There is a simple rule that will get your list done as you want (a drop down on roll over).

It is the simplest code, its as simple you would never have thought it! It is really cool, the only problem with it (yeah, it has a little problem, but as you must be figuring it out, its all about that hated non-standars-compliant nasty browser which happens to be, by the way, the most used browser around hehe).

But dont panic, it all solves with a little javascript (lets hope users allow js do the magic).

Well the trick is basically about this: you do your xhtml markup of lists, and sublists, so you will have something like this (ill borrow the code of a page im developing):

HTML
<ul id="menu_nav">
<li><a href="/pmic/presentacion.php" accesskey="N">Presentaci&oacute;n</a>
<ul>

<li><a href="/pmic/misionvision.php">Misi&oacute;n y Visi&oacute;n</a></li>
<li><a href="/pmic/objetivos.php">Objetivos</a></li>
<li><a href="/pmic/metas.php">Metas</a></li>
</ul>
</li>
<li><a href="/pmic/pmic/" accesskey="P">PMIC</a>

<ul>
<li><a href="javascript:;">Materiales</a></li>
<li><a href="javascript:;">Chapala</a></li>
<li><a href="javascript:;">Arcediano</a></li>
<li><a href="javascript:;">Humor</a></li>
<li><a href="javascript:;">Fotos</a></li>

<li><a href="javascript:;">Notichapala</a></li>
<li><a href="javascript:;">Staff</a></li>
<li><a href="javascript:;">Eventos y Actividades</a></li>
<li><a href="javascript:;">P&aacute;ginas Relevantes </a></li>
</ul>
</li>

<li><a href="/pmic/excit.php" accesskey="E">Excit</a></li>
<li><a href="/pmic/claseEA/" accesskey="C">Clase de Econom&iacute;a Ambiental </a>
<ul>
<li><a href="javascript:;">Programa Acad&eacute;mico</a></li>
<li><a href="javascript:;">Pizarra de Mensajes</a></li>
<li><a href="javascript:;">Materiales de Lectura </a></li>

</ul>
</li>
<li><a href="/pmic/investigadores/" accesskey="R">Red de Investigadores</a>
<ul>
<li><a href="javascript:;">Investigadores</a></li>
<li><a href="javascript:;">Proyectos</a></li>
</ul>

</li>
<li><a href="/pmic/publicaciones.php" accesskey="U">Publicaciones</a></li>
</ul>


And the glorious, yet as simple as hell css is all about this:

hide the sub lists with display: none and show it again with :hover. However, the problem with IE consists that the pseudo class :hover is only supported for anchors, and what we are using is a li:hover pseudo class so will have to fix it with another code.

the basic code will be something like this:

li ul {display:none}
li:hover ul {display:block}

That's it. You tell the ul child of the list item to keep hidden, and then you tell that ul (on the rollover of the li) to show. A more fancy css code (whill i'll also borrow from my other site) will be like this:

CODE

div#main_menu   {width:200px;background:#0b3a6a;color:white;font-size:.9em;float:left;text-align:left;clear:both;}
div#main_menu p   {margin:0;padding:10px;font-size:1em;background:#74a7cd;text-align:center;}
div#main_menu ul  {margin: 0;padding: 0;list-style:none;width: 200px;vertical-align:top;}
div#main_menu ul li  {position: relative;}
div#main_menu li ul  {position:absolute;left:200px;top:0;display: none;}/* quitar absolute para otro tipo de men� */
div#main_menu ul li a {display: block;text-decoration: none;background:#0b3a6a;color:white;padding:5px 0 5px 5px;
       /*border: 1px solid #ccc;border-bottom: 0;*/}
div#main_menu ul li a:hover{color:white;background:#11589F;border-right:5px solid #BF0D0D;padding-left:10px;}
/* Fix IE. Hide from IE Mac \*/
* html #main_menu ul li  {float: left; height:1%;}
* html #main_menu ul li a  {height: 1%;}
/* End */
div#main_menu li:hover ul, #main_menu li.sfhover ul  {display: block;}
div#main_menu li ul li a     {padding: 2px 5px;border-bottom:1px solid #28516F;}



That's it, it comes with IE fix hacks and everything hehe. Note that the markup the main ul had an id? its for the js fix. You can change it but youll have to do it in the javascript.

The code i posted, the css, its an already formatted horizontal menu with dropdowns on the right, got it from a tutorial on alistapart and its valid xhtml and css. This is the IE JS FIX:

CODE

sfHover = function() {
var sfEls = document.getElementById("menu_nav").getElementsByTagName("LI");
for (var i=0; i<sfEls.length; i++) {
 sfEls[i].onmouseover=function() {
  this.className+=" sfhover";
 }
 sfEls[i].onmouseout=function() {
  this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
 }
}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);


Hope this worx for ya.

 

 

 


Comment/Reply (w/o sign-up)

Tyssen
You've gone one step further there Lozbo in that you're talking about list menus with dropdown list menus. Of course you can create a list menu without it having an attached dropdown.
As for the javascript for IE thing, there is an alternative: whatever:hover which does for :hover in IE what all other modern browsers do by default.

Comment/Reply (w/o sign-up)

Lozbo
QUOTE(moldboy @ Nov 18 2005, 05:21 PM)
That's what I was thinking when I wrote the post but, oh well.  The code you just demonstrated would give a rollover link(I think), I was thinking something more along the lines of a menu.  The kind that drops down on rollover
*


Yeah thanks for that page tyssen! i have not read it all but i think that will make :hover life easier right? and i think moldboy wanted to know a little bit about dropdowns...

Comment/Reply (w/o sign-up)



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : css, rollover, menu, code, tags, html, pages

  1. Fixed Centred Layout
    Learn how to code that popular Centred/Fixed width layout... (2)
  2. A Little Help With This Css Menu..
    (2)
    i dont have any idea about css but tried meesing around. here is the code . please tell me how can i
    make the background appear and how to get the menu in full length and buttons in centre. i got this
    frm somewhere and messed it up slightly /* menu*/ #bottom_div{ width: 520px; height: 150px;
    background-color: #b9d1ea; clear:both; } #vista_toolbar { font:normal 12px 'Trebuchet
    MS','Arial'; margin:0 auto; padding:0 auto; } #vista_toolbar ul {
    background-image:url(images/back.gif)center center repeat-x; /*THEME CHANGE HERE*/ width: 100%;
    line-height:32px....
  3. How Often Do You Use Css?
    As opposed to using simple <font> and <div> tags (20)
    CSS is basically made to make your html coding a heck of a lot easier. However, sometimes I prefer
    to use html instead of CSS since it sometimes gets frustrating to refer to the same old style sheet
    and use an old font ID you made about a week ago. I find it a lot more efficient to just type out
    the specified font color/style/size in the tag than to use an individual or for certain fonts
    you'd like to use. Sure, it's very helpful a lot of the time to use CSS in most cases, since
    it's kind of like a very simplified version of PHP but in a different case,....
  4. Problem With Css Code
    (3)
    Hello, I have a css file that i made for the my forum as a skin and for some reason in Opera
    browser it dose not work right. what it does is it dose not bring in one of the images. and i just
    installed mkportal and made it to where the forum shows in the portal and now it dose not work that
    way either. Here is a picture of what it does in Opera: and here is what is show in Internet
    Explore and what it is suppot to look like: the id for the title bar in the css file is
    "maintitle" here is the css code: CODE html { overflow-x: auto; } BODY { background-c....
  5. Adding Accessibility Features To Your Pages
    Using the :active/:focus pseudo-class (0)
    Here is a tip for its use on your site. Using CSS to tell your visitors where they are on your site
    can be fairly complicated depending on the method you use. The CSS Specifications have defined a
    "pseudo-class" for the link which currently has 'focus' and another pseudo-class for the
    'active' state. They can be used to tell your visitors where their cursor is on the page
    and you do that be altering the CSS for the link which currently has the focus and those which are
    being activated. The ' active' state only occurs momentarily while the ....
  6. How Do You Make A Dropdown Menu?
    with css (13)
    Hi, i want to know how to make a horizontal drop down menu with css? i am not talking about the ones
    that have like an arrow where you click for options.. but when you hover over the buttons, other
    buttons drop down below..i have seen some tutorials but i couldn't understand how they did it..
    so if anybody knows how to make one and if you can put it here or if you have a link where i can
    find some.. that would be great.. Thanks....
  7. Css Menu -- A Little Help Needed
    i want a cool menu (5)
    ok, basicly i learned on trap17 that tables are bad for webdesign beacouse of their slow loading and
    incompability with browsers. since i have some html, css and even php skills (very little indeed but
    i have the will) i'm building this new website for local alternative club. i have enough time to
    build it, so it means i can practice on those skills.. i basicly know how to build css website (and
    i have a good book about it /smile.gif" style="vertical-align:middle" emoid=":)" border="0"
    alt="smile.gif" /> ) so i wont bother you much with all of it, just this menu i ....
  8. Css Dropdown Menu
    (10)
    hi eveyone for my new site i want to create a css dropdown menu the image has what the menu needs
    to look like. but beneth all the links when you need to click on them another set needs to dropdown
    how can i achieve this. ....
  9. Help With A Css Design
    code optimization (8)
    hello there!, i need somebody tell me how to put a scrollbar into my divs or tables, and how to
    delete the h3 cellpadding or how to bold a text with css. thanks a lot! /blink.gif' border='0'
    style='vertical-align:middle' alt='blink.gif' /> ....

    1. Looking for css, rollover, menu, code, tags, html, pages

Searching Video's for css, rollover, menu, code, tags, html, pages
See Also,
advertisement


CSS Rollover Menu Code - for < a > tags used on HTML pages

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com