简体   繁体   English

CSS的第一个孩子第二次不工作

[英]CSS first-child doesn't work for the second time

I have this HTML: 我有这个HTML:

  <div class="row">
         <div id="sidebar" class="column large-2 medium-3">
             <div class="row">
               test
             </div>
         </div>
         <div id="rightside" class="column large-10 medium-9">
             <div class="row">test2</div>
         </div>
    </div>

And this CSS: 而这个CSS:

#rightside:first-child{
 border-bottom:solid 1px @main_color;
text-align:center;

}
#sidebar:first-child{
   border-bottom:solid 1px @main_color;
    text-align:center;
}

I'm using Zurb Foundation 5. The sidebar first-child works, but the one for the #rightside elements does not. 我使用的是Zurb Foundation5。侧边栏的第一个孩子可以工作,但#rightside元素的孩子不能。 Any idea why? 知道为什么吗?

I've inspected the element #rightside and I can't see the CSS selector that I've applied in the inspector in Chrome. 我已经检查了元素#rightside,但是看不到我在Chrome的检查器中应用的CSS选择器。 It seems that it doesn't recognize that selector for some reason. 似乎由于某种原因无法识别该选择器。

I have nothing else in the CSS code, just this. 我在CSS代码中没有别的了,仅此而已。

#rightside div:first-child 
{
  /* styles */
}

#sidebar div:first-child 
{
  /* styles */
}

This way you apply your styles to the first DIV inside of #rightside and #sidebar . 这样,您将样式应用于#rightside#sidebar内的第一个DIV。

Because of comments : this will only work if your first-child is actually a div, if you want to style the first-child regardless of it's type you can use: 由于有注释 :仅当您的第一个孩子实际上是一个div时,这才起作用;如果您要为第一个孩子设置样式,而无论其类型如何,都可以使用:

#sidebar :first-child
{  
  /* styles */
}

For #rightside:first-child to work, the div with ID rightside would need to be the first child of the parent, as the div with ID sidebar is. 对于#rightside:first-child工作, div ID为rightside将需要父母的第一个孩子,因为div ID为sidebar是。

Given that you're using IDs, the div with ID rightside should be the only one in your HTML so the selector you'd use would be simply #rightside . 假设您使用的是ID,则ID rightsidediv应该是HTML中唯一的div ,因此您要使用的选择器将只是#rightside

You need this https://developer.mozilla.org/en-US/docs/Web/CSS/:first-of-type 您需要这个https://developer.mozilla.org/en-US/docs/Web/CSS/:first-of-type

This targets the first type of a particular element, in your case it is a div 这针对特定元素的第一种类型,在您的情况下为div

#rightside div:first-of-type {
    background-color: red;
}

http://jsfiddle.net/w0wprkb0/ http://jsfiddle.net/w0wprkb0/

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

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