编程笔记 html5&css&js 048 CSS链接

通过 CSS,可以用不同的方式设置链接的样式。

一、设置链接样式

链接可以使用任何 CSS 属性(例如 color、font-family、background 等)来设置样式。
a {
color: hotpink;
}
可以根据链接处于什么状态来设置链接的不同样式。这里使用的是伪类选择器
四种链接状态分别是:

a:link - 正常的,未访问的链接
a:visited - 用户访问过的链接
a:hover - 用户将鼠标悬停在链接上时
a:active - 链接被点击时

/* 未被访问的链接 */
a:link {
  color: red;
}
/* 已被访问的链接 */
a:visited {
  color: green;
}
/* 将鼠标悬停在链接上 */
a:hover {
  color: hotpink;
}
/* 被选择的链接 */
a:active {
  color: blue;
}

如果为多个链接状态设置样式,请遵循如下顺序规则:
a:hover 必须 a:link 和 a:visited 之后
a:active 必须在 a:hover 之后

二、文本装饰

text-decoration 属性主要用于从链接中删除下划线:

a:link {
  text-decoration: none;
}
a:visited {
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}
a:active {
  text-decoration: underline;
}

三、背景色

background-color 属性可用于指定链接的背景色:

a:link {
  background-color: yellow;
}
a:visited {
  background-color: cyan;
}
a:hover {
  background-color: lightgreen;
}
a:active {
  background-color: hotpink;
} 

四、链接按钮

本例演示了一个更高级的例子,其中我们组合了多个 CSS 属性,将链接显示为框/按钮:

a:link, a:visited {
  background-color: #f44336;
  color: white;
  padding: 14px 25px;
  text-align: center; 
  text-decoration: none;
  display: inline-block;
}

a:hover, a:active {
  background-color: red;
}

五、练习

<!DOCTYPE html>
<html lang="zh-cn">
   <head>
      <title>css链接 编程笔记 html5&css&js</title>
      <meta charset="utf-8" />
      <style>
         body {
            color: cyan;
            background-color: teal;
         }
         a.one:link {
            color: #ff0000;
         }
         a.one:visited {
            color: #0000ff;
         }
         a.one:hover {
            color: #ffcc00;
         }

         a.two:link {
            color: #ff0000;
         }
         a.two:visited {
            color: #0000ff;
         }
         a.two:hover {
            font-size: 150%;
         }

         a.three:link {
            color: #ff0000;
         }
         a.three:visited {
            color: #0000ff;
         }
         a.three:hover {
            background: #66ff66;
         }

         a.four:link {
            color: #ff0000;
         }
         a.four:visited {
            color: #0000ff;
         }
         a.four:hover {
            font-family: monospace;
         }

         a.five:link {
            color: #ff0000;
            text-decoration: none;
         }
         a.five:visited {
            color: #0000ff;
            text-decoration: none;
         }
         a.five:hover {
            text-decoration: underline;
         }
      </style>
   </head>
   <body>
      <div align="center">
         <h2>移动鼠标观察样式的变化:</h2>
         <p>
            <b>
               <a class="one" href="https://yss5678.blog.csdn.net/" target="_blank">
                  明月看潮生的博客 此链接改变颜色
               </a>
            </b>
         </p>
         <p>
            <b>
               <a class="two" href="https://yss5678.blog.csdn.net/" target="_blank">
                  明月看潮生的博客 此链接改变字体大小
               </a>
            </b>
         </p>
         <p>
            <b>
               <a class="three" href="https://yss5678.blog.csdn.net/" target="_blank">
                  明月看潮生的博客 此链接改变背景色
               </a>
            </b>
         </p>
         <p>
            <b>
               <a class="four" href="https://yss5678.blog.csdn.net/" target="_blank">
                  明月看潮生的博客 此链接改变字体族
               </a>
            </b>
         </p>
         <p>
            <b>
               <a class="five" href="https://yss5678.blog.csdn.net/" target="_blank">
                  明月看潮生的博客 此链接改变文本装饰
               </a>
            </b>
         </p>
      </div>
   </body>
</html>

小结

有关伪类选择器就不再单独练习了。

相关推荐

  1. 编程笔记 html5&css&js 048 CSS

    2024-01-20 21:42:01       37 阅读
  2. 编程笔记 html5&css&js 009 HTML

    2024-01-20 21:42:01       32 阅读
  3. 编程笔记 html5&css&js 009 HTML 我的网址簿

    2024-01-20 21:42:01       18 阅读
  4. 编程笔记 html5&css&js 042 CSS颜色

    2024-01-20 21:42:01       26 阅读
  5. 编程笔记 html5&css&js 038 CSS背景

    2024-01-20 21:42:01       35 阅读
  6. 编程笔记 html5&css&js 044 CSS显示

    2024-01-20 21:42:01       26 阅读
  7. 编程笔记 html5&css&js 049 CSS列表

    2024-01-20 21:42:01       33 阅读
  8. 编程笔记 html5&css&js 058 css计数器

    2024-01-20 21:42:01       30 阅读
  9. 编程笔记 html5&css&js 008 HTML图片

    2024-01-20 21:42:01       33 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-20 21:42:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-20 21:42:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-20 21:42:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-20 21:42:01       20 阅读

热门阅读

  1. 笨蛋学设计模式行为型模式-观察者模式【14】

    2024-01-20 21:42:01       31 阅读
  2. 基本算法--分治法(快排,归并)习题

    2024-01-20 21:42:01       42 阅读
  3. 鸿蒙harmony--HTTP数据请求的简单使用

    2024-01-20 21:42:01       26 阅读
  4. 微信小程序实现下拉简单展示接口数据

    2024-01-20 21:42:01       39 阅读
  5. 嵌入式C语言--LD文件的概念

    2024-01-20 21:42:01       37 阅读
  6. Jtti:Ubuntu中如何搭建LAMP开发环境

    2024-01-20 21:42:01       32 阅读
  7. 《python算法与数据结构2000讲》0217. 存在重复元素

    2024-01-20 21:42:01       37 阅读