【问题标题】:Issue with CSS :first-of-type :first-letterCSS 问题:first-of-type :first-letter
【发布时间】:2012-12-29 06:39:02
【问题描述】:

我已将我的页面设置为在第一段内容上创建一个大字首字母。这可以按预期工作,但是当我在受影响的元素之前有另一个具有指定类的 div 时,它会使首字下沉消失。

查看示例...
正确显示:http://jsfiddle.net/KPqgQ/
显示不正确:http://jsfiddle.net/d73u9/

我的页面中有以下代码:

<section id="review">
  <div class="content_right"></div>
  <div class="content_left">
    <p>text</p>
    <p>text</p>
  </div>
  <div class="content_left">
    <p>text</p>
    <p>text</p>
  </div>
</section>

使用以下 CSS:

   #review .content_left:first-of-type p:first-of-type{
        padding-top:10px;
    }

    #review .content_left:first-of-type p:first-of-type:first-letter{
        float: left;
        line-height:90%;
        padding-right: 15px;
        padding-bottom: 10px;

        font-family: "Georgia",_serif;
        font-weight: bold;
        font-size: 60px;
        color:black;
    }

创建一个大写首字母。

当我删除“content_right” div 时,此代码有效,但在那里,CSS 不起作用。我以为我有关于声明的正确顺序,但我一定遗漏了一些东西。

有人知道为什么这不能按预期工作吗?

【问题讨论】:

    标签: css css-selectors


    【解决方案1】:

    这是按预期工作的,因为 :first-of-type 选择器实际上选择了特定类型元素(section、div、span 等)中的第一个,而实际上并没有选择类的第一个类型。

    替代方案

    “~”修复

    提到了波浪号“~”解决方法here

    /* Targets all paragraphs */
    p 
    {
            display: block;
            clear: both;
    }
    
    /* Targets all paragraphs within divs in #review */
    #review div p
    { 
            padding-top:10px;
    }
    
    /* Targets the first letter within each paragraph */
    #review div p:first-letter{
            float: left;
            line-height:90%;
            padding-right: 15px;
            padding-bottom: 10px;
            font-family: "Georgia",_serif;
            font-weight: bold;
            font-size: 60px;
            color:black;
    }    
    
    /* Removes the padding from all except the first paragraphs */
    #review > div ~ div > p ~ p, 
    #review > .content_left ~ .content_left p:first-of-type 
    {
            padding-top: 0px;  
    }
    
    /* Removes the first letter stylings of the non-first paragraphs within 
       .content_left classes */
    #review > .content_left ~ .content_left p:first-of-type:first-letter, 
    #review > div ~ div > p ~ p:first-letter 
    {
            float: none;
            line-height:90%;
            padding-right: 0px;
            padding-bottom: 0px;
            font-family: "Georgia",_serif;
            font-weight: normal;
            font-size: 12px; 
    }
    

    Example using '~' Method

    (感谢 BoltClock 帮我解决了这个问题,毕竟这是他的方法。)


    切换 div 顺序

    .content_right div 移到 .content_left 部分后面,因为它们是浮动的,您仍然可以保持相同的布局。

    代码:

    <section id="review">
        <div class="content_left">
            <p>text</p>
            <p>text</p>
         </div>
         <div class="content_left">
            <p>text</p>
            <p>text</p>
         </div>
         <div class="content_right"></div>
    </section>​ 
    

    Example of switching order:


    使用 :nth-of-type 选择器

    使用:nth-of-type 选择器选择合适的div。

    代码:

    #review .content_left:nth-of-type(2) :first-child
    {
        padding-top:10px;
    }
    
    #review .content_left:nth-of-type(2) :first-child:first-letter
    {
        float: left;
        line-height:90%;
        padding-right: 15px;
        padding-bottom: 10px;
        font-family: "Georgia",_serif;
        font-weight: bold;
        font-size: 60px;
        color:black;
    }  
    

    Example using :nth-of-type

    【讨论】:

    • 您可能想提供一个示例,说明如何在此处应用我的~ 解决方案:)
    • @BoltClock,我开始制作一个,但根本没有时间制作一个,至少没有我想花的时间 :)
    • 好吧,我总是可以把一个放在一起,然后分开发布。虽然,似乎有很多规则需要撤消,这会很痛苦......
    • 目前正在处理一个 - 我觉得我很接近。我选择了第一个,但第二课的第一段仍然受到打击。 jsfiddle.net/d73u9/4
    • 啊,p 不必重复,因为元素类型是p,这意味着:first-of-type:first-letter 将按预期工作。您需要使用选择器.content_left ~ .content_left p:first-of-type:first-letter 进行覆盖,这是繁琐的部分,因为有许多声明需要覆盖。这里已经很晚了,所以我现在不想胡思乱想:P
    【解决方案2】:

    这是我对~ 解决方法的看法。我想我会加入,因为 Rion Williams 很友好地链接到我对 his answer 的解决方法的描述(这也很好地解释了为什么你的代码不能按预期工作):

    /* The first p in all .content-left elements */
    #review .content_left p:first-of-type {
        padding-top: 10px;
    }
    
    /* 
     * Undo the above style for the first p in subsequent .content-left elements.
     */
    #review .content_left ~ .content_left p:first-of-type {
        padding-top: 0px;
    }
    
    /* The first letter in the first p in all .content-left elements */
    #review .content_left p:first-of-type:first-letter {
        float: left;
        line-height: 90%;
        padding-right: 15px;
        padding-bottom: 10px;
        font-family: "Georgia", _serif;
        font-weight: bold;
        font-size: 60px;
        color: black;
    }
    
    /* 
     * Undo the above styles for the first letter in the first p
     * in subsequent .content-left elements.
     * 
     * I'm just using the initial values for every property here; you'll want to
     * adjust their values as necessary.
     */
    #review .content_left ~ .content_left p:first-of-type:first-letter {
        float: none;
        line-height: normal;
        padding-right: 0px;
        padding-bottom: 0px;
        font-family: inherit;
        font-weight: normal;
        font-size: medium;
        color: inherit;
    }
    

    jsFiddle preview

    根据您的布局,您可能还需要对某些其他类重复此操作。不幸的是,由于 CSS 还没有提供匹配某个类的第一个元素的选择器,所以您现在只能使用覆盖规则。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      • 2011-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-30
      相关资源
      最近更新 更多