Skip to page content or skip to Accesskey List.
Search evolt.org
evolt.org login: or register

Work

Main Page Content

Absolute Lists: Alternatives to Divs

Rated 2.85 (Ratings: 5) (Add your rating)

Log in to add a comment
(8 comments so far)

Want more?

 
Picture of patcoll

Patrick Collins

Member info | Full bio

User since: January 21, 2004

Last login: January 14, 2010

Articles written: 1

I am a list person. As far back as when my own wallet started directing my spending habits, I was making lists that instructed me "what to-do," "who to call," and "who to pay back exorbitant amounts of money." Lists make the most sense when trying to organize a life that needs it. The same ethos applies to the web design world.

Drop That Div!

As we already know, lists can be tamed through various CSS techniques. These lists are most commonly contained within div tags. This article discusses how to get rid of div elements altogether, and step into a world of pure semantics utilizing only lists for an entire page.

I'll start with an example:

Figure 1A

<div id="links">
	<h2>links</h2>
	<ul>
		<li></li>
		<li></li>
	</ul>
</div>

Figure 1B

<ul id="links">
	<li class="header"><h2>links</h2></li>
	<li></li>
	<li></li>
</ul>

In Figure 1A, the div is styled and positioned uniquely according to its id. The difference between the two is miniscule, but the benefits of directly styling lists, as in Figure 1B, prove to be useful and expandable. Identical visual effects can be created using CSS with various margin and padding edits, as well as other uncomplicated CSS tricks. Styling our ideal list (Figure 1B) will be easy once we get the structure down.

In Figure 1A, the header is outside the unordered list. If you wanted to float the list to the right using the float:right CSS property, the header would not budge. The real advantage of putting the h2 element inside the list as the first item is being able to position the list anywhere you want on the page, taking the header where the list goes.

The Content

So let's practice what we preach. Check out Example 1. We give each first-level unordered list its own unique id, so they can be styled separately later on. You can see that any header within a list has a disc beside it, telling you it is indeed the first item in the list.

In the main section, we daringly put more than links in the list items: we go so far as to put another list, this time with more extended content. In the second nested list, we semantically separate each list item with its respective header and paragraph, giving true division among the different headings and concepts in the page. Visions of school notes are now flashing in my head. Call Mom... Wait, no— pay Mom $50... Hold on...

Ok, back. Of course we know the true spirit of CSS is the separation of style from content. Now that we have the content, let's style it. Shall we?

The Style

As we can see, the default styling for unordered lists is quite boring. No offense, Sir Tim. Plus, we have some unsightly discs (the default style-type) lying about that we do not want in the final product. So let's start there.

First, we begin with the designated 'header' list items in the document:

ul li.header {
	margin: 0;
	padding: 0;
	list-style-type: none;
	list-style-position: outside;
}

Or the shorthand:

ul li.header {
	margin: 0;
	padding: 0;
	list-style: none outside;
}

Note: a CSS property of ul li:first-child could be used here to style the first item in every list, but it becomes universal. Be more specific with ul#main li:first-child, etc. Unfortunately, this valid CSS is not supported in one of the most-used browsers on the Internet.

Even with the list-style-type:none property, browsers still put an indent where the disc would have been, so we put the invisible style-type outside the margin of the list with list-style-position:outside. With help from the margin and padding properties being set to zero, the style-type disappears completely.

Standards alert: Though the W3C does not recommend directly styling li elements, it is unavoidable in this circumstance unless you want to get rid of the style-types in the list altogether.

Secondly, we continue by getting rid of discs in the main section:

ul#main { list-style: none outside }

This gets rid of the disc beside "Main content," and the seemingly random one right below it, designating the start of the nested list, used for the actual content. Further styling will enhance your list page even more, which we will see in the final example.

Now the fun part begins. Three consecutive lists on a page are not appealing in and of themselves. Though we have styled them as we want them, we can see that they could benefit from a bit of CSS positioning.

Position It!

Positioning is the real name of the game in CSS. Linear, non-styled content on a web page does not make the grade for professionally developed sites. Besides, we can do better than that. In our final steps, we see what can be accomplished with CSS positioning.

