CSS/HTML 中几种不同的鼠标悬停字体颜色变化
等待!这不仅仅是另一个“当鼠标悬停在图像/文本/等上时如何更改字体颜色?”
我认为我可以将其作为一种黑客手段(通过内联每段文本的样式),但我听说样式应该与内容分开。我正在与我那老套的程序员路线作斗争,并努力变得更加自律。所以我来这里是为了寻求最好的方法来做到这一点。
我在我制作的网站上有一个简单的链接菜单,我希望它们是黑色字体(当鼠标没有悬停时),然后在鼠标悬停时更改为颜色。但我希望当鼠标悬停在其上时,每个都变成不同的颜色。
我应该为每种颜色命名一个样式,然后在使用每种颜色时声明一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这一切都使用 CSS 完成 - 只需给它们不同的类名和
:hover
状态即可。使用 JavaScript 交换类名,以便在每次鼠标悬停事件后产生不同的颜色。html:
css:
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:
css:
CSS:
HTML:
CSS:
HTML:
你可以使用 CSS 的 nth-child 选择器,下面是一个例子:
You can use CSS's nth-child selector, here's an example:
您只需为所有不同的链接/
li
提供不同的类即可做到这一点。HTML
CSS
这不是最好的方法:最好的方法是使用 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 /
li
s a different class to be able to do this.The HTML
The CSS
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