如何将焦点添加到没有 href 的锚点?
How to add focus to an anchor that doesn't have an href?
我的网站很大,有数百个没有附加 href
属性的锚标记。与其采取 search/replace 并添加 href="#"
的冒险步骤,我更愿意使用 CSS 来定位他们。我正在尝试对这些应用焦点状态,但事实证明这很困难。我正在使用以下 :not([href])
来定位那些非 href 链接,但我似乎无法专注于它。
/* Duru Sans */
@import url("https://fonts.googleapis.com/css2?family=Duru+Sans&display=swap");
/*resets*/
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html,
body {
font-family: "Duru Sans", sans-serif;
font-size: 16px;
}
.container {
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: center;
height: 60vh;
}
a {
text-decoration: none;
}
a.custom-bttn-anim:not([href]), a.custom-bttn-anim {
align-items: center;
border: 0;
border-radius: 4px;
box-shadow: 0px 12px 30px rgba(0, 0, 0, 0.12);
cursor: pointer;
display: inline-flex;
font-size: 1.062rem;
font-weight: 700;
justify-content: center;
min-width: 185px;
outline: 0;
padding: 1.25rem 0;
position: relative;
text-transform: none;
transition: all 0.2s ease;
}
a.custom-bttn-anim:not([href]):hover, a.custom-bttn-anim:hover {
box-shadow: 0px 8px 12px rgba(0, 0, 0, 0.08);
}
a.custom-bttn-anim:not([href]):before, a.custom-bttn-anim:before {
content: "";
position: absolute;
border: solid 0px #005fec;
top: 0px;
bottom: 0px;
left: 0px;
right: 0px;
opacity: 0;
transition: all 0.2s ease;
border-radius: 4px;
filter: blur(4px);
}
a.custom-bttn-anim:not([href]):focus-visible:before, a.custom-bttn-anim:focus-visible:before {
content: "";
position: absolute;
border: solid 2px #005fec;
top: -8px;
bottom: -8px;
left: -8px;
right: -8px;
opacity: 1;
filter: blur(0px);
}
a.custom-blue-bttn {
background: #005fec;
color: white;
}
a.custom-bttn-anim .bttn-txt {
color: inherit;
font-weight: inherit;
display: inline-block;
transform: translateX(0.625rem);
transition: transform 0.2s;
}
a.custom-bttn-anim .learn-more-arrow {
height: 0.75rem;
opacity: 0;
transition: opacity 0.2s, transform 0.2s;
}
a.custom-bttn-anim:hover .bttn-txt {
transform: translate(0px);
transition: transform 0.2s;
margin-right: 0.3rem;
}
a.custom-bttn-anim:hover .learn-more-arrow {
opacity: 1;
transition: opacity 0.2s, transform 0.2s;
}
<div class="container">
<a class="custom-bttn-anim custom-blue-bttn light" href="#">
<span class="bttn-txt">Small business</span>
<img src="https://nextivaweb.imgix.net/icons/Arrow-Right-Hover-Animation_white.svg" alt="right arrow icon" class="learn-more-arrow">
</a>
<a class="custom-bttn-anim custom-blue-bttn light">
<span class="bttn-txt">Small business</span>
<img src="https://nextivaweb.imgix.net/icons/Arrow-Right-Hover-Animation_white.svg" alt="right arrow icon" class="learn-more-arrow">
</a>
</div>
没有 href
属性的锚元素在默认情况下不可聚焦。
您可以使用 tabindex
属性使它们可聚焦,但我不确定这是否比添加 href="#"
.
对您更好
示例:
<a tabindex="0">
<!-- other stuff -->
</a>
您甚至可以使用一些 JavaScript 来动态添加属性:
document.querySelectorAll("a:not(a[href])").forEach(element => {
element.setAttribute("tabindex", "0")
});
如果空锚标记不导航到 URI,则此配置可能会使使用辅助技术的人感到困惑,带有附加事件处理程序的按钮可能是更合适的选择。
我的网站很大,有数百个没有附加 href
属性的锚标记。与其采取 search/replace 并添加 href="#"
的冒险步骤,我更愿意使用 CSS 来定位他们。我正在尝试对这些应用焦点状态,但事实证明这很困难。我正在使用以下 :not([href])
来定位那些非 href 链接,但我似乎无法专注于它。
/* Duru Sans */
@import url("https://fonts.googleapis.com/css2?family=Duru+Sans&display=swap");
/*resets*/
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html,
body {
font-family: "Duru Sans", sans-serif;
font-size: 16px;
}
.container {
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: center;
height: 60vh;
}
a {
text-decoration: none;
}
a.custom-bttn-anim:not([href]), a.custom-bttn-anim {
align-items: center;
border: 0;
border-radius: 4px;
box-shadow: 0px 12px 30px rgba(0, 0, 0, 0.12);
cursor: pointer;
display: inline-flex;
font-size: 1.062rem;
font-weight: 700;
justify-content: center;
min-width: 185px;
outline: 0;
padding: 1.25rem 0;
position: relative;
text-transform: none;
transition: all 0.2s ease;
}
a.custom-bttn-anim:not([href]):hover, a.custom-bttn-anim:hover {
box-shadow: 0px 8px 12px rgba(0, 0, 0, 0.08);
}
a.custom-bttn-anim:not([href]):before, a.custom-bttn-anim:before {
content: "";
position: absolute;
border: solid 0px #005fec;
top: 0px;
bottom: 0px;
left: 0px;
right: 0px;
opacity: 0;
transition: all 0.2s ease;
border-radius: 4px;
filter: blur(4px);
}
a.custom-bttn-anim:not([href]):focus-visible:before, a.custom-bttn-anim:focus-visible:before {
content: "";
position: absolute;
border: solid 2px #005fec;
top: -8px;
bottom: -8px;
left: -8px;
right: -8px;
opacity: 1;
filter: blur(0px);
}
a.custom-blue-bttn {
background: #005fec;
color: white;
}
a.custom-bttn-anim .bttn-txt {
color: inherit;
font-weight: inherit;
display: inline-block;
transform: translateX(0.625rem);
transition: transform 0.2s;
}
a.custom-bttn-anim .learn-more-arrow {
height: 0.75rem;
opacity: 0;
transition: opacity 0.2s, transform 0.2s;
}
a.custom-bttn-anim:hover .bttn-txt {
transform: translate(0px);
transition: transform 0.2s;
margin-right: 0.3rem;
}
a.custom-bttn-anim:hover .learn-more-arrow {
opacity: 1;
transition: opacity 0.2s, transform 0.2s;
}
<div class="container">
<a class="custom-bttn-anim custom-blue-bttn light" href="#">
<span class="bttn-txt">Small business</span>
<img src="https://nextivaweb.imgix.net/icons/Arrow-Right-Hover-Animation_white.svg" alt="right arrow icon" class="learn-more-arrow">
</a>
<a class="custom-bttn-anim custom-blue-bttn light">
<span class="bttn-txt">Small business</span>
<img src="https://nextivaweb.imgix.net/icons/Arrow-Right-Hover-Animation_white.svg" alt="right arrow icon" class="learn-more-arrow">
</a>
</div>
没有 href
属性的锚元素在默认情况下不可聚焦。
您可以使用 tabindex
属性使它们可聚焦,但我不确定这是否比添加 href="#"
.
示例:
<a tabindex="0">
<!-- other stuff -->
</a>
您甚至可以使用一些 JavaScript 来动态添加属性:
document.querySelectorAll("a:not(a[href])").forEach(element => {
element.setAttribute("tabindex", "0")
});
如果空锚标记不导航到 URI,则此配置可能会使使用辅助技术的人感到困惑,带有附加事件处理程序的按钮可能是更合适的选择。