超链接虽然不起眼,但它却是万维网(Web)的基石,它使得用户可以在整个万维网进行导航。 链接的默认样式却很低调,在富有设计感的网站中通常会重写链接的样式。 下文介绍常用的一些链接样式技巧:链接状态选择符、自定义下划线、设置外链样式、按钮的实现等。
链接状态选择符
超链接标签有很多状态:未访问、已访问、悬停、激活。这些都可以通过CSS伪类选择符来识别。 其中悬停和激活是一般HTML元素都具有的状态,对于链接而言,当鼠标悬停时 :hover 生效, 点击后 :active 生效。比如我们设置未访问的链接呈蓝色,已访问的链接呈绿色:
a:link {color: blue;}a:visited {color: green;}a:hover, a:active {color: red;}
关于伪类选择器的概述可以参考:CSS选择符总结
超链接默认的下划线有时会在设计上造成过分的强调和凌乱感,一般倾向于去掉它。 同时在鼠标悬停时我们希望给用户一定的操作反馈。可以这样做:
a:link, a:visited {text-decoration: none;}a:hover, a:active {text-decoration: underline;}
注意上面两行代码的顺序是重要的。如果反过来 :hover 和 :active 是不生效的。
a:hover, a:active {text-decoration: underline;}a:link, a:visited {text-decoration: none;}
这是因为 :link, :visited 与 :hover, :active 选择的元素是存在交集的。 而这四个选择符具有同样的优先级,这时按照CSS的规则最后一条声明生效。 CSS优先级在 各种CSS选择器的优先级 一文中有详述。 在使用这四个选择器时,建议使用这样的顺序(从一般到特殊):
:link, :visited, :hover, :active
自定义下划线
超链接的下划线是由 text-decoration 属性指定的,本身它的样式是不可自定义的。 但我们可以用一个背景图来做到:
a { color: #666; text-decoration: none; background: url(underline.gif) repeat-x left bottom;}
设置背景图片的语法可参考 那些 CSS 背景图的技巧 一文。
背景图片也存在缺点,现代页面很少用很多图片来辅助做样式了, 甚至sprite也逐渐由字体文件所代替了。自定义下划线的另一个办法是设置 border-bottom :
a{ text-decoration: none; border-bottom: 1px dotted #000;}
设置外链样式
有时我们希望在样式上标识所有出站的链接,比如在右上角加一个图标:
CSS提供了非常强大的选择符,可以通过属性选择符来判断外链:
a { background: url(images/externalLink.gif) no-repeat right top; padding-right: 10px;}a[href^=/], a[href^=http://hartle.com], a[href^=http://blog.harttle.com]{ background-image: none; padding-right: 0;}
先设置右上角的图片,然后添加另一条规则将自己网站排除在外。 其中 href^=/ 匹配的是所有具有以 / 开头属性值的标签。参考:CSS选择符总结 当然,还可以单独设置某种文件类型(后缀)的链接,例如 a[href$=".pdf"]{...} 。
创建按钮
怎样用CSS创建一个按钮呢?我们来看看Bootstrap是怎样做的:
.btn { display: inline-block; padding: 6px 12px; margin-bottom: 0; font-size: 14px; font-weight: 400; line-height: 1.42857143; text-align: center; white-space: nowrap; vertical-align: middle; cursor: pointer; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; background-image: none; border: 1px solid transparent; border-radius: 4px}
其中重要的几个设置包括:
- inline-block 显示,这样才能设置 padding (准确的说,这时内边距才会影响行高,见 CSS Display属性与盒模型 )。
- cursor:pointer 设置光标为手型,这才是按钮嘛!
- border 和 border-radius 设置一个有圆角的边框。
除此之外,Bootstrap提供了 .btn-primary , .btn-success 等六个类来设置色调。 这些类与 .btn 类同时使用,例如:
<a class="btn btn-success">Click Me</a>
同时Bootstrap还通过 :hover 和 :active 来加深色调来给用户反馈:
.btn-success { color: #fff; background-color: #5cb85c; border-color: #4cae4c}.btn-success:hover,.btn-success:focus,.btn-success:active{ color: #fff; background-color: #449d44; border-color: #398439}

The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools
