Industry guru Dave Taylor answers free tech support questions about a wide variety of business and technical topics, including blogging, Google AdSense, MySpace, Sony PSP, Apple iPod, Mp3 players, management, Linux, SEO, Mac OS X, Facebook, Twitter, LinkedIn and Microsoft Windows.

How do I have a shared CSS menu system?

I love CSS. I would like to use CSS menus. I have seen several versions on the netover the last 12months (I currently use a javascript menu system). My biggest problem with these CSS menus is that you have to put the menu code into each HTML page as far as I can see). This means a menu change for a site must be updated in every page.

This seems to go against the idea that CSS can be used to change the look and feel of an entire web site by just chnaging the external style sheet. Do you know of a way of putting CSS menus information in an external file that can be linked or imported into the HTML?


Dave's Answer:

Sure, this is actually pretty darn easy! Just look at the <link> tag.

Or work by example: use View Source in your Web browser to view the HTML source to this very page and you'll see the following on about line six or so:

<link rel="stylesheet" href="http://www.askdavetaylor.com/styles-site.css" type="text/css" />

By adding that to all my HTML pages I have a single shared CSS file that can be as complex as I need, without any issues of having to edit multiple pages to make a stylistic or presentation tweak.

If you need to include HTML and/or JavaScript too, you might instead look into server-side includes. I use those extensively throughout my Web sites too:

<!--#include file="javascript-menu.html"-->

These let me have CSS, JavaScript, HTML, whatever else I need, neatly compartmentalized and easily edited. The best feature? That it's just a single include line you add to each page of my site.

Finally, I spend a lot of time in my book Creating Cool Web Sites talking about JavaScript and CSS-based menu systems. It turns out that compatibility across a wide range of web browsers is darn hard and if you choose to build your own, you might well be setting yourself up for a never-ending litany of complaints from visitors whose browsers don't quite work properly.

Instead, I've actually moved to simpler, less fancy site layouts in the interest of being compatible with the widest possible range of visitor applications, whether cell phones, wireless PDAs, Sony PSP users, or, now and then, an actual computer user. :-)

Hope this helps you out!



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

Have you tried PHP includes?
My CSS file is one file, imported into each page.
And ditto, my menu is a PHP file, included into each page.
(There is the small Javascript IE fix, also included into each page, but not necessary for Firefox et al).
So when I want to add new pages to my menu, I just edit my menu.php, and the lovely two-level CSS menu appears magically on each page.
I almost always use this technique now.
So my pages end in .php instead of .htm.
Good luck.

Posted by: chris at September 14, 2005 11:10 AM

The question was: "Do you know of a way of putting CSS menus information in an external file that can be linked or imported into the HTML."

The answer you provided, while good information about linking to the CSS style sheet, doesn't address the question. Rephrased, the writer wanted to know if he could write CSS Menu code in a separate file, and then link that menu (NOT the stylesheet) into the html code for his other pages.

Posted by: Ed Newbold at September 14, 2005 12:06 PM

Ed, using the PHP include as suggested by another commenter, or using the SSI include allows you to have arbitrarily complex material -- including a complete CSS menu solution -- neatly packaged into a separate file.

Posted by: Dave Taylor at September 14, 2005 1:18 PM

Hi,

Is there any way to INCLUDE a menu file into HTML. I have no experience with PHP.

Something akin to:

{.div class="Center_Box"}
{.link href="Links.Html"} // load the page
{./div}

So I only have to maintain one file for all my pages. I could find no references to anything like that in Cool Web Sites.

Thanks,

Swede

Posted by: Gösta H. Lovgren at October 10, 2005 3:04 AM

You certainly can, Gosta. Go back to my book and read up on Server Side Includes (or Google the phrase, for that matter).

Posted by: Dave Taylor at October 11, 2005 7:08 AM

Works great Dave.

Thanks.

Posted by: Gösta H. Lovgren at October 11, 2005 4:56 PM

Great info about css stylesheets Dave. But, I'm still unanswered about how to put a menu into a stylesheet. I tried it and tried the javascript menu. I wish you'd write more on this topic.

Posted by: Sara at May 31, 2006 12:17 PM

I'm having the same problem I beleive. I want to include one menu on multiple pages that are in multiple directories, but when I open the page, and click on the menu it follows a path to the original directory that the menu was created in.

Posted by: Jeff at February 27, 2008 4:42 PM

I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

I do have a comment, now that you mention it!









Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Uniblue: Free Virus Scan

Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2008 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]