CSS 如何单独更改 div 的 a:link 和 a:hover?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4516731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 23:22:31  来源:igfitidea点击:

How to change a:link and a:hover for a div alone?

css

提问by user539493

In my page's body I already have an a:linkand a:hover.

在我的页面正文中,我已经有一个a:linkand a:hover

But now I have another div tag where I want different a:link, a:hoverand a:visited.

但是现在我有另一个 div 标签,我想要不同的a:link,a:hovera:visited.

How do I do this?

我该怎么做呢?

回答by thedom

In CSS:

在 CSS 中:

div.classname a:link {
  color: #123456;
}

div.classname a:hover {
  color: #123;
}

div.classname a:visited {
  color: #654321;
}

In HTML:

在 HTML 中:

<div class="classname">
  <a href="#link">link</a>
</div>

回答by Arun P Johny

Assign a class for the div. something like

为 div 分配一个类。就像是

<div>
    <a></a> //style from a:link and a:hover
</div>
<div class="my-div">
    <a></a> //style from a:link and a:hover overridden by styles from .my-div a:link and .mydiv a:hover
</div>

Style

风格

a:link{} //old link
a:hover{}


.my-div a:link{} //second link
.my-div a:hover{}

You can chain the css selectors to form different combinations of styles

您可以链接 css 选择器以形成不同的样式组合

回答by superUntitled

You could use the child selector method:

您可以使用子选择器方法:

div.one > a { color:blue; }
div.two > a { color:red; }

will work for

会为

<div class=''><a>link</a></div>

but not

但不是

<div class=''><p><a>link</a></p></div>

回答by Vprashant Kumar

You can use !important like this if above code is not working

如果上面的代码不起作用,您可以像这样使用 !important

.classname > a:hover{color:red !important;}
.classname > a:link{color:red !important;}
.classname > a:visited{color:red !important;}