|
CSS Tutorials > Making Lists Look Nicer with CSS |
Making Lists Look Nicer with CSSStandard HTML lists are usually pretty boring - a few indented lines with bullets or numbers down the left side. However, with the power of CSS, you can really get creative with your lists! Read on for details... Controlling indentationCSS allows you to have more control over the level of indentation of the list items. This is great for those times when your lists start veering across the page, ruining the nice, perfectly-aligned look of your design. You can adjust the amount of indentation that a list uses by setting both the margin-left and padding-left properties for the ul selector. For example:
ul
{
margin-left: 0;
padding-left: 0;
}
Note that you have to specify both margin-left and padding-left for this to work properly across all browsers. So let's create a simple list inside a dark rectangle: <div style="background-color: #3C8B8B"> <ul style="margin-left: 0; padding-left: 0;"> <li>Rover the dog</li> <li>Fluffy the cat</li> <li>Jack the rabbit</li> </ul> </div> This will produce the following list:
The trouble is - our list bullets are outside the rectangle - invisible even, on some browsers! This is because, by default, a list item's bullet is outside the list item's CSS box. We can fix this problem by adding a bit of padding to the list: <ul style="margin-left: 0; padding-left: 30px;"> This produces the following effect:
That's more like it! So now we can control exactly how far our lists indent, for example: <ul style="margin-left: 0; padding-left: 5px;">
Using different bulletsAnother great thing you can do with CSS is change the style of bullets. This is achieved using the list-style-type property. For example, to change the bullets to squares: <ul style="list-style-type: square; margin-left: 0; padding-left: 30px;">
There are also many types of numbering systems you can use for numbered lists. For example: list-style-type: lower-roman;
list-style-type: lower-alpha;
list-style-type: decimal-leading-zero; (Only works on some browsers)
For a full list of bullet types that you can use in lists, see the W3C CSS Spec. Using images for bulletsYou can even use images for list bullets!
list-style-image: url("images/smiley.gif");
Inline listsAnother really useful thing you can do with CSS is create inline lists, using display: inline. For example:
ul, li
{
display: inline;
}
easy to select alternate style sheets, assuming that they can do soat all. Should a browser be able to use alternate style sheets, itwill use the values of the TITLE attributes togenerate a list of style alternatives. So you could write thefollowing:
<LINK REL="stylesheet" TYPE="text/css"HREF="sheet1.css" TITLE="Default"><LINK REL="alternate stylesheet" TYPE="text/css"HREF="bigtext.css" TITLE="Big Text"> Now that the list is not constrained to being displayed in a vertical row, you have a lot more control over the layout. For example, by adding in a few borders and background colours, you can give the list items a "button" effect:
ul, li
{
display: inline;
}
li
{
border: solid 1px white;
padding: 5px;
margin: 5px;
background-color: #339999;
}
You can see that, while the basic HTML for our list has remained the same throughout this tutorial, we have managed to change the look of the list fairly radically through the use of CSS. Pretty cool! This has hopefully given you a taste of some of the neat things you can do with CSS and lists. For more info on formatting lists with CSS, see the W3C Specification. The EndThat's the end of this tutorial. We hope you found it useful. If you're still stuck and would like further help, check out our online Help Forums, where you can get assistance from members of my and other webmasters. If you would like to offer us feedback on this or any of the tutorials, please contact us. Have fun! |
Of course, this is not always the case: you could set an explicitheight and let the width scale to fit the content. Thus:
top: 0; bottom: auto; left: auto; right: 0; width: auto; height: 10em;
Here the element will be 10 ems tall no matter what, but its widthcan vary to exactly fit the content. This is sometimes called"shrink-wrapping" thecontent, since it mimics the act of applying shrink-wrap to a box orother product. In the same way the plastic shrink-wrap precisely hugs
The situation can become markedly different if we change the vertical alignment of the inline boxes. Suppose that we change the boldface text to have a vertical alignment of middle. This would have the result shown in Figure 8-53.
Here, the middle of the boldfaced text's inline box has lined up with the middle of the inline boxes of the other text in the line. Because the inline boxes are all 12px tall, and their middles are allelement so that it ends up below a floated element, so any margin width set for the top of a cleared element should be effectively ignored. That is, instead of being 1.5em , for example, it could be increased to 10em , or 25px , or 7.133in , or however much is needed to move the element down far enough so that the content area is below the bottom edge of a floated element.
WARNING
Percentage values refer to a point on both the element and the origin image (see explanation in Section 6.2.3.2, "Percentage values" later in this chapter).
This positioning is all done using background-position, of course, but there are a whole lot of ways to supply values for this property. First off, there are the keywords top, bottom , left, right, and center. Usually, tag and theAtag, both of which are nonreplaced elements, and images,which are replaced elements.
WARNING
Note that none of this applies to table elements. CSS2 introduces new properties andbehaviors for handling tables and table content, and these newfeatures behave in ways fairly distinct from either block-level or