ul#main {
	margin: 0 200px 0 25px;
	background: #f99;
	padding: 10px;
	list-style: none outside;
}

The margin and padding properties read, left to right, as top, right, bottom, left (think TRouBLe, without those pesky vowels). Thus the main list is given 25 pixels of margin space to its left, and 200 pixels to its right. Padding is ten pixels all around. What to put in the 200px of blank space? You guessed it: the other lists.

ul#navigation, ul#links {
	float: right;
	clear: right;
	width: 165px;
	background: #99f;
	margin: 0;
	padding: 5px;
	list-style: inside square;
}

As we want both lists to be aligned vertically, one atop another, we use the float:right and clear:right properties. The links list will clear the floated navigation list, putting it beneath the latter.

The total width of the side lists, true to the CSS1 Box Model, will be 175 pixels (165px width + 5px left padding + 5px right padding). This gives the main content an equal 25 pixels of margin on either side, making it appear to float on the screen.

You can see the results in our final example. As with any CSS, style and position to your liking.

Notes & Conclusion

I juggled between the advantages and disadvantages of having nested h2 tags within the 'header' list items, and decided to keep them in for a simple reason: Without any CSS styling whatsoever, the headers look as they should. Without the h2 tags and unstyled, the headers have the same font size as the content below them. This is not how the header text should be interpreted. Thus the header tags stay.

As always, browsers with limited or no support of CSS will still display all information on the page in a traditional unordered list format, with no loss of structural semantics.

Using only lists for formatting an entire site can be extended to ordered lists (ol) and definition lists (dl), each with their own uses in their own respective contexts.

Creating an entire page of lists shows the power that presentation with CSS brings to the structure of a hypertext document. Having li elements represent distinctly separate items provides more division among what is being presented. This proves useful when displaying items with extended metadata in list form, such as: dates in concert schedules, items in an online store, even entries in online weblogs.

References

In attractive list format.

i dunno, i think i'll pass

Submitted by r937 on January 27, 2004 - 06:27.

the two sentences in "Positioning is the real name of the game in CSS. Linear, non-styled content on a web page does not make the grade for professionally developed sites" are as logically related as "a woman needs a fish like a bicycle needs a radish"

positioning is just one way to achieve professional results, and any non-styled content will not make the grade

what is really troubling is your use of pixels (shock! awe!) to define the width of the lists -- i mean, come on, get with the program

login or register to post comments

You're periously close

Submitted by slholmes on January 27, 2004 - 10:16.

to subsituting table layout for list layout. I agree with you, some what, that lists might be a more appropriate semantic approach than other approaches but it seems to me that the whole point behind an H2 element is to mark a block of text as a heading. By including it inside a list item blurs that semantic definition. I'm using the definition list in a similar fashion as your article proposes. The definition term element DT serves as your heading and multiple definition elements DD serve as the content (in your example, the remaining LI). I have semantic problems with my own approach but it seems that if the idea is to clue-in a machine as to your semantic meaning, that the definition of a term is closer than a list of non-list-items. Also, given the classic heading followed by a list as in your example 1A, a Div is appropriate for layout. Semantically, you are saying that the document is divided here to enhance the meaning of the section. Presentationally, the div is used for positioning - and CSS on Divs work fairly well in many browsers. Positioning on non-divs can be problematic for earlier browsers like Netscape 4.

login or register to post comments

A more semantic approach

Submitted by EarthnCirciuts on February 3, 2004 - 19:41.

I use a much more semantic approach to styling my pages.

Most of my pages have 2 or 3 main areas: header, navigation, and content. I do not use lists to style any of these, because, in all honesty, they are not lists.

The header, navigation, and content are areas of the page. Thus, I make them <div>s. Not surprisingly, I give them the ids "header", "navigation", and "content", respectively. I usually place the content <div> first, then the navigation, then the header. Don't wory, I use CSS to place them in the layout.

Because the content of the page appears first, it gets weighted most relevant in search engines.

