css - 链接状态在我的代码中不起作用
问题描述
我的代码显示出一种奇怪的行为。我不知道为什么当我向 .btn 类添加状态时,按钮的所有 CSS 功能都会被 Web 浏览器完全忽略。因此,按钮以没有 CSS 的方式显示。但是,如果我摆脱状态,则 css 会应用于按钮,并且它们会被浏览器很好地呈现。
你可以在下面找到我的代码。如果有人能帮助找出为什么向按钮添加状态是有问题的,我真的很感激。
/* links in general have 4 states
hover, visited, link(normal mode) and active
*/
.btn:link,
.btn:visited {
/* We use inline-block to be
able to use hight and width
for that*/
display: inline-block;
padding: 10px 30px;
font-weight: 300;/* Removing the line under a word */
text-decoration: none;
border-radius: 200px;
}
.btn-full:link,
.btn-full:visited {
background-color: #e67e22;
border: 1px solid #e67e22;
color: #fff;
}
.btn-ghost:link,
.btn-ghost:visited {
border: 1px solid #e67e22;
color: #e67e22;
}
.btn:hover,
.btn:active {
background-color: #bf6516;
border: 1px solid #bf6516;
}
<a class="btn"> This link is not working </a>
解决方案
有状态的a
标签与没有:link
状态的标签并不完全相同:link
。此状态适用于a
定义了 href
:link
CSS 伪类表示尚未访问的元素。它匹配每个未访问<a>
的 ,<area>
或具有 href 属性的<link>
元素。参考
/* links in general have 4 states
hover, visited, link(normal mode) and active
*/
.btn:link,
.btn:visited{
/* We use inline-block to be
able to use hight and width
for that*/
display: inline-block;
padding: 10px 30px;
font-weight: 300;
text-decoration: none;
border-radius: 200px;
background:pink;
}
.btn:hover,
.btn:active {
background-color: #bf6516;
border: 1px solid #bf6516;
}
<a href="#" class="btn">A link with href</a>
<a class="btn">A link without href</a>
所以为了确保它总是能更好地添加没有任何状态的样式:
/* links in general have 4 states
hover, visited, link(normal mode) and active
*/
.btn,
.btn:link,
.btn:visited{
display: inline-block;
padding: 10px 30px;
font-weight: 300;
text-decoration: none;
border-radius: 200px;
background:pink;
}
.btn:hover,
.btn:active {
background-color: #bf6516;
border: 1px solid #bf6516;
}
<a href="#" class="btn">A link with href</a>
<a class="btn">A link without href</a>
推荐阅读
- javascript - 如何在 Sequelize 中直接更改 Eager Loading 的结果
- c# - Automapper,报告更新的属性
- pine-script - 对函数内部简单条件的看似错误的评估
- php - 类 stdClass 的 PDO 函数对象无法转换为字符串
- python - Python按字节读取文件直到字节序列
- java - 为什么我不能直接在子类中使用 Reentrant 类的 getOwner() 方法?
- amazon-web-services - AWS RDS:user/redshift 无权执行:rds:DescribeDBInstances
- javascript - 在 JavaScript 中使用 Kotlin 标准库
- bash - 如何将具有递增数字的文件重命名为具有该数字加 10 的文件
- javascript - 如何更改与其他一组同名的 html 值的值?