title besides title

 
Showing posts with label CSS - Lists. Show all posts
Showing posts with label CSS - Lists. Show all posts

Monday, December 17, 2012

CSS : Lists - [4] Introduction

From a wife handing a husband a grocery list as he steps out the door to a music channel presenting their top 100 worst songs of all time, lists help people stay focused and organized. In web design, it's the same case. HTML lists facilitate the presentation to our site visitors with organized content.
HTML lists, which group key elements together, can be ordered or unordered. Both types of lists are appealing in part because of the way they appear on the page. List items typically are indented and are keyed off by a marker, usually a filled circle for an unordered list or numbers for an ordered list (see Figure 4-1). With a few lines of HTML, a web coder can create a bulleted list on a web page without opening an image editor. Through CSS, you can create even more visually compelling lists.
Figure 4-1. The default rendering of a list

With a little CSS magic, we can tailor the presentation of the list to complement the design of a web page instead of relying on the browsers' default styling. While Chapter 3 covered navigation lists, this chapter illustrates how to change the numbering of list items, use your own image for a list marker, create a hanging indent that doesn't use a list marker, and more.

CSS : Lists - [4.1] Changing the Format of a List

Problem

You want to change the default list style, for example to change the bullet or numbering as in Figure 4-2.
Figure 4-2. The list markers changed to lowercase Roman numerals

Solution

Use the list-style-type property to change the bullet or type of counter:
li {

 list-style-type: lower-roman;

}

Discussion

The CSS 2.1 specification offers several styles for numbering a list, as shown in Table 4-1. Browsers typically vary the bullet style from one level of nesting to the next. To stop lists from presenting this traditional system of setting the list marker, change the value of list-style-type for each child list.
Table 4-1. Styles available for list markers
Style/value
Description
Browser support
square
Usually a filled-in square, although the exact representation isn't defined.
All major browsers
disc
Usually a filled-in circle, although the exact representation isn't defined.
All major browsers
circle
Usually an unfilled circle, although the exact representation isn't defined.
All major browsers
decimal
Starts with 1 and continues with 2, 3, 4, etc.
All major browsers
decimal-leading-zero
Starts with 01 and continues with 02, 03, 04, etc. The number of leading zeros may equal the number of digits used in a list. For example, 0001 might be used for a 5876-item list.
All major browsers, although leading zeros is optional
lower-roman
Starts with lowercase roman numbers.
All major browsers
upper-roman
Starts with uppercase roman numbers.
All major browsers
lower-alpha
Starts with lowercase ASCII letters.
All major browsers
upper-alpha
Starts with uppercase ASCII letters.
All major browsers
lower-latin
Starts with lowercase ASCII letters.
All major browsers
upper-latin
Starts with uppercase ASCII letters.
All major browsers
lower-greek
Starts with classical Greek letters, starting with alpha and then beta, gamma, etc.
Safari, Mozilla, Netscape 6+
hebrew
Starts counting with traditional Hebrew.
Safari, Mozilla, Netscape 6+
hiragana
Starts counting with the Japanese hiragana system.
Mozilla, Netscape 6+
katakana
Starts counting with the Japanese traditional katana system.
Mozilla, Netscape 6+
hiragana-iroha
Starts counting with the Japanese hiragana-iroha system.
Mozilla, Netscape 6+
katakana-iroha
Starts counting with the Japanese katakana-iroha system.
Mozilla, Netscape 6+
none
No marker is displayed.
All major browsers

CSS : Lists - [4.2] Writing Cross-Browser Indentation in Lists

Problem

Different browsers use different methods to indent lists. You want to specify left margins for your list that will render on all browsers.

Solution

Set both the margin-left and padding-left properties for the ul element:
ul {

 margin-left: 40px;

 padding-left: 0px;

}

Discussion

Different browsers use different methods to pad or indent a list. Mozilla and Netscape 6+ browsers indent a list on the padding, while Internet Explorer and Opera pad a list through the margin of a list.
To gain cross-browser effectiveness, you need to set the values for both the left margins and the padding for the list. Keep the amount of the indentation in one of the properties. Splitting the amount into two different properties results in inconsistent presentation across the browsers.
If you set the margin and padding to zero while the list is contained by only the body element, the browser renders the markers outside the viewport, making them invisible to the user. To make sure the markers are visible, set the left margin or left padding of the ul to at least 1em.

CSS : Lists - [4.3] Creating Custom Text Markers for Lists

Problem

You want to use a custom text marker in a list.

Solution

Indent the first line of text and insert the custom text, along with the right-angle quotes acting as pointers, through auto-generated content (see Figure 4-3):
ul {

 list-style: none;

 margin: 0;

 padding: 0 0 0 1em;

 text-indent: -1em; 

}

li {

 width: 33%;

 padding: 0;

 margin: 0 0 0.25em 0;

}

li:before {

 content: "\00BB \0020"; 

}

Figure 4-3. Text marker for a list

Discussion

Setting the list-style property to a value of none turns off the list marker usually associated with a list. Typically, a marker is appended to the left of each list item.
Instead of appending the marker to the list item, the custom text marker will be placed inline with the content of the item. Because the text marker is inside the list item, you need to push the marker out of the list item box. Indenting the first line of the marker with a negative value creates this push. The negative value for the text-indent property moves the first line to the left, whereas a positive value moves the indent to the right:
ul {

 list-style: none;

 margin: 0;

 padding: 0 0 0 1em;

 text-indent: -1em; 

}

The :before pseudo-element generates the text marker. Because the marker used in this example falls outside of the American Standard Code for Information Interchange (ASCII) 256-character set, its numerical equivalent needs to be determined.
However, because the ASCII character will be used in the CSS property and not on an HTML page, you need to write out the character in its escaped hexadecimal equivalent. You escape values in CSS by inserting a backslash before each hexadecimal value:
li:before {

 content: "\00BB \0020"; 

}

At press time, this solution worked in Mozilla, Netscape 6+, Safari, and Opera browsers because they can handle the creation of auto-generated content. Unfortunately, this list omits Netscape 4 and Internet Explorer for Windows and Macintosh.
To create a cross-browser effect, don't use auto-generated content. Instead, insert the text marker manually before the list item:
<ul>

 <li>&#187; I'm not the Same Person I was in the Database</li>

 <li>&#187; Past Breaches of Our Privacy</li>

 <li>&#187; The Best of Intentions</li>

 <li>&#187; Whatever Happened to Automation?</li>

 <li>&#187; The Smart Choice is Not Needing to Make One</li>

</ul>

The main drawback with this approach is that you have two markers for every list item. Although this isn't a mission-critical problem, it adds an unneeded design element to the web page.

CSS : Lists - [4.4] Creating Custom Image Markers for Lists

Problem

You want to use your own graphic for a list marker. For example, Figure 4-4 uses a diamond image.
Figure 4-4. Custom-made image markers for a list

Solution

Use the list-style-image property to use a graphic for a bullet marker:
ul {

 list-style-type: disc;

 list-style-image: url(bullet.gif);

}

Discussion

Set the location of the image you want to use as a marker as the value of the list-style-image property. You can't control the size of the image used as a list marker through CSS, so the image you specify should already be at the correct size. Images that are too large might interfere with the legibility of the list item or the marker might not be displayed entirely in the viewport, as shown in Figure 4-5. When creating custom bullets, make sure they are of the appropriate size to compliment the design of your web page.
Figure 4-5. A large image used for a marker isn't fully displayed

The value for the image marker is inherited, meaning that nested lists pick up the image as the marker as does the parent. To stop this inheritance, the value of none needs to be set for the child lists.
ul {

 list-style-type: disc;

 list-style-image: url(bullet.gif);

}

ul ul {list-style-type: none:}

Always include the list-style-type property to provide a fallback should the image not be usable. In the Solution the list marker disc is used if the image, bullet.gif, can't be displayed.

CSS : Lists - [4.5] Creating Inline Lists

Problem

You want to list items to be displayed within a paragraph, as in Figure 4-6 in which the bold, comma-separated list was generated from an HTML ul list.
Figure 4-6. The list formatted to appear inside a paragraph

Solution

Set the paragraphs before (and, if needed, after) the list:
<h3>

 Table of Contents

</h3>

<p>

 As proposed, the contents of the paper will contain the 

following sections:

</p>

<ul>

 <li>I'm not the Same Person I was in the Database</li>

 <li>Past Breaches of Our Privacy</li>

 <li>The Best of Intentions</li>

 <li>Whatever Happened to Automation?</li>

 <li class="last">The Smart Choice is Not Needing to Make One</li>

</ul>

<p>

 If there are any objections to how these sections are divided, 

please let <a href="nick@heatvision.com">Nicholas</a> know about 

it.

</p>

Through CSS, set the paragraph to display as inline elements and then use auto-generated content to show the commas between items and the period at the end of the list:
ul, li {

 display: inline;

 margin: 0;

 padding: 0;

 font-weight: bold; 

 font-style: italic;

}

li:after {

 content: ", "; 

}

li.last:after {

 content: ".";

}

p {

 display: inline;

}

Discussion

Through this method you retain the structure of lists and paragraphs, but you stretch CSS's capability to present the list inside a paragraph. However, you hide the obvious visual appearance of a list in favor of having the contents placed inside a paragraph.

CSS : Lists - [4.6] Making Hanging Indents in a List

Problem

You want the first line of a list item to begin further to the left than the rest of the list, thereby creating a hanging indent as in Figure 4-7.
Figure 4-7. Hanging indents on a list

Solution

Use a negative value for the text-indent property:
ul {

 width: 30%;

 padding: 0 0 0.75em 0; 

 margin: 0; 

 list-style: none;

}

li {

 text-indent: -0.75em; 

 margin: 0.33em 0.5em 0.5em 1.5em;

}

Discussion

Although list markers (numeric, image, or text) help to call attention to the actual list, sometimes you might not want to add those kinds of design elements to a list. Instead of relying on markers to carry off the list design, use a hanging indent.
In this Solution, you indent the list by three-quarters of an em unit, creating a visible but almost subtle hanging indent effect. You can push this design technique from subtle to the foreground by reducing the negative value further, or by increasing the font size of the text in the list item.

CSS : Lists - [4.7] Moving the Marker Inside the List

Problem

You want the list marker to be pulled inside the border of the list items, as in Figure 4-8. This creates an effect in which the text wraps around the marker.
Figure 4-8. Moving the marker inside the list item

Solution

Use the list-style-position property and set the value to inside:
li {

 list-style-position: inside;

 width: 33%;

 padding: 0;

 margin: 0;

}

ul {

 margin: 0;

 padding: 0 0 0 1em;

}

Discussion

Normally the list marker stands outside the text and the result is a very distinctive list. Some designs, however, might require the marker to appear as part of the text. A designer might choose to keep the marker inside, for example, to eliminate the need to have enough whitespace on the left side. Also, replacing the list marker with your own custom marker can visually enhance this recipe. For example, Figure 4-9 shows arrows rather than the default bullet.
Figure 4-9. Custom marker inside the list item