Clickable Link Backgrounds

本文介绍了一种使用CSS为无序列表中的链接分配独特且可点击图标的方法。通过为每个列表项分配背景图像,并调整链接的内边距,使得图标成为可点击区域的一部分。这种方法避免了在Internet Explorer 6中出现的闪烁问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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

exampleA 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.

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值