打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
The Amazing LI: Using CSS and Unordered List Items to Do Just About Anything

March 19th, 2008 in Design Tips & Tutorials

by: Matthew Griffin

Ican still remember the day I discovered the <li> tag. It's notthat I had never used list items before—I had built plenty of bulletedlists. What I discovered that day was that with a little CSS, the<li> becomes one of the most powerful and versatile tags in a webdesigner's arsenal. So versatile is the list item, in fact, that youcould build and entire website layout out of just <ul> <li>tag pairs. Of course, that wouldn't be semantically correct, but you could do it. This article is a tutorial and a tribute to the amazing <li>.

Using <li>s for Horizontal Navigation

You can use unordered list items to present horizontal navigationbuttons and other horizontal lists. When I first moved from table-basedlayouts to CSS, this was a big shocker for me. It opens up a world ofpossibilities and it makes your code oh so beautiful and easy to read.Here's an example of a five-button horizontal nav bar made completelyof list items.

<!-- The CSS -->

ul{
    margin: 0 auto;
}
ul.horizontal_list li{
    text-align: left;
    float: left;
    list-style: none;
    padding: 3px 10px 3px 10px;
    margin: 5px;
    border: 1px solid #CCC;
}

<!-- The HTML -->

<ul class="horizontal_list">
    <li>Home</li>
    <li>About Us</li>
    <li>Contact Us</li>
    <li>News</li>
    <li>Mission</li>
</ul>

This is how it should look:

Multi-column Lists with <li>

Building lists that wrap into multiple columns is quick and easywith <li>. Say goodbye to <tr> because you'll probablynever need it again. When you use list items instead of table rows andcells, it's easy to reorder your items without moving every block ofcontent in the process. Here's how it works.

<!-- Here's the HTML -->

<div id="list_wrapper">
    <ul class="multiple_columns">
        <li>One</li>
        <li>Two</li>
        <li>Three</li>
        <li>Four</li>
        <li>Five</li>
        <li>Six</li>
        <li>Seven</li>
        <li>Eight</li>
        <li>Nine</li>
    </ul>
</div>

<!-- Here's the CSS -->

ul{
    margin: 0 auto;
}

/* The wider the #list_wrapper is, the more columns will fit in it */
#list_wrapper{
    width: 200px
}

/* The wider this li is, the fewer columns there will be */
    ul.multiple_columns li{
        text-align: left;
        float: left;
        list-style: none;
        height: 30px;
        width: 50px;
    }

This is how it should look:

Cool <li> Background Effects

Want lists with cool bullets instead of the boring default blackdot? CSS makes this possible with some simple adjustments to thebackground properties of your <li>. For this example, we'llbarrow our code from the first example and build on it.

<!-- Here's the HTML -->

<ul class="cool_background">
    <li>Home</li>
    <li>About Us</li>
    <li>Contact Us</li>
    <li>News</li>
    <li>Mission</li>
</ul>

<!-- Here's the CSS -->

ul{
    margin: 0 auto;
}
ul.cool_background li{
    text-align: left;
    float: left;
    list-style: none;
    padding: 3px 10px 3px 25px;
    margin: 5px;
    background: url(cool_background.gif) 5px 5px no-repeat;
}

IMPORTANT: Don't forget to make your "cool_background.gif" file and put it into the same directory as your page.

This is what it should look like:



Animating Your <li>s with a Rollover Effect

A combination of <li> and <a> tags, and a little CSS canmake for a good-looking rollover effect. Using CSS to produce yourrollovers is quicker and easier than JavaScript. It also makes iteasier change in the future. Below is a basic example:

<!-- Here's the HTML -->


<ul class="rollover">
    <li><a href="#">Home</a></li>
    <li><a href="#">About Us</a></li>
    <li><a href="#">Contact Us</a></li>
    <li><a href="#">News</a></li>
    <li><a href="#">Mission</a></li>
</ul>

<!-- Here's the CSS -->

ul{
    margin: 0 auto;
}
ul.rollover li{
    text-align: left;
    float: left;
    list-style: none;
}
    ul.rollover a{
        display: block;
        text-decoration: none;
        background: url(cool_background.gif) 5px 5px no-repeat;
        padding: 3px 10px 3px 25px;
        margin: 5px;
    }
    ul.rollover a:hover{
        background-image: url(cool_background2.gif);
        text-decoration: none;
    }

IMPORTANT: Don't forget to make your coolbackground images and put it into the same directory as your page. Thisis what it should look like:

I'm sure this tutorial will be helpful for the CSS newbie. If you'rea CSS veteran purist , though, and you know a better way I could havecoded any of the examples, please comment. I purposefully left outdetailed explanations in this tutorial. It's meant as a place to startand should give you enough code to begin playing with <li>s.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
使用jQuery创建简单的手风琴菜单
Tab滑动选项卡
DivCSS布局实例:很实用的图文混排CSS列表
怎么去掉li、ul等前面的点及如何去掉空格的方法
php分页
HTML 中的列表
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服