The menu uses the ultra efficient Nekkidblogger menu framework, the same as I used for the horizontal CSS drop menu. It uses an accessible, standard UL (unordered list) classed with classes for the levels of the menu. The CSS is light, simple and very fast.

Here it is. It only uses three "structural" CSS-rules for a multi-level menu (no javascript):

It is the most efficient menu there is: no complex CSS rules with long lists of descendant selectors and a clear separation between presentational and structural elements.

The menu has been tested extensively and works well in all modern browsers: Chrome, Firefox, Internet Explorer from IE 7 and up, as well as Safari and Opera. For IE6 some fixes are required. You can find these, along with the markup and CSS for this menu, in the online demo.

{ 0 comments }

Introduction

Over the last couple of years there has been an increasing focus on Web page speed. Partly this is due to initiatives by Google and Yahoo, partly it is due to the explosive increase in web browsing on devices such as iPads and advanced cell phones (iPhone, Androids, etc).

Gradually, the focus on speed has also resulted in increased attention being devoted to more efficient CSS. Efficient HTML and CSS can help improve rendering speed significantly, not least on mobile devices with slower processors. I have done a lot of work to speed up web sites lately, and in the process I have noticed that menu systems is an area where there is a lot of inefficient CSS floating around. It turns out there is lots of room for improvements.

Markup (HTML)

What we want is a HTML/CSS menu with very efficient CSS. In the example I use a horizontal 4-level dropdown menu.

The markup is basically a standard unordered list (<ul><li>). I have highlighted the non-standard features. It has id="nekkidblogger" and each of the sub-levels (nested lists) have classes describing the level: level2, level3 and level4.

Usually lists used in menus are not styled with classes like this. However, doing it this way has some great advantages: It makes it easy to build a CSS menu with very few style rules; it allows for very efficient CSS; and provides (as a kind of bonus) convenient "hooks" (i.e. the "level" classes") for styling if one wants to style the dropdown levels differently from the top level.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<ul id="nekkidblogger">
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a>
  <ul class="level2">
    <li><a href="#">Link 2-1</a></li>
    <li><a href="#">Link 2-2</a></li>
    <li><a href="#">Link 2-3</a></li>
  </ul></li>
<li><a href="#">Link 3</a>
  <ul class="level2">
    <li><a href="#">Link 3-1</a></li>
    <li><a href="#">Link 3-2</a>
       <ul class="level3">
         <li><a href="#">Link 3-2-1</a></li>
         <li><a href="#">Link 3-2-2</a>
            <ul class="level4">
              <li><a href="#">Link 3-2-2-1</a></li>
              <li><a href="#">Link 3-2-2-2</a></li>
            </ul></li></ul></li>
  </ul></li><li><a href="#">Link 4</a></li>
</ul>

On the right it is shown without styling. This is a standard unordered list. The <ul> for the overall menu has id=”nekkidblogger”. It has four top level links, and under Link 3 there are three dropdown levels. This is more than sufficient for a demonstration – there is no need to fill it up with lots of dropdown elements.

So now we are ready to give it structure and style!

CSS for the Nekkidblogger menu

One of the keys to efficient CSS is to use the "cascade" as much as possible – to allow "child-elements" to inherit as much as possible from "parent" elements. So here I don’t define selectors to the extent that I can avoid it. Also, I try to only style elements once, and then let inheritance take care of as much as possible.

In the following I mark the rules that are structural (makes the menu work as it should) as “Rule” and the presentational style rules as “Style” in order to clearly separate the two.

#nekkidblogger ul {padding:0;margin:0;} (Style 1)

This isn’t really part of the menu, but useful for setting the context. If you use a CSS reset, you don’t need this. Also, you may prefer simply ul, li {padding:0;margin:0;}. I use the more specific reset here (for the "nekkidblogger"-id only) since this is a blog where the general rules have been set for other purposes.

The Top level

#nekkidblogger li { list-style:none; position:relative; float:left;
    text-align:center; margin-right:5px; font: 10px verdana,
    sans-serif; } (Rule 1)

This is the first structural rule of the menu. Here "list-style", "position" and "float" are necessary structural elements, while "text-align" and "margin" are presentational. Note that I style the a-selector, not the <li>. The <li>-element adapts to its content – so we don’t need to define a "width" here for example.

#nekkidblogger a { display: block; width: 8em; color: black;
     line-height: 30px; text-decoration: none; background: silver;
     border:1px solid #fff; margin: 0 -1px -1px 0; } (Style 2)

Here everything is presentational. "display:block" is important in order to display the links as boxes. "width", "height" and the rest is styling. I set the properties of the links here, and then they are inherited by the sub-levels. In your own menu, you may want to style the dropdowns as well.

#nekkidblogger :hover > a { background:#7B7B7B;
         color:yellow } (Style 3)

This is a presentational element only – we don’t need to set different properties for the hover state – but most often one does that in menus to make them look better. Here I style the hovered link as well as the whole hover path instead of just the hovered link (one usually uses simply "a:hover"). I picked up and adapted this neat little trick from the excellent CSS experiments by Stu Nicolls.

What we have done so far (we have also "set the stage" for the lower levels):

Second and lower levels

The rest is purely structural – no presentational elements, as I simply let the lower levels of the menu inherit everything, including the hover state (see Style 3). However, I will show you how you easily can style the lower levels yourself if you want to:

A. Style the boxes

The classes ".level2-.level(n)" provide "hooks" that you can easily use to format the lower levels.

We could, for instance, give a different style to level two and the lower levels – just style ".level2" and let the lower levels inherit. Or we could style each level separately. For instance:

#nekkidblogger .level2 a { background: #FFF; height: 25px; etc .. }

And/or, we could style the lower levels like so:

#nekkidblogger .level3 a { ... }
#nekkidblogger .level4 a { ... }

OK. Back to the nekkidblogger menu:

B. Position the dropdown boxes

The top level is horizontal. We want to drop the second level down vertically from it. We have defined the <li>’s to be relative, so now we position the dropdown (.level2) in relation to them:

.level2 { position:absolute; top:30px; left:0;  }  (Rule 2)

Here "left:0" is default in the good browsers, so we should not need to specify it, but as it for some reason is not so in IE (surprise?), we still must. 30px is the line-height – see Style 2.

The next two levels – level 3 and level 4 – we want to fly out to the right of level 2. 8 em is the width of the a-element (see Style 2). I add .08em (about 1px – the width of the border) for good alignment. So:

.level3,.level4 { position:absolute; top:0; left:8.08em; } (Rule 3)

C. Dropdowns: Hide and make visible when hover

.level2,.level3,.level4 {visibility:hidden} (Rule 4)

We need to hide all levels separately – if we let "hidden"-property be inherited, then the "visible"-property will be too, and all levels would show up when we hovered a given level. Also note that if you want to add another level or two to the menu, all you need is to insert the correct classes in the HTML for the list and then add ".level5, .level6" in Rules 3 and 4. So adding N more levels, using the Nekkidblogger menu framework, does not add to the complexity of the CSS.

#nekkidblogger :hover > ul { visibility:visible; }(Rule 5)

Makes the child elements of a <ul> visible when <li> is hovered (but only the child-elements, not the "grand-children"). And that’s all!

Here is the menu:

Final remarks

So, that’s it. Five "structural" CSS-rules for a multi-level menu (The “Style”-rules are purely presentational). The rules are also comparatively very simple, mostly very short, and computationally effective (good for fast page rendering) with minimum use of descendant selectors.

If you examine the live demo with Google’s Page Speed (using Firefox), and look at the use of efficient selectors, you will find that it has "0 very inefficient rules, 0 inefficient rules, and 2 potentially inefficient uses of :hover". The "potentially inefficient ones are Style 3 and Rule 5, but they are as simple as can be.

If by comparison you run Page Speed on the Son of Suckerfish menu with three dropdowns, which is a very widely used menu and which was published in A List Apart, you will find that it has "7 very inefficient rules, 5 inefficient rules, and 5 potentially inefficient uses of :hover". So, there is a difference. And if Google’s Page Speed is worth anything, and if page rendering speed matters, it may well be a difference that matters.

The Nekkidblogger menu has been extensively tested, and works in all major browsers: Chrome, Firefox, Internet Explorer from IE 7 and up, Safari, and Opera. In the demo I give some more detail and show the fixes necessary to make it work in IE 6.

See Copyright and Terms and Conditions, on demo page, sidebar 1.

PS: In later posts I’ll show how to make the rightmost flyouts fly left and how to convert this horizontal menu into a vertical CSS menu by changing only tiny bits of CSS.

{ 0 comments }

Flexible and robust 3 column CSS layout

by Peter on September 9, 2011

I have just completed a new three column CSS layout designed to be extremely robust and flexible. The major DIVs are build outside in to prevent column overflow, and the layout is designed to degrade very gracefully in narrower viewports. Give it a test by clicking the demo (below is just an image) – see how the header splits and the sidebars fold under one another as you make the viewport narrower.

It’s designed for ads in the header and 25o px wide ads in the middle sidebar. Have fun!

Three column CSS layout

You can get the markup and the CSS by viewing the source of the demo!

{ 0 comments }

Introduction

The Suckerfish menu, first published in A List Apart in 2003, was a major achievement. The authors, Patrick Griffiths and Dan Webb, showed how to build a horizontal dropdown menu with very efficient CSS. It is often referred  to as a CSS-only menu. For browsers that didn’t support the “li:hover” (Internet Explorer – IE – 6, of course), the authors provided a JavaScript that made the menu work even there.

Markup (HTML)

A standard unordered list (<ul><li>). It has class="barmenu".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<ul class="barmenu">
	<li><a href="#">Link 1</a></li>
	<li><a href="#">Link 2</a>
<ul>
	<li><a href="#">Link 2-1</a></li>
	<li><a href="#">Link 2-2</a></li>
	<li><a href="#">Link 2-3</a></li>
</ul>
</li>
	<li><a href="#">Link 3</a>
<ul>
	<li><a href="#">Link 3-1</a></li>
	<li><a href="#">Link 3-2</a></li>
	<li><a href="#">Link 3-3</a></li>
</ul>
</li>
</ul>

CSS (with comments)

The Suckerfish

The logic of the Suckerfish menu has since been used widely to build menus for web pages. The core of it was provided by the authors in a barebones version. The Suckerfish dropdown menu uses the following 5 rules to do the job (I have included their original comments as well):

ul { /* all lists */
    padding: 0; margin: 0; list-style: none; } (Rule S-1)
li { /* all list items */
     float: left; position: relative; width: 10em; }(Rule S-2)
li ul { /* second-level lists */ display: none;
     position: absolute; top: 1em; left: 0; }(Rule S-3)
li>ul { /* to override top and left in browsers other than IE,
  which will position to the top right of the containing li, rather
  than bottom left */ top: auto; left: auto; } (Rule S-4)
li:hover ul, li.over ul { /* lists nested under hovered list items */
     display: block; } (Rule S-5)

That’s it. Five CSS rules for a dropdown. This was a major achievement at the time it was published – neat, simple, orderly rules and very cross-browser compatible. The question is: Can it be improved on now, almost ten years later, with more standards-compatible browsers and with IE 6 more or less out of the picture? As of July 2011 IE6 is used by 2.3% of users. And IE7 is used by a little more than 4%!

Modern CSS dropdown – barebones

In the following rules you can take out the “.barmenu” if you want – I just need to use a “class” here because I have other menus and other unordered lists in this blog, so that I need to use more specific rules.

.barmenu ul, .barmenu li { /* all lists */
	padding: 0; margin: 0; list-style: none; } (Rule 1)
.barmenu li { position:relative; float:left; width: 5em;
    padding:0 0 1px; } (Rule 2)

I usually style the link element rather then the <li>, but since I don’t style I simply give the <li> a width here. This should go out when the menu is styled. The strange padding is explained below (under Rule 4).

.barmenu li ul { position:absolute; visibility:hidden;
      top:20px; left:0; }(Rule 3)

I usually use use “visibility: hidden” and “visibility: visible” to hide and unhide the dropdown. “Visibility” was introduced in CSS2 and is well supported today. I could also have hidden the dropdown outside the viewport, positioning it absolutely at -999em, which today is another standard method. Or, I could have used “display:none”. As it turns out, all the three ways of doing it encounter similar problems in IE7 and IE8.

[click to continue…]

{ 0 comments }

phpBB3 so 1980s – user unfriendly

July 16, 2011

I’ve been looking into forum software lately. I want to set up a couple of discussion forums. It seems to me that the most highly recommended among the free, open source bulletin boards is phpBB, now in version 3 – phpBB3. So I’ve installed it on a test site and have started to work with [...]

Read the full article →

How to Change DOCTYPE in the Thesis 1.8 WordPress Theme

June 22, 2011

Thesis 1.8 uses DOCTYPE 1.0 Strict. This is fine if that’s the right DOCTYPE for the content of your WordPress blog. Otherwise you may want to change it. If you do, there are mainly two options available to you: <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”> <!DOCTYPE HTML> The first is 1.0 Transitional, and [...]

Read the full article →

Doctype in WordPress – How to change DOCTYPE

June 19, 2011

Most WordPress blogs seem to use either 1.0 Strict or 1.0 Transitional. This makes some sense, given that the WordPress engine unfortunately delivers output in XHTML rather than HTML format. Because of this, pure HTML doctypes, such as DOCTYPE 4.01 Transitional, are not good choices for bloggers using WordPress for the moment. However, HTML 5 [...]

Read the full article →

WordPress 3.2 will be lighter and faster

April 1, 2011

In a previous post I wrote that for WordPress 3.1 and beyond I would like to see speed increases – blogs that are meaner and leaner, that load faster. Now it seems I will get what I want. In the WordPress Development Blog, Mark Jaquith writes that the plan for WordPress 3.2 is “faster, lighter”. [...]

Read the full article →