Home Web Front-end HTML Tutorial CSS中a标签样式的“爱恨”原则 - jerrylsxu

CSS中a标签样式的“爱恨”原则 - jerrylsxu

May 20, 2016 pm 01:46 PM

CSS为一些特殊效果准备了特定的工具,我们称之为“伪类”。其中有几项是我们经常用到的,下面我们就详细介绍一下经常用于定义链接样式的四个伪类,它们分别是:

1 :link
2 :visited
3 :hover
4 :active
因为我们要定义链接样式,所以其中必不可少的就是超级链接中的锚标签--a,锚标签和伪类链接起来书写的方法就是定义链接样式的基础方法,它们的写法如下:
1 a:link,定义正常链接的样式;
2 a:visited,定义已访问过链接的样式;
3 a:hover,定义鼠标悬浮在链接上时的样式;
4 a:active,定义鼠标点击链接时的样式。
示例:
01 a:link {
02    color:#FF0000;
03    text-decoration:underline;
04 }
05 a:visited {
06    color:#00FF00;
07    text-decoration:none;
08 }
09      
10 a:hover {
11    color:#000000;
12    text-decoration:none;
13    }
14      
15 a:active {
16    color:#FFFFFF;
17    text-decoration:none;
18 }
上面示例中定义的链接颜色是红色,访问过后的链接是绿色,鼠标悬浮在链接上时是黑色,点击时的颜色是白色。
 
如果正常链接和已访问过的链接样式相同,鼠标悬浮和点击时的样式相同,也可以将它们合并起来定义:
1 a:link, a:visited {
2    color:#FF0000;
3    text-decoration:underline;
4 }    
5 a:hover, a:active {
6    color:#000000;
7    text-decoration:none;
8 }
链接定义的顺序
 
没有规矩不成方圆,虽然链接定义写好了,但它也是有规则的,如果这四项的书写顺序稍有差错,链接的效果可能就没有了,所以每次定义链接样式时务必确认定义的顺序,link--visited--hover-active,也就是我们常说到的LoVe HAte原则(大写字母就是它们的首字母)。
 
老外总结了一个便于记忆的“爱恨原则”(LoVe/HAte),即四种伪类的首字母:LVHA。定义A链接样式的正确的顺序:a:link、a:visited、a:hover、a:active。
 
为什么我们不能改变定义的顺序?做下测试就可以了。
 
假设我们想实现下面的样式:
 
状态 样式 颜色
已访问 a:visited 红
未访问 a:link 蓝
选定 a:active 绿
鼠标移入 a:hover 黄
鼠标移入时,并没有变黄。而是当这个链接已经被访问过后,鼠标移入才变黄:
1 a:visited{color:red;}
2 a:hover{ color:yellow;}
3 a:link{ color:blue;}
4 a:active{ color:green;}
这是因为,一个鼠标经过的未访问的链接同时拥有a:link,a:hover两种属性,在上述的CSS样式中,a:link离他最近,先满足a:link,而放弃a:hover的重复定义。
 
而使用LVHA顺序声明后,它首先检查a:hover的符合标准,先变色。
 
所以说,为了符合浏览器解释CSS遵循的"就近原则"。我们在定义CSS中,宜将最一般的条件放在最上面,并依次向下,最下面放最特殊的。
 
在W3C规范中,也规定了链接的声明顺序:
 
在 CSS 定义中,a:hover 必须被置于 a:link 和 a:visited 之后,才是有效的。
在 CSS 定义中,a:active 必须被置于 a:hover 之后,才是有效的。
定义局部链接样式
 
在CSS中写上a:link{}这样的定义会使整个页面的链接样式改变,但有些局部链接需要特殊化,这个问题也不难解决,只要在链接样式定义的前面加上指定的id或class就可以了。
1 #sidebar a:link, #sidebar a:visiteid {
2    color:#FF0000;
3    text-decoration:none;
4 }
5 #sidebar a:hover, #sidebar a:active {
6    color:#000000;
7    text-decoration:underline;
8 }
HTML调用:
<div id="</code"> <code>"sidebar"><a href><code>"aa.aspx" target="_blank">链接到aa页面<a></a>
class的定义方法和id相同,只要将#sidebar改为.sidebar就行了,还有一种方法是直接定义链接的样式,那样更直接,不过调用时比较麻烦,需要给每个特定的链接加上定义的代码。
1 a.redlink a:link, a.redlink a:visiteid {
2    color:#FF0000;
3    text-decoration:none;
4 }
5 a.redlink a:hover, a.redlink a:active {
6    color:#000000;
7    text-decoration:underline;
8    background:#FFFFFF;
9 }
 
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to add an icon to your website title tab in HTML How to add an icon to your website title tab in HTML Aug 07, 2025 pm 11:30 PM

To add an icon to the website title bar, you need to link a favicon file in part of the HTML. The specific steps are as follows: 1. Prepare a 16x16 or 32x32 pixel icon file. It is recommended to use favicon.ico to name it and place it in the website root directory, or use modern formats such as PNG and SVG; 2. Add link tags to HTML, such as PNG or SVG formats, adjust the type attribute accordingly; 3. Optionally add high-resolution icons for mobile devices, such as AppleTouchIcon, and specify different sizes through the sizes attribute; 4. Follow best practices, place the icon in the root directory to ensure automatic detection, clear the browser cache after update, and check the correctness of the file path.

Why is my HTML image not showing up? Why is my HTML image not showing up? Aug 16, 2025 am 10:08 AM

First, check whether the src attribute path is correct, and ensure that the relative or absolute path matches the HTML file location; 2. Verify whether the file name and extension are spelled correctly and case-sensitive; 3. Confirm that the image file actually exists in the specified directory; 4. Use appropriate alt attributes and ensure that the image format is .jpg, .png, .gif or .webp widely supported by the browser; 5. Troubleshoot browser cache issues, try to force refresh or directly access the image URL; 6. Check server permission settings to ensure that the file can be read and not blocked; 7. Verify that the img tag syntax is correct, including the correct quotes and attribute order, and finally troubleshoot 404 errors or syntax problems through the browser developer tool to ensure that the image is displayed normally.

How to use the HTML abbr tag for abbreviations How to use the HTML abbr tag for abbreviations Aug 05, 2025 pm 12:54 PM

Using HTML tags can improve the accessibility and clarity of content; 1. Mark abbreviations or acronyms with abbreviations; 2. Add title attributes to unusual abbreviations to provide a complete explanation; 3. Use when the document first appears, avoiding duplicate annotations; 4. You can customize the style through CSS, and the default browser usually displays dotted underscores; 5. It helps screen reader users understand terms and enhance user experience.

How to add an icon to a button in HTML How to add an icon to a button in HTML Aug 07, 2025 pm 11:09 PM

Using FontAwesome can quickly add icons by introducing CDN and adding icon classes to buttons, such as Like; 2. Using labels to embed custom icons in buttons, the correct path and size must be specified; 3. Embed SVG code directly to achieve high-resolution icons and keep them consistent with the text color; 4. Spacing should be added through CSS and aria-label should be added to the icon buttons to improve accessibility; in summary, FontAwesome is most suitable for standard icons, pictures are suitable for custom designs, while SVG provides the best scaling and control, and methods should be selected according to project needs. FontAwesome is usually recommended.

How to use the bdo tag to override text direction in HTML How to use the bdo tag to override text direction in HTML Aug 16, 2025 am 09:32 AM

Thebdotagisusedtooverridethebrowser’sdefaulttextdirectionrenderingwhendealingwithmixedleft-to-rightandright-to-lefttext,ensuringcorrectvisualdisplaybyforcingaspecificdirectionusingthedirattributewithvalues"ltr"or"rtl",asdemonstrat

Extract nested URLs from dynamic web pages using R language: httpr and API interaction practice Extract nested URLs from dynamic web pages using R language: httpr and API interaction practice Aug 27, 2025 pm 07:06 PM

This tutorial explores the problem of crawling failure if JavaScript dynamically loads content when crawling URLs from web pages using the R language rvest package. The article explains in detail why traditional HTML parsing methods may be invalid and provides an efficient solution: by identifying and directly calling the API interface behind the web page, using the httr package to obtain JSON data, thereby successfully extracting the required information.

What is the difference between HTML id and class What is the difference between HTML id and class Aug 07, 2025 am 12:03 AM

The id must be unique. One id in each page can only be used for one element, and the class can be reused on multiple elements, and one element can have multiple classes; 2. Scenarios using id include: positioning a single specific element, link anchors within the page, JavaScript operates elements through id, and labels associated with form elements; scenarios using class include: applying the same style or behavior to multiple elements, building reusable UI components, and selecting multiple elements in JavaScript; 3. In CSS, targeting is done by #id selector and .class selector respectively, getElementById() is used for id, getEleme

How to highlight text with the  tag? How to highlight text with the tag? Aug 04, 2025 pm 04:29 PM

Use tags to highlight text semantically, often used to identify search results or important content; 2. Custom styles such as background colors, text colors and borders can be customized through CSS; 3. It should be used in contexts with practical significance, rather than just visual decoration to improve accessibility and SEO effects.

See all articles