css - :first-child not selecting first paragraph

标签 css css-selectors

我有两个段落包含在一个 div 中。我想让第一段的文字变大一点,但使用 :first-child 并不能像我所说的那样工作。看不出有什么问题。

        <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 的第一个 child 不是 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;
}

关于css - :first-child not selecting first paragraph,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7961329/

相关文章:

css - 分组 CSS @keyframes 规则

jquery - 如何使用 JQuery 在元素网格下方切换单独的下拉列表?

jquery - 更改事件类(class)的背景颜色

html - 表格单元格高度,img显示: block

internet-explorer - ie8,9 中的 css 选择器问题

html - 仅使用 CSS 的可能解决方案之前的兄弟选择器

css - 为什么嵌套 CSS 选择器是自下而上应用的?

javascript - 在结束 body 标签之前编写 CSS

css - 播放时youtube嵌入视频圆 Angular

Sass 使用与号 (&) 与类型选择器组合父级