简体   繁体   English

如何在悬停时制作虚线下划线链接?

[英]How to make a dotted underline link on hover?

I have a link <a class="link-articles" href="#articles">My Articles</a>我有一个链接<a class="link-articles" href="#articles">My Articles</a>

and my css .link-articles { text-decoration: underline; color: blue; }和我的 css .link-articles { text-decoration: underline; color: blue; } .link-articles { text-decoration: underline; color: blue; }

However, on hover I would like to have instead of blue underline a red underline, but the text should remain blue and only the underscore change color to red.但是,在悬停时,我希望用红色下划线代替蓝色下划线,但文本应保持蓝色,只有下划线将颜色更改为红色。

How to do such thing?这样的事情怎么办?

Since you cannot denote因为你不能表示which color哪种颜色a second color for the text underline, one strategy is to remove it and use a border.文本下划线的第二种颜色,一种策略是将其删除并使用边框。

.link-articles
{
    border-bottom: solid 1px blue;
    text-decoration: none;
}

.link-articles:hover
{
    border-bottom-color: red;
}

Note that if you leave the text-underline , it will shift down when hovering, since it's placement is not exactly the same location as the bottom border.请注意,如果您保留text-underline ,它会在悬停时向下移动,因为它的位置与底部边框的位置不完全相同。

This approach has an added advantage of being able to position the underline by using line-height and have alternate line styles, by replacing solid with dotted or dashed .这种方法有一个额外的优势,即能够 通过使用 line-height 定位下划线,并 通过用dotteddashed替换solid 具有替代线条样式。

Borderless Approach:无边界方法:

As @Pacerier points out in the comments, here is an alternate strategy using pseudo-classes and CSS content ( JSFiddle ):正如@Pacerier 在评论中指出的那样,这是使用伪类和 CSS 内容( JSFiddle )的替代策略:

.link-articles
{
    position: relative;
}

.link-articles[href="#articles"]:after
{
    content: 'My Articles';
}

.link-articles:after
{
    color: red;
    left: 0;
    position: absolute;
    top: 0;
}

However, with anti-aliasing, it may have some color-blending on the text edges.但是,使用抗锯齿功能时,文本边缘可能会出现一些颜色混合。 If you don't like the thought of having to manually put content in your CSS, you could use an attribute or duplicate element.如果您不喜欢必须手动将content放入 CSS 的想法,您可以使用属性或重复元素。

Use border-bottom :使用border-bottom

a:hover.link-articles {border-bottom: 1px dotted red; text-decoration: none;}

See the demo看演示

Use a border:使用边框:

.link-articles { text-decoration: none; border-bottom: blue 1px solid; }
.link-articles:hover { border-bottom: red 1px dotted; }

Demo Fiddle演示小提琴

.link-articles { text-decoration: none; border-bottom: 1px dotted blue; }
.link-articles:hover { text-decoration: none; border-bottom: 1px dotted red; }

Try this:试试这个:

 .link-articles{ text-decoration: none; border-bottom:1px dotted; border-color:blue; }
 .link-articles:hover{ border-color:red; }

DEMO演示

Show bottom border on hover:在悬停时显示底部边框:

a.link-articles {
    text-decoration: none;
    border-bottom: 1px dotted blue;
}
a.link-articles:hover {
    border-bottom: 1px dotted red;
}

The :hover style is used to set style when user places mouse over element. :hover样式用于设置用户将鼠标悬停在元素上时的样式。

.link-articles       { ... }
.link-articles:hover { ... }

And you can use the border-bottom property instead of text-decoration for dotted, dashed and width styling.并且您可以使用border-bottom属性代替text-decoration来设置点、虚线和宽度样式。

You can use the CSS3 text-decoration-color property, but unfortunatly, the text-decoration-color property is not supported in any of the major browsers.您可以使用 CSS3 text-decoration-color属性,但遗憾的是,任何主流浏览器都不支持text-decoration-color属性。

Firefox supports an alternative, the -moz-text-decoration-color property. Firefox 支持另一种选择,即-moz-text-decoration-color属性。

Reference: http://www.w3schools.com/cssref/css3_pr_text-decoration-color.asp参考: http : //www.w3schools.com/cssref/css3_pr_text-decoration-color.asp

Browser Support: http://caniuse.com/#search=text-decoration-color浏览器支持: http : //caniuse.com/#search=text-decoration-color

JSFiddle (Doesn't works on all browsers) JSFiddle (不适用于所有浏览器)

So the best way to do that is still to use the border-bottom css property as a trick.所以最好的方法仍然是使用border-bottom css 属性作为一个技巧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM