我们在之前 HTML 基础 提到了,<a> 标签是一个超链接标签,它在不添加样式时,<a> 标签的默认外观包括以下特点:
那如果这些默认的样式,不符合设计稿要求,能不能修改呢?当然能。本文,就说一下这个。
<a> 标签可以使用任何 CSS 属性,比如以下几个常用的:
好,那我们看代码吧。
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <title>Link Styles</title>
- <style>
- .box {
- margin: 60px 0px;
- }
- /* 链接样式 */
- a {
- margin: 60px 20px 10px 10px;
- padding: 15px 10px;
- border-radius: 5px;
- color: #333; /* 默认链接颜色 */
- }
-
- /* 额外的链接样式 */
- .blue {
- color: #007bff; /* 蓝色链接颜色 */
- font-weight: bold; /* 加粗字体 */
- }
-
- .underline {
- text-decoration: none; /* 移除下划线 */
- }
-
- .italic {
- font-style: italic; /* 斜体字体 */
- border: 1px solid #000;
- }
-
- .pink-background {
- background-color: #ffc0cb; /* 粉色背景颜色 */
- }
- </style>
- </head>
- <body>
- <div class="box">
- <!-- Example Links -->
- <a href="#" class="blue">Blue Bold Link</a>
- <a href="#" class="underline">没有下划线的链接</a>
- <a href="#" class="italic">Italic Link</a>
- <a href="#" class="pink-background">Pink Background Link</a>
- <a href="#">Default Link</a>
- </div>
- </body>
- </html>
-
效果如下:
伪类选择器是 CSS 中一种特殊的选择器,用于选择元素的特定状态或者位置。它们以冒号(:)开头,放置在元素选择器的后面,用于指定元素在特定情况下的样式。
对于 <a> 标签的伪类选择器,常见的有以下几种:
下面是一个示例 HTML 代码,演示了如何使用这些伪类选择器:
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <title>Link Pseudo-classes Example</title>
- <style>
- /* 默认链接样式,不写:link也可以 */
- a {
- color: #00f;
- text-decoration: none;
- }
-
- /* 访问过的链接样式 */
- a:visited {
- color: #da2cdd;
- }
-
- /* 鼠标悬停链接样式 */
- a:hover {
- color: #f00;
- text-decoration: underline;
- }
-
- /* 激活链接样式 */
- a:active {
- color: #0f0;
- }
- </style>
- </head>
- <body>
- <!-- 示例链接 -->
- <a href="#">我是链接</a>
- </body>
- </html>
-
在这个示例中,链接的默认状态是蓝色且没有下划线,访问过后变为紫色,鼠标悬停时变为红色且有下划线,激活时变为绿色。这些样式通过伪类选择器实现了对链接不同状态的控制。
接下来呢,因为 hover 比较常用,这里演示几个 a 标签的 hover 效果,也许你项目里会用的上。
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <title>Document</title>
- <style>
- a.txtlink-1 {
- font-size: 25px;
- color: #333333;
- border-bottom: 2px solid #333333;
- }
-
- a.txtlink-1:hover {
- color: #25b6e6;
- border-bottom: 2px solid #25b6e6;
- }
-
- a.txtlink-2 {
- font-size: 25px;
- text-decoration: none;
- color: #008000;
- border-bottom: 0px;
- margin-left: 30px;
- }
-
- a.txtlink-2:hover {
- text-decoration: none;
- color: #008000;
- border-bottom: 2px solid #008000;
- }
-
- a.button-1 {
- color: #000000;
- border: 2px solid #4caf50;
- padding: 10px 20px;
- text-align: center;
- text-decoration: none;
- display: inline-block;
- font-size: 16px;
- margin-left: 30px;
- margin-top: 20px;
- }
-
- .button-1:hover {
- background-color: #4caf50;
- color: #ffffff;
- }
-
- a.button-2 {
- display: inline-block;
- background-color: #11b247;
- color: #ffffff;
- padding: 14px 25px;
- text-align: center;
- text-decoration: none;
- font-size: 16px;
- margin-left: 20px;
- opacity: 0.9;
- border: 0px;
- }
-
- a.button-2:hover {
- color: #ffffff;
- background-color: #05d44a;
- opacity: 1;
- }
- </style>
- </head>
- <body>
- <a href="#" class="txtlink-1">文本链接1</a>
- <a href="#" class="txtlink-2">文本链接2</a>
- <a href="#" class="button-1">链接按钮1</a>
- <a href="#" class="button-2">链接按钮2</a>
- </body>
- </html>
-
效果如下:
ok,本文完。
后台回复“前端工具”可获取开发工具,持续更新中
后台回复“前端基础题”可得到前端基础100题汇总,持续更新中
后台回复“前端电子书”可获取20+本精选电子书
我们在之前 HTML 基础 提到了,<a> 标签是一个超链接标签,它在不添加样式时,<a> 标签的默认外观包括以下特点:
那如果这些默认的样式,不符合设计稿要求,能不能修改呢?当然能。本文,就说一下这个。
<a> 标签可以使用任何 CSS 属性,比如以下几个常用的:
好,那我们看代码吧。
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <title>Link Styles</title>
- <style>
- .box {
- margin: 60px 0px;
- }
- /* 链接样式 */
- a {
- margin: 60px 20px 10px 10px;
- padding: 15px 10px;
- border-radius: 5px;
- color: #333; /* 默认链接颜色 */
- }
-
- /* 额外的链接样式 */
- .blue {
- color: #007bff; /* 蓝色链接颜色 */
- font-weight: bold; /* 加粗字体 */
- }
-
- .underline {
- text-decoration: none; /* 移除下划线 */
- }
-
- .italic {
- font-style: italic; /* 斜体字体 */
- border: 1px solid #000;
- }
-
- .pink-background {
- background-color: #ffc0cb; /* 粉色背景颜色 */
- }
- </style>
- </head>
- <body>
- <div class="box">
- <!-- Example Links -->
- <a href="#" class="blue">Blue Bold Link</a>
- <a href="#" class="underline">没有下划线的链接</a>
- <a href="#" class="italic">Italic Link</a>
- <a href="#" class="pink-background">Pink Background Link</a>
- <a href="#">Default Link</a>
- </div>
- </body>
- </html>
-
效果如下:
伪类选择器是 CSS 中一种特殊的选择器,用于选择元素的特定状态或者位置。它们以冒号(:)开头,放置在元素选择器的后面,用于指定元素在特定情况下的样式。
对于 <a> 标签的伪类选择器,常见的有以下几种:
下面是一个示例 HTML 代码,演示了如何使用这些伪类选择器:
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <title>Link Pseudo-classes Example</title>
- <style>
- /* 默认链接样式,不写:link也可以 */
- a {
- color: #00f;
- text-decoration: none;
- }
-
- /* 访问过的链接样式 */
- a:visited {
- color: #da2cdd;
- }
-
- /* 鼠标悬停链接样式 */
- a:hover {
- color: #f00;
- text-decoration: underline;
- }
-
- /* 激活链接样式 */
- a:active {
- color: #0f0;
- }
- </style>
- </head>
- <body>
- <!-- 示例链接 -->
- <a href="#">我是链接</a>
- </body>
- </html>
-
在这个示例中,链接的默认状态是蓝色且没有下划线,访问过后变为紫色,鼠标悬停时变为红色且有下划线,激活时变为绿色。这些样式通过伪类选择器实现了对链接不同状态的控制。
接下来呢,因为 hover 比较常用,这里演示几个 a 标签的 hover 效果,也许你项目里会用的上。
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <title>Document</title>
- <style>
- a.txtlink-1 {
- font-size: 25px;
- color: #333333;
- border-bottom: 2px solid #333333;
- }
-
- a.txtlink-1:hover {
- color: #25b6e6;
- border-bottom: 2px solid #25b6e6;
- }
-
- a.txtlink-2 {
- font-size: 25px;
- text-decoration: none;
- color: #008000;
- border-bottom: 0px;
- margin-left: 30px;
- }
-
- a.txtlink-2:hover {
- text-decoration: none;
- color: #008000;
- border-bottom: 2px solid #008000;
- }
-
- a.button-1 {
- color: #000000;
- border: 2px solid #4caf50;
- padding: 10px 20px;
- text-align: center;
- text-decoration: none;
- display: inline-block;
- font-size: 16px;
- margin-left: 30px;
- margin-top: 20px;
- }
-
- .button-1:hover {
- background-color: #4caf50;
- color: #ffffff;
- }
-
- a.button-2 {
- display: inline-block;
- background-color: #11b247;
- color: #ffffff;
- padding: 14px 25px;
- text-align: center;
- text-decoration: none;
- font-size: 16px;
- margin-left: 20px;
- opacity: 0.9;
- border: 0px;
- }
-
- a.button-2:hover {
- color: #ffffff;
- background-color: #05d44a;
- opacity: 1;
- }
- </style>
- </head>
- <body>
- <a href="#" class="txtlink-1">文本链接1</a>
- <a href="#" class="txtlink-2">文本链接2</a>
- <a href="#" class="button-1">链接按钮1</a>
- <a href="#" class="button-2">链接按钮2</a>
- </body>
- </html>
-
效果如下: