:第一个孩子没有选择第一段

问题描述:

我有两个段落包在一个div。我想让第一段文字稍微大一点,但是使用:第一段孩子不会像我所说的那样工作。看不出有什么不对。:第一个孩子没有选择第一段

 <div id="main-content"> 
      <h2 class="title"> 
       About Us 
      </h2> 
      <p>Formerly Days Hotel Waterford, Treacys Hotel Spa & Leisure Centre is situated in the heart of Waterford City in Ireland's Sunny South-East, and is proud to be one of the finest hotels in Waterford. The hotel is the perfect choice for your business, leisure and family breaks. Guests can dine in Croker's Restaurant, enjoy a drink with friends in Timbertoes bar, relax in the swimming pool or Jacuzzi at Spirit Leisure Centre or be pampered at Spirit Beauty Spa. 
      </p> 
      <p> 
       The hotel is ideally located on the Quays in Waterford and is just a five minute walk from both the bus and train stations, and only 10km from Waterford Airport. Treacys hotel is one of the finest hotels in Waterford city centre and is a popular location for visitors who love shopping, golf, surfing, or choose from our selection of great value packages, including pampering spa breaks and activity breaks. 
      </p> 
     </div><!-- inner content End --> 

CSS:

#main-content p:first-child { 
font-size:16px; 
} 

#main-content的第一个孩子不是p,这是一个h2

如果您想将规则应用于第一p,使用CSS3 :first-of-type

#main-content p:first-of-type { 
    font-size:16px; 
} 

或者h2:first-child与兄弟选择,它与IE扮演更漂亮:

#main-content h2:first-child + p { 
    font-size:16px; 
} 
+0

谢谢,h2:第一个孩子+ p做的窍门 –

+0

+1'第一类型' – Mohsen

这不是第一个孩子时,h2元素。可以使用#main-content h2 + p。它有很好的浏览器支持。有更好的配合(请参阅BoltClock's answer),但令人遗憾的是IE停止使用它们。

如果您只有h1titlediv,那么你可以避免使用高级CSS选择器来支持更多的浏览器使用此代码:

h2.title + p { 
    font-size:16px; 
}