2017-10-19 12 views
0
<div class="icons"> 
<a href="https://www.facebook.com/" class="fa fa-facebook" alt="facebook" title="Facebook"></a> 
<a href="https://twitter.com/" class="fa fa-twitter" alt="twitter" title="Twitter"></a> 
</div> 

それぞれのアイコンをdiv内に別々に配置したいのですが、CSSで別々にアイコンを選択するにはどうすればいいですか? .icons a {はdiv内のすべてのアイコンを編集するので、私は位置を制御できません。CSSでこの特定の行を選択するにはどうすればよいですか?

+1

あなたは要素の直接の子を選択する方法を求めています。私はあなたがこれを初めて知っていることを知っていますので、質問する前に以前の質問を検索してください。 –

+0

別々に配置する場合は、1つのdivにアイコンを置くのはなぜですか? –

答えて

3

あなたが最初の子と最後の子を使用することができます。

.icons a:first-child { color: red; } // just affects facebook-link 
.icons a:last-child { color: blue; } // just affects twitter-link 

つ以上の子供がいたIIFあなたは単にクラスによって直接リンクのスタイルを設定できも:nth-child(x)

使用することができます

.icons a.fa-twitter { } 
+0

良い答えがたくさんありましたが、これは私のために働いていました。 – Stacey

1

pseudo classで試すことができます。

たとえば、あなたの問題を解決する一つの方法は、次のようになります。

.icons a:first-child {}.icons a:last-child {}

2

あなたは、それぞれ独立してアンカータグをターゲットにしようとしている場合(あなたがすでにそこにある便利なクラスを使いたくない場合は)、nth-child疑似セレクタを使用することができます:

.icons > a:nth-child(1){background:red;} 
 
.icons > a:nth-child(2){background:green;}
<link rel="stylesheet" href="https://opensource.keycdn.com/fontawesome/4.7.0/font-awesome.min.css" integrity="sha384-dNpIIXE8U05kAbPhy3G1cz+yZmTzA6CY8Vg/u2L9xRnHjJiAK76m2BIEaSEV+/aU" crossorigin="anonymous"> 
 

 

 
<div class="icons"> 
 
<a href="https://www.facebook.com/" class="fa fa-facebook" alt="facebook" title="Facebook"></a> 
 
<a href="https://twitter.com/" class="fa fa-twitter" alt="twitter" title="Twitter"></a> 
 
</div>

チェックbrowser support for nth-child

[1]:あなたはfontawesome fa-facebookfa-twitterクラスを使用していますが、それらを選択しないでください。

.icons > a.fa-facebook{background:red;} 
 
.icons > a.fa-twitter{background:green;}
<link rel="stylesheet" href="https://opensource.keycdn.com/fontawesome/4.7.0/font-awesome.min.css" integrity="sha384-dNpIIXE8U05kAbPhy3G1cz+yZmTzA6CY8Vg/u2L9xRnHjJiAK76m2BIEaSEV+/aU" crossorigin="anonymous"> 
 

 

 
<div class="icons"> 
 
<a href="https://www.facebook.com/" class="fa fa-facebook" alt="facebook" title="Facebook"></a> 
 
<a href="https://twitter.com/" class="fa fa-twitter" alt="twitter" title="Twitter"></a> 
 
</div>


補遺:他の回答で指摘したようにはい、あなたはfirst-childlast-childを使用することができます。私はもっ​​と多くのアイコンがあるという前提の下で私の答えを書いていました、そして、提示されたコードはちょうど最小の例です。しかし、2つだけ必要な場合は、first-childlast-childを自由に使用してください。あなたが試すことができます

-2

の一つは、jQueryを使用してそれらを反復することです:あなたは以上の2人の子供を持っている場合

$(".icons a").each(function(index, element){ 
    //index is the current element index 
    //element is the a tag 
}); 
+1

彼は、aタグのスタイルを違うものにしたいと考えています。なぜ地獄の人は、世界のすべての解決策としてjQueryを見ているのですか? –

0

あなたは擬似要素:first-child:last-childそしてもちろん:nth-child(x)を利用することができます。参照のために下のスニペットを確認してください。

.icons a:first-child { 
 
    color: red; 
 
} 
 

 
.icons a:nth-child(2) { 
 
    color: green; 
 
} 
 

 
.icons a:nth-child(3) { 
 
    color: cyan; 
 
} 
 

 
.icons a:last-child { 
 
    color: yellow; 
 
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" /> 
 
<div class="icons"> 
 
    <a href="https://www.facebook.com/" class="fa fa-facebook" alt="facebook" title="Facebook"></a> 
 
    <a href="https://twitter.com/" class="fa fa-twitter" alt="twitter" title="Twitter"></a> 
 
    <a href="https://www.facebook.com/" class="fa fa-facebook" alt="facebook" title="Facebook"></a> 
 
    <a href="https://twitter.com/" class="fa fa-twitter" alt="twitter" title="Twitter"></a> 
 
</div>

関連する問題