首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CSS的问题:第一个类型:第一个字母

问CSS的问题:第一个类型:第一个字母
EN

Stack Overflow用户
提问于 2012-12-29 04:18:57
回答 2查看 6.6K关注 0票数 4

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

请参阅示例...

正确显示:http://jsfiddle.net/KPqgQ/

错误显示:http://jsfiddle.net/d73u9/

我的页面中有以下代码:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
   #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就不起作用了。我认为我有关于声明的正确顺序,但我肯定遗漏了什么。

有没有人知道为什么它不能像预期的那样工作?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-29 04:39:30

当: first - of -type选择器实际选择特定类型的元素(section、div、span等)中的第一个时,这就像预期的那样工作。并且实际上不会选择类类型中的第一个。

替代方案

修复'~‘

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

代码语言:javascript
复制
/* 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; 
}

(感谢BoltClock,你帮了我大忙,毕竟这是他的方法。)

切换div顺序

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

代码:

代码语言:javascript
复制
<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>​ 

使用:n类型选择器的

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

代码:

代码语言:javascript
复制
#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;
}  
票数 6
EN

Stack Overflow用户

发布于 2012-12-29 06:36:04

这是我对~变通方法的看法。我想我应该加入进来,因为Rion Williams很友好地链接到了我对his answer的变通方法的描述(它也很好地解释了为什么你的代码不能像预期的那样工作):

代码语言:javascript
复制
/* 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还没有提供与某个类的第一个元素相匹配的选择器,您现在只能凑合使用覆盖规则。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14075274

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档