【问题标题】:How do I go about targeting the literal "first-child" of a div, styling depending on element type如何定位 div 的字面“第一个孩子”,样式取决于元素类型
【发布时间】:2015-02-09 22:41:29
【问题描述】:

我正在为网站创建自定义 CSS,但遇到了有关将 CSS 应用于 div 的第一个子项的问题。我正在尝试仅对实际的第一个子元素设置特殊的 CSS 样式,具体取决于它的元素类型,并且样式特定于该元素的类型。因此,只有当文章的第一个元素(在 div 内)是图像时,它才会忽略文章边距。如果文章的第一个元素是一个段落,它会创建一个额外的 margin-top。任何不是字面上第一个(非 div)元素的东西都不应该应用任何第一个子规则。

我想尽可能只使用 CSS,因为我只能将 CSS 应用到现有的 HTML。

这可能令人困惑。这个jsfiddle 使用与我的html 非常相似的设置,并且我创建了一个显示我的错误的CSS。我知道我没有选择正确嵌套的 div,我只是不确定如何实现我想要的。在小提琴中,不同元素类型之后的每个元素都重新应用了第一个子规则。这又是因为我不确定要引用哪个 div。

这是我的代码:

HTML:

<article>
<div class="body entry-content">
    <div class="sqs-layout sqs-grid-12 columns-12">
        <div class="col sqs-col-12 span-12">

            <div class="sqs-block image-block sqs-block-image">
                <div class="sqs-block-content">
                    <div class="image-block-outer-wrapper">
                        <div class="intrinsic">
                            <div class="image-block-wrapper">
                                <img src="http://media.npr.org/images/picture-show-flickr-promo.jpg" alt="image"></img>


            <div class="sqs-block markdown-block sqs-block-markdown">
                <div class="sqs-block-content">
                    <p>p1</p>
                    <p>p2</p>
                    <p>p3</p>
                </div>
            </div>
                                </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="sqs-block markdown-block sqs-block-markdown">
                <div class="sqs-block-content">
                    <p>p1</p>
                    <p>p2</p>
                    <p>p3</p>
                </div>
            </div>

            <div class="sqs-block image-block sqs-block-image">
                <div class="sqs-block-content">
                    <div class="image-block-outer-wrapper layout-caption-hidden">
                        <div class="intrinsic">
                            <div class="image-block-wrapper has-aspect-ratio">
                                <img src="http://media.npr.org/images/picture-show-flickr-promo.jpg" alt="image"></img>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="sqs-block markdown-block sqs-block-markdown">
                <div class="sqs-block-content">
                    <p>p1</p>
                    <p>p2</p>
                    <p>p3</p>
                </div>
            </div>

        </div>
    </div>
</div>

CSS:

    article {  
background-color:red;
padding:14px;
}
.body.entry-content {
padding:0px;
background-color:grey;
}
.body.entry-content p, img {
padding:0px;
margin:0px;
margin-bottom:1em;
}
.body.entry-content p:first-child{
    /* Only the first paragraph in an article confines to red margine and creates an extra margin-top to space the content further from the title (not seen here). */
margin-top:1em;
background-color:blue;
}
.body.entry-content img:first-child {
/* Only the first image in an article ignores all red margins, filling the article.*/
margin:-14px -14px 0px -14px;
}

【问题讨论】:

  • 文章在上面的标记中没有边距,只是填充。它通常也有边距吗?
  • 哦,那是我的错。它应该说红色填充。我相信,jsfiddle 代码是正确编写的。

标签: html css css-selectors


【解决方案1】:

有可能,但所有嵌套的 div 让它变得非常丑陋:

.body.entry-content div:first-child div:first-child div:first-child .sqs-block-content > p:first-child{
    background-color:blue;
}
.body.entry-content div:first-child div:first-child div:first-child .sqs-block-content div img:first-child {
    margin:-14px -14px 0px -14px;
}

Fiddle here.

【讨论】:

  • 这实际上在我的示例中有效,所以这太棒了。你是对的,尽管 div 让它变得丑陋。不幸的是,我无法访问 html 本身。事实证明,我网站的 CMS 生成了不同格式的不同“文章”,因此这个嵌套 div 列表仅适用于几种类型的条目。我需要在“.body.entry-content”中搜索包含

    first-child 的第一个 div 并应用我的规则。否则,我将不得不专门针对数十种不同“条目类型”中的正确 div。还是谢谢!

【解决方案2】:

要引用 img,这对我有用 - 我认为这个选择器对你来说已经足够具体了:

        .body.entry-content .sqs-layout div.image-block:first-child img {
            /* Only the first image in an article ignores all red margins, filling the article.*/
            margin: -14px -14px 0px -14px;
        }

【讨论】:

  • 我可能犯了一个错误,但是当我使用这个时,第二个图像也使用了第一个子代码。
猜你喜欢
  • 1970-01-01
  • 2021-05-18
  • 1970-01-01
  • 2021-01-23
  • 1970-01-01
  • 2019-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多