简体   繁体   English

在CSS中设置链接的字体颜色

[英]Set font colors in CSS for links

I've been trying to get my navigation bar links to be one color while the main window has links in another color. 我一直试图让我的导航栏链接为一种颜色,而主窗口有另一种颜色的链接。 I'm using a CSS stylesheet and it works but I think my main rule for a is overriding any other rules i have set as the .leftside navigation bar has the wrong color text. 我使用的是CSS样式表和它的作品,但我想我的主规则a是压倒我已经设置为任何其他规则.leftside导航栏有错误的彩色文本。 I even tried the !IMPORTANT but still nothing seems to fix it unless I remove the a rule in the CSS sheet. 我甚至试过了!IMPORTANT但除非我删除了CSS表格中a规则,但似乎仍然无法修复它。 Could someone tell me what I'm doing wrong? 有人能告诉我我做错了什么吗?

HTML: HTML:

 a { text-decoration: none; color: #303030; } .leftside { position: relative; float: left; margin-top: 70px; width: 10%; height: 600px; background-color: #4C4C33; margin-bottom: 10px; color: white; } 
 <div class="leftside"> <a href="gallery.html">Image Gallery</a> </div> 

The text inside .leftside actually has the color white . .leftside里面的文字实际上white But inside .leftside , there is an <a> . .leftside 里面 ,有一个<a> You have defined separate style definitions for <a> , so the color from .leftside is overridden . 您已为<a>定义了单独的样式定义,因此将覆盖 .leftside的颜色。 Overriding is the main idea behind CSS ( Cascading StyleSheet): 覆盖是CSS( Cascading StyleSheet)背后的主要思想:

The rule used is chosen by cascading down from the more general rules to the specific rule required. 使用的规则是通过从更一般的规则级联到所需的特定规则来选择的。 The most specific rule is chosen. 选择最具体的规则。 ( source ) 来源

To fix the problem, you need to assign the color to the <a> inside .leftside , which can be done by using the more specific selector .leftside a : 要解决此问题,您需要将颜色分配给<a> inside .leftside ,这可以通过使用更具体的选择器完成.leftside a

 a { text-decoration: none; color: #303030; } .leftside { position: relative; float: left; margin-top: 70px; width: 10%; height: 600px; background-color: #4C4C33; margin-bottom: 10px; color: white; } .leftside a { color: white; } 
 <div class="leftside"> <a href="gallery.html">Image Gallery</a> </div> 

from your css and html I don't see you setting a specific color for your links within the .leftside div. 从你的CSS和HTML我没有看到你为.leftside div中的链接设置特定的颜色。 Here is an example of how I would set the css for the link (using the ".leftside a" selector): 这是一个如何为链接设置css的示例(使用“.leftside a”选择器):

html HTML

<div class="leftside">
    <!-- navigation link will be red -->
    <a href="gallery.html">Image Gallery</a>
</div>

<!-- non-navigation link will be default color -->
<a href="http://google.com" target="_blank">Visit Google</a>

css CSS

a {
    text-decoration: none;
    color: #303030;
}

.leftside {
    position: relative;
    float: left;
    margin-top: 70px;
    width: 10%;
    height: 600px;
    background-color: #4C4C33;
    margin-bottom: 10px;
    color: white;
}

/* here is the selector for the link within the .leftside */
.leftside a {
    color: red;
}

Additional notes 补充笔记

You can also style links based on state. 您还可以根据州设置链接样式。 so to style your .leftside a links you would do the following (inspired by W3Schools ) 所以要为你的.leftside设置一个链接你会做以下(受到W3Schools的启发)

/* unvisited link */
.leftside a:link {
    color: #FF0000;
}

/* visited link */
.leftside a:visited {
    color: #00FF00;
}

/* mouse over link */
.leftside a:hover {
    color: #FF00FF;
}

/* selected link */
.leftside a:active {
    color: #0000FF;
}

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

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