EzPortal - Portal Software for Forums

EzPortal - Made to Fit Your SMF Forum - Glad You Found Us!
+- +-


Welcome, Guest.
Please login or register.
Forgot your password?

+- Recent Posts

Minor Update for SMF 2.1.x by EzPortal
October 18, 2020, 09:33:09 am

Re: EzPortal unable to be seen by user with primary custom usergroup by EzPortal
October 12, 2020, 07:55:08 am

EzPortal unable to be seen by user with primary custom usergroup by radi27
October 11, 2020, 04:46:32 pm

Show Likes mod by NickT
July 10, 2020, 12:09:44 pm

Re: ezPortal inbuilt module downloaders not working. by EzPortal
March 26, 2020, 08:19:48 am

Re: ezPortal inbuilt module downloaders not working. by Valdivia
March 26, 2020, 08:16:33 am

Re: ezPortal inbuilt module downloaders not working. by EzPortal
February 12, 2020, 10:01:42 pm

Re: ezPortal inbuilt module downloaders not working. by usalabs
February 12, 2020, 05:59:54 pm

+- HTML Menu

Sample HTML Block Usage - You May Custom Code it, as needed!

Author Topic: CSS Lists: Part 1  (Read 6089 times)

0 Members and 1 Guest are viewing this topic.

Offline Maxx

  • VN Vet E4
  • Hero Member
  • *****
  • Posts: 560
  • Karma: 8
  • Gender: Male
  • Web Designer
    • SurfaceThemes - Web Design Ideas Classic Rock
CSS Lists: Part 1
« on: May 08, 2010, 02:26:15 pm »
CSS Lists: Part 1

Note: this just code so you will only see the results in the browser after you have apply to your Page!

Lists have been used as an active navigational element for a while now, by using CSS to style them in a fashion that makes them recognizable as part of the page's navigation. They weren't originally intended for this, however. The more you look back at HTML specifications, the more it's obvious that their purpose was as a visual, documentational aid. Indeed, those of you who are veterans to the web will remember the very old pages characterized by the left-aligned black text on a white background, the ubiquitous Times New Roman font, the very long pages with many anchors, and the black circle bullet point lists. Actually, the HTML part didn't change much since those times as far as lists are concerned, apart from the class attribute.

We'll start with just such a list:

    * List Item 1
    * List Item 2
    * List Item 3

The HTML used for it is:

<div class="list_container">
<li>List Item 1</li>
<li>List Item 2</li>
<li>List Item 3</li>

I opted not to style the list ul or il elements themselves, but rather encase the list in a DIV so that we'll be able to address the different elements within it in a simple manner. This isn't really necessary -- you can often achieve exactly what you need by styling the ul element and addressing the elements under it, instead. Remember though, that the element that you wrap the list around needs to be a DIV, rather than a SPAN, since strict XHTML won't validate an Inline element having a Block child (you could, of course, add display: block; to its class, by why go that far when DIV is ready and waiting to serve?). Since I'm going to be referring to elements of the Box Model frequentry throughout this document, you may want to check out the Box Model article to get better acquainted with how CSS tells the browser to draw elements onto the display device.

We'll start by taking the reigns and assigning values to several well-known properties:

.list_container ul
margin-left: 0;
padding-left: 0;
list-style-type: none;
font-size: 14px;
line-height: 17px;
font-family: Trebuchet MS, sans-serif, Arial, Helvetica;

The first thing we do is eliminate the padding and margin to the left of the text. We may want to recover that later, but for now we're simplifying the list. We also remove the bulletpoints completely by assigning none to the list-style-type property. Lastly, we assign font and line-height properties. You may be wondering why bother with line-height? This is a technicality that you'll be able to experiment with later -- If you just set a font size, sometimes the list il items will distance themselves from each other in Internet Explorer, creating what looks like a margin, even if you specify the li's margin property to 0. There is a similar problem with horizonal lists, however the solution to that problem is different. The problem often shows itself when the line-height is too small compared to the font size, though your best bet is to simply check your design in Internet Explorer and see how it renders. Also keep in mind that while the UL element is a Block element, the LI element is of type list-item, which means that the line-height property governs its height, rather than the height property. In fact, if you were to replace the line-height property with the height property of the same value, you'll get some amusing results, which will vary from one browser to another.

Here's what our list looks like now:

    * List Item 1
    * List Item 2
    * List Item 3

The list has been simplified to simply lines at this point, though of course lists have inherent properties that mere HTML lines do not. Before we keep adding properties to the list, let's first turn the list items into links. We do this as easily as it sounds, by encasing them in a elements:

<div class="list_container">
<li><a href="#">List Item 1</a>
</li> <li><a href="#">List Item 2</a>
</li> <li><a href="#">List Item 3</a>
</li> </ul>

Now that we have links on our list, we should style them as well:

.list_container a
display: block;
padding: 1px;
padding-left: 10px;
width: 200px;
background-color: #FFF;
border-bottom: 1px solid #DDF;
.list_container a:link, .list_container a:visited
{ color: #369; text-decoration: none; }
.list_container a:hover { background-color: #369; color: #fff;

Before going over the changes, let's view them first:


    * List Item 1
    * List Item 2
    * List Item 3

Suddenly it no longer looks like a "documentation aid" anymore... All that's changed is that the items were made into links, and the links were styled the same way you would normally use CSS to style any other link. Actually that's not entirely accurate: a very important property has been added - display: block;. This transformed the a elements from Inline elements into into Block elements, similar to how DIVs and Ps are. The block takes up whatever space it can, horizontally, within the il area. This way, the structure of the list is taken advantage of, and you can add borders and padding that apply to all of the links in the same manner. This is also helpful when designing mouse-over effects, of course, since the cursor will be detected all over the link area, not just the text portion.

Notice that the only elements that were specified a width were the a elements. In other words, at the very least, the encasing DIV is taking up all the horizontal space that it can. Normally, when encasing a list with a DIV, you would need to specify a width for the a elements, and for the DIV itself. You can't leave the width of the a elements empty, or the il elements will react badly in different manners depending on the browser. You also shouldn't set the a width to 100%, since this will conjure its own set of problems, even if you've set the DIV's width to a specific amount of pixels. This may seem troublesome, but there's really nothing preventing you from setting both width properties to the same pixel value, which is the recommended route.

Let's not write off the list markers just yet. Remember that it's always nice to get more graphical value out of less code, when possible (and when appropriate!). We'll make three changes to the CSS:

.list_container ul
margin-left: 2em;
padding-left: 0;
list-style-type: upper-roman; font-size: 14px; line-height: 17px;
font-family: Trebuchet MS, sans-serif, Arial, Helvetica; color: #369;

The list-style-type property value was changed to upper-roman, the margin-left was set to 2em, and color: #369; was added, all to the .list_container ul selector. Here's what we get after applying the changes:

    * List Item 1
    * List Item 2
    * List Item 3

The margin-left had to be increased, since the list markers would otherwise appear outside of the list's area, to the left (in fact, the list itself is no longer 200 pixels wide, only the a elements are). The list-style-type property value was changed to upper-roman (check the list-style-type CSS property reference for more options), and the markers themselves were colored using the color set to the UL element. Note that this will allow the background behind the list to show through if you don't set a background-color (or a background-image) value to the .list_container class.

We've covered some basic uses of HTML lists as navigational elements. You can see this is no rocket science, and is very easy to manipulate and scale. In CSS Lists: Part 2, we'll get into more complex lists, and more subtle property usage.

If you find this Portal System useful, please consider supporting us with a small Donation!
Check out the How to Section!!
Please remember to keep your back ups, up to date, before you try anything new!


Related Topics

  Subject / Started by Replies Last post
0 Replies
Last post May 08, 2010, 02:36:29 pm
by Maxx
2 Replies
Last post April 04, 2012, 02:40:57 pm
by Maxx
0 Replies
Last post January 29, 2013, 04:20:17 am
by skadamo
4 Replies
Last post March 10, 2015, 05:40:19 am
by Maxx
0 Replies
Last post March 10, 2015, 06:48:01 am
by Maxx

+-SMF Gallery

Quick Menu

Members Online

66 Guests, 0 Users
Powered by EzPortal