CSS 在 li 标签内设置 <a> 的字体颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1585855/
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 21:10:14  来源:igfitidea点击:

setting font color of <a> inside a li tag

css

提问by mrblah

My markup looks like:

我的标记看起来像:

<div class="c1">
      <li class="c2"><a href="">blah</a></li>
</div>

I want the text blah to be red.

我希望文本 blah 是红色的。

c1 is used other places, so I want to drill down as much as possible without affecting other markup.

c1 是用在其他地方,所以我想在不影响其他标记的情况下尽可能地向下钻取。

回答by Espo

Use this style definition in your css-file:

在您的 css 文件中使用此样式定义:

div.c1 li.c2 a {
  color: red;
}

PS: Having your <li>tag inside your <div>-tag without an <ul>-tag is not recommended.

PS:不建议在没有-tag 的情况下将<li>标签放在<div>-tag 中<ul>

回答by Pointy

<style>
  div.c1 li.c2 a { color: red; }
</style>

回答by rahul

<div class="c1">
      <li class="c2"><a href="">blah</a></li>
</div>
<style>
  div.c1 li.c2 a { color: red; }
</style>

回答by ndim

The most specific CSS selectors would probably be

最具体的 CSS 选择器可能是

div.c1 > li.c2 > a:link,
div.c1 > li.c2 > a:active,
div.c1 > li.c2 > a:hover,
div.c1 > li.c2 > a:visited {
    color: red;
}

The more specific the CSS selectors, the less work for the browser's rendering engine.

CSS 选择器越具体,浏览器渲染引擎的工作就越少。

However, something is wrong with your markup if this is supposed to be HTML and the <li> element's parent is a <div> instead of <ol> or <ul>.

但是,如果这应该是 HTML 并且 <li> 元素的父元素是 <div> 而不是 <ol> 或 <ul>,那么您的标记就会出现问题。

回答by SLaks

Use the following rule:

使用以下规则:

div.c1 li.c2 a {
    color: red;
}

This matches atags inside of litags with class c2inside of divtags with class c1.

这个比赛a的内部标签li与标签类c2里面div有类标签c1

For added uniqueness, you might want to give the atag its own class name.

为了增加唯一性,您可能希望为a标记提供自己的类名。

Also, litags should only appear inside of list tags. (ulor ol).
Did you mean <li class="c1">?

此外,li标签应该只出现在列表标签​​内。(ulol)。
你的意思是<li class="c1">

回答by vikingosegundo

.c1 .c2 a {
  color: red;
}

回答by vikingosegundo

.c2 a
{
     color: red;
}

回答by NawaMan

The following code will do (very specific).

以下代码将执行(非常具体)。

.c1 .c2 a { color: red; }

回答by Nick Schreuder

First choose the div you want to change which is .c2 inside this is a link which should be selected like

首先选择要更改的 div 里面的 .c2 这是一个应该选择的链接

.c2 a { color:red }