CSS/HTML 中几种不同的鼠标悬停字体颜色变化

发布于 2024-11-30 23:19:11 字数 388 浏览 1 评论 0原文

等待!这不仅仅是另一个“当鼠标悬停在图像/文本/等上时如何更改字体颜色?”

我认为我可以将其作为一种黑客手段(通过内联每段文本的样式),但我听说样式应该与内容分开。我正在与我那老套的程序员路线作斗争,并努力变得更加自律。所以我来这里是为了寻求最好的方法来做到这一点。

我在我制作的网站上有一个简单的链接菜单,我希望它们是黑色字体(当鼠标没有悬停时),然后在鼠标悬停时更改为颜色。但我希望当鼠标悬停在其上时,每个都变成不同的颜色

我应该为每种颜色命名一个样式,然后在使用每种颜色时声明一个 div 吗?这看起来与内联样式几乎相同。

我非常感谢您关于如何以正确的方式做到这一点的明智建议!

Wait! This isn't just another "how do you change the font color when the mouse hovers over an image/text/etc.?"

I think I can do this as a hack (by inlining the style for each piece of text), but I've heard that style should be separate from content. I'm fighting my hacky programmer routes and trying to become more disciplined. So I'm here to ask for the best way to do this.

I have a simple menu of links on a website I've made, and I'd like them to be black font (when the mouse isn't hovering over) and then change to a color when the mouse hovers. But I'd like each to turn a different color when the mouse hovers over it.

Should I name a style for each of these colors and then declare a div when each is used? That seems almost the same as inlining the style.

I'd really appreciate your sage advice on how to do this the right way!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故事灯 2024-12-07 23:19:11

这一切都使用 CSS 完成 - 只需给它们不同的类名和 :hover 状态即可。使用 JavaScript 交换类名,以便在每次鼠标悬停事件后产生不同的颜色。

html:

<a href="..." class="c1">MENU TEXT</a>

css:

.c1 { background-color:#003300 }
.c1:hover { background-color:#006600 }
.c2 { background-color:#003300 }
.c2:hover { background-color:#006600 }
.....

Do it all using CSS - just give them different class names and :hover states. Swap class names using JavaScript to make different colours after each mouseover event.

html:

<a href="..." class="c1">MENU TEXT</a>

css:

.c1 { background-color:#003300 }
.c1:hover { background-color:#006600 }
.c2 { background-color:#003300 }
.c2:hover { background-color:#006600 }
.....
十年不长 2024-12-07 23:19:11

CSS:

<style type="text/css">
    a { color: black; }
    a:hover { color: grey; }
    a.green:hover { color: green; }
    a.red:hover { color: red; }
    a.blue:hover { color: blue; }
    .content a:hover, .content a.green:hover, .content a.red:hover, .content a.blue:hover { color: yellow; }
</style>

HTML:

<a href="#" class="green">green link</a>
<a href="#" class="red">red link<a>
<a href="#" class="blue">blue link</a>
<a href="#" >grey link</a>

<div class="content">
    <a href="#" class="green">yellow link</a>
    <a href="#" class="red">yellow link<a>
    <a href="#" class="blue">yellow link</a>
    <a href="#" >yellow link</a>
</div>

CSS:

<style type="text/css">
    a { color: black; }
    a:hover { color: grey; }
    a.green:hover { color: green; }
    a.red:hover { color: red; }
    a.blue:hover { color: blue; }
    .content a:hover, .content a.green:hover, .content a.red:hover, .content a.blue:hover { color: yellow; }
</style>

HTML:

<a href="#" class="green">green link</a>
<a href="#" class="red">red link<a>
<a href="#" class="blue">blue link</a>
<a href="#" >grey link</a>

<div class="content">
    <a href="#" class="green">yellow link</a>
    <a href="#" class="red">yellow link<a>
    <a href="#" class="blue">yellow link</a>
    <a href="#" >yellow link</a>
</div>
﹉夏雨初晴づ 2024-12-07 23:19:11

你可以使用 CSS 的 nth-child 选择器,下面是一个例子:

#yourMenu ul li:nth-child(1) {
  color: #ff0000;
}
#yourMenu ul li:nth-child(2) {
  color: #00ff00;
}
#yourMenu ul li:nth-child(3) {
  color: #0000ff;
}
...
..

You can use CSS's nth-child selector, here's an example:

#yourMenu ul li:nth-child(1) {
  color: #ff0000;
}
#yourMenu ul li:nth-child(2) {
  color: #00ff00;
}
#yourMenu ul li:nth-child(3) {
  color: #0000ff;
}
...
..
北方的巷 2024-12-07 23:19:11

您只需为所有不同的链接/li提供不同的类即可做到这一点。

HTML

<ul class="menu">
  <li class="home">Home</li>
  <li class="contact">Contact</li>
  <li class="help">Help</li>
</ul>

CSS

ul.menu li a { color: black; }
ul.menu li.home:hover a { color: #000000; }
ul.menu li.contact:hover a { color: #ffffff; }
ul.menu li.help:hover a { color: #ff3300; }

这不是最好的方法:最好的方法是使用 CSS 伪类选择器:nth-​​child。

http://reference.sitepoint.com/css/pseudoclass-nthchild

然而惊喜惊喜IE不支持。较旧的浏览器也不支持它。

要查看哪些浏览器支持它,请参阅:

http://caniuse.com/#search=nth

You just have to give all the different links / lis a different class to be able to do this.

The HTML

<ul class="menu">
  <li class="home">Home</li>
  <li class="contact">Contact</li>
  <li class="help">Help</li>
</ul>

The CSS

ul.menu li a { color: black; }
ul.menu li.home:hover a { color: #000000; }
ul.menu li.contact:hover a { color: #ffffff; }
ul.menu li.help:hover a { color: #ff3300; }

It isn't the best way: the best way would be to use the CSS pseudo-class selector: nth-child.

http://reference.sitepoint.com/css/pseudoclass-nthchild

However surprise surprise IE doesn't support it. Also older browsers won't support it.

To check out what browsers support it see:

http://caniuse.com/#search=nth

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文