Clickable Link Backgrounds
来源:互联网 发布:the one 钢琴软件 编辑:程序博客网 时间:2024/06/12 01:17
A notebook entry from July 18, 2004
Is there any harm in documenting a seemingly rudimentary and obvious CSS technique? I tend to think not, and so the following may (or may not) turn out to be enlightening for you, depending on the level of your CSS-ness.
The Goal
A bulletproof unordered list of links, each with a unique (purely decorative) left-aligned icon that is referenced with CSS — but that is also clickable.
Often I’ll use a background-image
on unordered list items to create unique bullets. Applying the background
to the <li>
element usually gets the job done. But what if our list contains links, where we’d like the image to be clickable as well?
How to Get There
The first step is build the unordered list, adding a unique id
to each list item. We’ll use that id
to assign each corresponding icon later on.
<ul>
<li id="email"><a href="/email/">Send an Email</a></li>
<li id="donate"><a href="/donate/">Donate Money</a></li>
<li id="active"><a href="/active/">Get Active</a></li>
<li id="tell"><a href="/tell/">Tell a Friend</a></li>
<li id="vote"><a href="/vote/">Register to Vote</a></li>
</ul>
Next we’ll add the CSS, which is rather straighforward. Disabling default bullet styling, zero-ing out margins and padding, then assigning a background-image
to each <li>
element.
ul {
margin: 0;
padding: 0;
list-style: none;
}
ul li {
margin: 2px 0 6px 0;
padding: 0;
font-weight: bold;
line-height: 24px; /* height of icon */
background-repeat: no-repeat;
background-position: 0 50%;
}
ul li a {
padding-left: 30px; /* width of icon + whitespace */
}
#email { background-image: url(email.gif); }
#donate { background-image: url(donate.gif); }
#tell { background-image: url(tell.gif); }
#active { background-image: url(active.gif); }
#vote { background-image: url(vote.gif); }
So what we’ve done above is given each list item’s link a padding-left
that’s at least equal to the width of each icon. This will make the clickable area “stretch” over the icon — which is actually assigned as a background to each individual <li>
element.
Also notice the line-height
rule. I’ve found this to work out fairly well, matching that value to the height of the icon, so as to show the entire image, and nothing more. This doesn’t seem to negatively affect text resizing issues.
Flicker-free
Why not just assign the image as a background of the <a>
element? That’s when we run into flickering problems in IE6/Win. Assigning the image to the <li>
element and stretching the link’s width over the image fixes this issue, and thus the main reason for this entire write-up.
You may also be wondering what the harm would be in marking up these images inline with <img>
tags. There really isn’t. But the preceding method is another easy way to keep those non-essential images in CSS and out of the markup, making changes to the list and graphics a snap.
There is also a French translation of this article available.
- Clickable Link Backgrounds
- Clickable objects
- ListView Backgrounds: An Optimization
- CSS3 Borders & Backgrounds & Colors
- Window Backgrounds & UI Speed
- Multiple Backgrounds with CSS
- CSS3 Multiple Backgrounds
- CSS3 Multiple Backgrounds
- Stacking Order of Multiple Backgrounds
- CSS3属性之Multiple backgrounds
- Creating a clickable map
- FAQ_07_android:clickable 问题
- clickable textblock for wp7
- FAQ_07_android:clickable 问题
- setOnClickListener 影响 clickable
- FAQ_07_android:clickable 问题
- Androidz之clickable
- android:clickable 问题
- 人际沟通黄金四原则
- 如何取消开机时按F1才能继续的设置
- IT茶会66期,成功产品经理研讨会
- WINDOWS API 函数
- C#里静态构造函数
- Clickable Link Backgrounds
- 分析器错误信息: 未能加载类型"命名空间.类"... ---小结
- 按需变化解释有状态还是无状态EJB
- J2EE开发之常用开源项目介绍
- 用SessionListener实现统计在线人数及访问量
- 如何提高自己的管理效率-用好“计划”
- (Http请求)IIS状态代码的含义
- 复习数据结构和算法04-Hash Table(线型寻址散列)
- struts2.0简单的例子