So, the <body> of my page, without any content looks like this:

<body>
	<div id="content" ></div>
	<div id="navigation" ></div>
	<div id="header" ></div>
</body>

And I have an external Stylesheet something like this:

id#content {
	display: block;
	position: abolute;
	top: 4em;
	left: 20em;
}
id#navigation {
	display: block;
	position: absolute;
	top: 4em;
	left: 0px;
	width: 20em;
}
id#header {
	display: block;
	position: absolute;
	top: 0px;
	left: 0px;
	width: 100%;
	height: 4em;
}

Now, the part of this that is relevant to this article is the contents of the navigation <div>. Usually it has two things: section headings, and links. This is not really a list of things, but a collection. It looks like this:

<h3>main</h3>
<a href="/">Home&lt/a>
<a href="/dev/urandom">Privacy Policy&lt/a>
<a href="/xml/about.html">Web Services & RSS &lt/a>

<h3>annother topic</h3>
<a href="/content/blah.html">Blah Blah Blah&lt/a>
<a href="/actions/doit.php">A Script&lt/a>
<a href="/media/foo.mpg">A Video&lt/a>
<a href="/media/foo.pdf" type="application/pdf">A PDF Document&lt/a>

<h3>references</h3>
<a href="http://www.w3.org/TR/2002/REC-xhtml1-20020801">XHTML&lt/a>
<a href="http://www.w3.org/TR/1998/REC-CSS2-19980512">CSS 2&lt/a>
<a href="http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113">DOM 2 Core&lt/a>



<h3>links</h3>
<a href="http://shoultexist.org">ShouldExist&lt/a>
<a href="http://slashdot.org">/.&lt/a>
<a href="http://evolt.org">evolt&lt/a>
<a href="http://alistapart.com">A List Apart&lt/a>


Notice that there are no <br /> tags, images, tables, nor anything else. I replace these with an entry like this in the attached stylesheet:

div#navigation a {
	padding-left: 3em;
	padding-bottom: 2em;

}

I'd also like to add this:

div#navigation a[type="application/pdf"]:after {
	content:  url("/media/pdf-icon.gif")

}

But I'm not sure which browsers support it (I've only tested it in Mozilla 1.5).

So, what does it look like?

To users of modern browsers, it looks like it should... all laid out nice and neat.

To Google (perhaps one of the most important site visitors) it looks like it should... the site contents, followed by links to other articles on the same site.

To aural browsers, it sounds like it should... the site contents, followed by the navigation, followed by the (irrelevant) branding.

login or register to post comments

Pure Thoughts

Submitted by slholmes on February 4, 2004 - 06:07.

It sounds like you're having "pure" thoughts related to this, so I'll point out that based on my studies of the xhtml spec, the semantic way of designating the navigational elements of a web site is to the use the link element. The w3c describes this element thusly: "The Link Module defines an element that can be used to define links to external resources. These resources are often used to augment the user agent's ability to process the associated XHTML document." Having experimented with this, I found that Mozilla works rather well. A bit of digging in the HTML 4.0 spec is in order here for examples and things. Until, IE provides a mechanism to display the "links" of a page, we're SOL. Also, the w3c did not seem to provide for the stylization of users agent's rendering of the links so you're stuck as far as presentation goes. This is not a bad thing on careful reflection, me thinks.

login or register to post comments

Browser &lt;link&gt; rendering

Submitted by tupholme on February 11, 2004 - 15:47.

Agreed, slholmes. One can't style the Back button and it's the most used control around. Admittedly it's also the simplest control, but would it be so well used if it weren't so clear and consistent?

login or register to post comments

What about printing

Submitted by zde on May 12, 2004 - 08:48.

Nice idea. Unfortunatly, this layout vanishes when printing (at least in Mozilla 1.6).

login or register to post comments

temp ?

Submitted by shehsuvar on March 13, 2007 - 22:54.

hi,this is very very nice , but i dont know anything about CSS and JS. is there a template for this ? please help . if you need e-mail shehsuvar@hotmail.com

login or register to post comments

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.orgEvolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.