简体   繁体   English

如何使用 CSS 和 HTML 更改 hover 上的链接颜色和效果?

[英]How to change color of link and effect on hover using CSS and HTML?

I am trying to replicate the "AC Wall Plug Adapter Recall Program" text header on this apple clone website , its the blue text located right above the footer.我正在尝试在这个苹果克隆网站上复制“AC Wall Plug Adapter Recall Program”文本 header,它的蓝色文本位于页脚正上方。 I have succeeded in centering the text, but I need some help in making the text link blue and when I hover my mouse over the text, it should become underlined.我已经成功地将文本居中,但是我需要一些帮助来使文本链接变为蓝色,当我将鼠标 hover 放在文本上时,它应该变成下划线。 I know it must be the CSS :hover selector, but I don't know how to place it within my CSS.我知道它一定是 CSS :hover选择器,但我不知道如何将它放在我的 CSS 中。 Also, I am a amateur at coding, so I would really appreciate it if you could explain my mistakes instead of just giving me the answer, thank you.另外,我是编码的业余爱好者,所以如果你能解释我的错误而不是只给我答案,我将不胜感激,谢谢。

 .ac-pro { text-align: center; color: blue; } a.ac-pro { color: blue; }
 <div class="container"> <h2 class="ac-pro"><a href="#">AC Wall Plug Adapter Recall Program ></a></h2> </div>

I believe that you are trying to change the colour of the link when it's hovered over.我相信您正在尝试更改悬停时链接的颜色。

The CSS you are currently using isn't quite right.您当前使用的 CSS 不太对。 There is no a.ac-pro because the class ac-pro is set on the h2 .没有a.ac-pro因为 class ac-pro设置在h2上。

Once way to resolve this is to fix the CSS so that it applies to the a underneath h2.ac-pro :解决此问题的方法之一是修复 CSS 以使其适用于h2.ac-pro下面的a

h2.ac-pro > a {

Then simply add a new CSS style for the hover:然后只需为 hover 添加一个新的 CSS 样式:

h2.ac-pro > a:hover {

The snippet shows this in action:该片段显示了这一点:

 .ac-pro { text-align: center; color: blue; } h2.ac-pro > a { color: blue; } h2.ac-pro > a:hover { color: black; }
 <div class="container"> <h2 class="ac-pro"><a href="#">AC Wall Plug Adapter Recall Program ></a></h2> </div>

    .ac-pro {
  text-align: center;
  color: blue;
}

  .ac-pro a {
  color: blue;
text-decoration : none;

}
.ac-pro a:hover {
  color: red;

}

you were not targetting the class properly try this it will work.您没有针对 class 正确尝试此操作。

you are not applying class and tags properly.您没有正确应用 class 和标签。 please try this.请试试这个。 this will help you in making text-line blue and when you hover it it will become underline.这将帮助您将文本行设为蓝色,当您使用 hover 时,它将变为下划线。

code:代码:

<div class="container">
<h2 class="ac-pro"><a href="#">AC Wall Plug Adapter Recall Program ></a></h2>
</div>

.ac-pro {
text-align: center;
color: blue;
}

.ac-pro > a{
text-decoration: none;
}

.ac-pro > a:hover {
 text-decoration: underline;
 }

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

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