简体   繁体   English

CSS:为什么我在悬停时会在图像链接上显示红色下划线?

[英]CSS: Why do I have a red underline on image links when hovered?

On my web site, I have the following CSS: 在我的网站上,我有以下CSS:

a:hover{color:red;border-bottom:1px solid}

This displays a red underline on text links when hovered, which is desired. 这在悬停时在文本链接上显示红色下划线,这是期望的。

However, this also displays a red underline on image links when hovered, which is NOT desired. 但是,这也会在悬停时在图像链接上显示红色下划线,这是不希望的。

I only want to display the red underline on text links when hovered but not image links when hovered. 我只希望在悬停时在文本链接上显示红色下划线,但在悬停时不显示图像链接。

Any ideas how I could accomplish this with CSS? 有什么想法我怎么能用CSS完成这个?

You are styling the links with 您正在为链接设置样式

border-bottom:1px solid red

It might be better to instead use 改为使用可能更好

color: red;
text-decoration: underline;

as images cannot be underlined. 因为图像不能加下划线。

Because the style is probably applies to the anchor elements and its applying the style to the image links as well 因为样式可能适用于锚元素,并且它也将样式应用于图像链接

a { border-bottom:1px solid red}

would also apply to 也适用于

<a href="somelink"> <img src="someimage.jpg"> </a>

Seeing as the :hover text colour is almost red, why not just use text-decoration: underline ? 看起来像:悬停文字颜色几乎是红色,为什么不只是使用text-decoration: underline

Failing that, either have a textlink class and set the border property, a.textlink { border-bottom: 1px solid red } , or have an imglink class and clear the border property, a.imglink { border-bottom: none; } 如果没有,要么有textlink类并设置border属性, a.textlink { border-bottom: 1px solid red } ,或者有一个imglink类并清除border属性, a.imglink { border-bottom: none; } a.imglink { border-bottom: none; }

You have two options. 你有两个选择。

The first option is to do a { color: red; text-decoration: underline; } 第一个选择是a { color: red; text-decoration: underline; } a { color: red; text-decoration: underline; } a { color: red; text-decoration: underline; } . a { color: red; text-decoration: underline; } This has the downside of changing the text to a red colour, not just the line. 这具有将文本更改为红色而不仅仅是线条的缺点。 If that's acceptable, this is the easiest way. 如果这是可以接受的,这是最简单的方法。

If you need the text to stay whatever colour it normally is, things are a bit more complicated. 如果你需要文本保持正常的颜色,事情就会复杂一些。 There's no way with CSS to say "apply this rule unless there's an image tag within the link", so you're somewhat stuck. 除非链接中有图像标记, 否则 CSS无法说“应用此规则”,因此您有点卡住了。 What I've always done is manually (or in your CMS, if it's something automatic) add a class to links that contain images - <a href="#" class="imagelink"><img src="#" alt="" /></a> - and remove the border from those. 我一直在做的是手动(或在CMS中,如果它是自动的)将类添加到包含图像的链接 - <a href="#" class="imagelink"><img src="#" alt="" /></a> - 并删除那些边框。

Also note that you'll want to set a one pixel transparent border on non -hovered links, or the page'll jump around a little due to one pixel being added/subtracted to the height of the line containing the link. 另请注意,您需要在悬停链接上设置一个像素透明边框,或者由于一个像素被添加/减少到包含链接的行的高度,页面会跳转一点。

I'm pretty sure that this cannot be done with CSS alone (I'm thinking that maybe some CSS pseudo-selectors can do the trick, only if we are talking about thumbnail images linking to their larger versions) but if you are already using jQuery in your page, it may be done with pretty much one line of code: 我很确定单凭CSS无法做到这一点(我想也许一些CSS伪选择器可以做到这一点,只有当我们讨论链接到更大版本的缩略图时)但是如果你已经在使用在您的页面中使用jQuery,可以使用几行代码完成:

$('a img').each(function(){$(this.parentNode).addClass('image-link')});

and do not forget to add the correspondent class in your CSS: 并且不要忘记在CSS中添加通信类:

a.image-link{ border:none;}

And that's it. 就是这样。

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

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