简体   繁体   English

设置页面一部分的链接样式,而不是另一部分

[英]Styling links for one part of a page but not another

first off I want to say thanks for making me welcome, I've learned more in the last few days on here than I have in the last 6 months offsite, so props! 首先,我想感谢您的到来,这几天来我比过去六个月在异地学习到的更多,所以这里的道具! My question is one that has bothered me for a long time, I can't find an appropriate answer, perhaps I am asking the wrong question though. 我的问题是困扰我很长时间的问题,我找不到合适的答案,也许我是在问错问题。 Basically I have links for my header navigation that I want to customize without affecting the rest of the links on the page. 基本上,我的标题导航有一些链接,我希望在不影响页面上其余链接的情况下自定义这些链接。 I want them to change color on mouse over, etc using purely css without changing the links in the body. 我希望他们使用纯CSS在鼠标悬停时更改颜色,而无需更改主体中的链接。 How do I do this since a:link a:visited, etc seem to be their own class :/ Thanks in advance! 由于a:link a:visited等似乎是他们自己的课程,所以我该怎么做:/预先谢谢!

just use the parent selector, wrap the links you want to edit without changing any else in a wrapper 只需使用父选择器,包装您要编辑的链接,而无需在包装器中更改其他任何链接

html html

<div id="header"><a href="">styled link</a></div>
<a href="">not styled link</a>

css 的CSS

#header a, #header a:active, #header a:link, #header a:visited { color: white }
#header a:hover { color: blue }

also note that the latest versions of firefox & chrome doesn't support :visited pseudoclass due to security issues 还请注意,由于安全问题,最新版本的Firefox和chrome不支持:visited伪类

html: 的HTML:

<a class='fancylink'>Hi</a>
<a>No Fancy Link</a>

css: CSS:

.fancylink{
    color:blue;
    text-decoration:none;
}

.fancylink:hover{
    color:yellow;
}

It's not much of a design but it answers your question. 它不是设计的主要内容,但可以回答您的问题。 =) =)

<div class="firstbit">
   <a href=......

</div>

then 然后

.firstbit a,
.firstbit a:hover
{
   Style stuff here
}

Then do the same but change the name of the class 然后执行相同操作,但更改类的名称

You want to use specificity within CSS. 您想在CSS中使用特异性。 This can be done multiple ways. 这可以通过多种方式完成。

Method 1: Classes 方法1:类

First, add a similar class to all navigation links eg <a href="#" class="classname"></a> 首先,向所有导航链接添加一个类似的类,例如<a href="#" class="classname"></a>

Second, in the CSS, add 其次,在CSS中,添加

a.classname:link {} 
a.classname:visited {} 
/* etc */

Method 2: Container 方法2:容器

First, group all of the navigation links within a tag known as a container eg 首先,将所有导航链接分组在称为容器的标签内

<div id="idname">
<a href="#"></a>
<a href="#"></a>
</div>

Second, in the CSS 二,在CSS中

#idname a:link {}
#idname a:visited{}
/* etc */

CSS selectors have multiple parts, separated by spaces. CSS选择器有多个部分,以空格分隔。 Each of these parts selects from children (or children of children, and so on) of the previous part, or the whole page if it's the last one. 这些部分中的每一个都从上一部分的子项(或子项的子项,依此类推)中选择,如果是最后一部分,则选择整个页面。 (this behavior changes if there's things like > in there, but that's not relevant for this question) (如果里面有>之类的,这种行为就会改变,但这与这个问题无关)

Each of those parts can have multiple things that it uses to filter. 这些部分中的每个部分都可以具有多个用于过滤的内容。 For example, you mentioned a:link . 例如,您提到a:link This is two parts: a , which filters it to a tags, and :link , which filters it to links. 这是两个部分: a ,它过滤到a标签,和:link ,它过滤到链接。 If you put this together, it says " a tags which are links". 如果将它们放在一起,它会显示“链接a标签”。

If you use this with the ID selector ( #id ), you can make something like #header a:link , which will only target a tags which are links in elements which have an id of header . 如果您使用此ID为选择器( #id ),你可以制造类似#header a:link ,这将只针对a标签,它是其中有一个元素链接idheader

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

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