如何设置样式<a> tags independently?</a>

时间:2013-06-27 13:59:05

标签: html css css3

我应该如何使锚标记彼此独立,因此改变其中一个的大小不会影响其他人?

6 个答案:

答案 0 :(得分:4)

给他们一个css课程?

<a href='http://google.com' class='class1'>Google</a>
<a href='http://yahoo.com' class='class2'>Yahoo</a>

a.class1{border:1px solid #ff9900;}
a.class2{border:1px solid #ff0099;}

或使用包装元素

<div class='class-a'>
  <a href='http://google.com'>Google</a>
  <a href='http://yahoo.com'>Yahoo</a>
</div>

<div class='class-b'>
  <a href='http://google.com'>Google</a>
  <a href='http://yahoo.com'>Yahoo</a>
</div>

div.class-a a{background-color:red;}
div.class-b a{background-color:blue;}

答案 1 :(得分:3)

你应该给他们个别的CSS类:

在你的CSS中:

a.this { ... }
a.that { ... }

在您的HTML中:

<a href='' class='this'>...</a>
<a href='' class='that'>...</a>

答案 2 :(得分:2)

您可以使用类对锚点进行分类。

CSS:

a.red {
   color: red;
}

a.blue {
   color: blue;
}

HTML:

<a href="#" class="red">Hello!</a>
<a href="#" class="blue">Bye!</a>

您的输出将分别为红色和蓝色。

看看here, on JSFiddle即可玩。

答案 3 :(得分:1)

为他们添加ID或类

HTML:

<a href="..." class="anchor1">Anchor 1</a>
<a href="..." class="anchor2">Anchor 2</a>

的CSS:

.anchor1{
   color: red;
}

.anchor2{
   color: blue;
}

答案 4 :(得分:1)

<a class="someClass">Link</a>

.someClass { /* your styles here */ }

答案 5 :(得分:1)

为了向您的代码(或任何HTML元素)添加不同的CSS样式,请使用ID或CLASS。

请注意一个重要的概念,即ID只能使用一次,而类应该用于同一元素的多个项目,但不是全部,例如: 使用CLASS :(链接1的大小为20,链接2和3的大小为10,而链接3的大小设置为默认大小)

<a href="#" class="anchor1">Link 1</a>
<a href="#" class="anchor2">Link 2</a>
<a href="#" class="anchor2">Link 3</a>
<a href="#" class="anchor3">Link 4</a>

<style>
.anchor1{
   font-size:20px;
}

.anchor2{
   font-size:10px;
}
</style>

使用ID :(链接1的大小为20,链接2的大小为10,而链接3和4的大小设置为默认大小。)

    <a href="#" id="anchor1">Link 1</a>
    <a href="#" id="anchor2">Link 2</a>
    <a href="#" id="anchor3">Link 3</a>
    <a href="#" id="anchor4">Link 4</a>

<style>
#anchor1{
   font-size:20px;
}

#anchor2{
   font-size:10px;
}
</style>

有关何时使用ID与CLASS的更深入说明,请参阅this guide