3

我想要做的是在 header1 元素之后的最后一个元素(例如段落<p>或无序列表),该元素具有一个类,该类被称为仅对具有类 specialInstructionsNotes的最后一个元素应用额外的填充(30px) 。<ul><h1>specialInstructionsNotes

我尝试合并使用:first-of-type:nth-of-type(1)CSS3 选择器,但这些选择器并没有转移到 IE8(这是我要使用的最低 IE 兼容浏览器)。我的目标是能够在所有最新的浏览器和 IE8+ 中显示CSS 设置(即将填充应用于带有 class 的最后一个元素)。specialInstructionsNotes

注意:可以有 3 个或更多元素应用了这个类,只要这个类的最后一个元素.specialInstructionNotes在最底部有填充。很抱歉有任何混淆。

这是我到目前为止的完整 CSS:

.specialInstructionsNotes {
    color: #004abc;
    font-size: 1.1em;
    line-height: 26px;
    padding: 0 3%;
}    

h1 + p.specialInstructionsNotes + *, 
    h1 + ul.specialInstructionsNotes + *, 
    h1 + p.specialInstructionsNotes:nth-of-type(1), 
    h1 + ul.specialInstructionsNotes:nth-of-type(1) { 
        padding: 0 0 30px; 
    }

h1 + p.specialInstructionsNotes:first-of-type, 
h1 + ul.specialInstructionsNotes:first-of-type { 
    padding: 0; 
}

对于 HTML,元素位于一个 div 中,并且是 div 中包含的最顶层元素。这是我目前遇到的一些页面/场景的 HTML。

第 1 页:

<div class="span12 main">
<h1><small>Proposal Narrative</small></h1>
            <p class="specialInstructionsNotes">A narrative proposal sufficient for peer review is required. Narratives can be up to <strong>5 single-spaced (12 pt. font) pages</strong>. Please note: the bibliography does <em>not</em> count towards the 5-page limit). The narrative should include:</p>
            <ul class="specialInstructionsNotes">
                <li>A statement of the objectives and significance of the proposed research/creative activity.</li>
                <li>A summary of relevant previous work by the applicant and/or others in the field.</li>
                <li>A description of the plan for accomplishing the objectives - include methodology, the roles of all personnel involved, and plans for access to any special resources.</li>
                <li>Although budget details and justification for personnel and non-personnel costs are required in the budget section, the narrative should make reference to all items, activities, and services of the requested funding.</li>
            </ul>
.
.
.
other elements added to the page
.
.
.
</div>

第2页:

<div class="span12 main">
<h1><small>Brief Summary of Current Research/Creative Activities</small></h1>
            <p class="specialInstructionsNotes">A brief summary of the investigator’s research/creative activities, whether or not they are related to the activities proposed in this application.</p>
.
.
.
other elements added to the page
.
.
.
</div>

第 3 页:

<div class="span12 main">
<h1><small>Supplemental Information</small></h1>
            <ul class="specialInstructionsNotes">
                <li>Supplemental information is not required. </li>
                <li>Please do not upload article reprints or book chapters. </li>
                <li>Uploaded files must be less than 1 megabyte in size. If you are attempting to upload scanned documents please use the OCR function to convert them to text before uploading. </li>
            </ul>
.
.
.
other elements added to the page
.
.
.
</div>

第 4 页:

<div class="span12 main">
<h1><small>Publication List</small></h1>
            <ul class="specialInstructionsNotes">
                <li>A listing of publications and/or performances/creative activities for the last three years of the investigator(s)</li>
                <li>Vitae are not requested</li>
            </ul>
            <p class="specialInstructionsNotes">Each applicant on the proposal should upload a separate list. This list will be stored in the system and you may retrieve it in the future. If you or any other CoPI had previously uploaded a publication list, it will be shown below.<br /><br />
                <strong>Please upload your own publication list only.</strong>
            </p>
.
.
.
other elements added to the page
.
.
.
</div>

如果我需要进一步解释,请告诉我。

4

3 回答 3

5

这样的解决方法呢?

.specialInstructionsNotes {
   padding-bottom: 30px; /* set padding for all special elements */
}
.specialInstructionsNotes + .specialInstructionsNotes {
   margin-top: -30px; /* but make the next element hide the bottom padding
                         of its preceding siblings of the same class */
}
于 2013-07-18T16:04:05.873 回答
0

如果您需要您的解决方案在两次以上的情况下工作,.specialInstructionsNotes并且如果没有其他元素,您还希望在第一个元素上进行填充,那么即使您不喜欢它,js 解决方案也会派上用场;)

JS

$('h1').each(function() {
    $(this).nextAll('.specialInstructionsNotes:last').addClass('last-note');
});

然后很容易解决 CSS 中的问题:

CSS

.specialInstructionsNotes.last-note {
    padding: 0 0 30px; 
}

请参阅示例小提琴

于 2013-07-18T15:45:44.873 回答
0

将它们添加到您的<head>部分:

使用CSS(脏但有效!):

<!--[if lte IE 8]>
<style type="text/css">
    .main .specialInstructionsNotes{
        padding-bottom: expression(this.nextSibling==null?'30px':'3%');
    }
</style>
<![endif]-->

您还可以创建一个ie.css包含上面的 CSS 并像这样调用它:

<!--[if lte IE 8]>
    <link rel="stylesheet" href="ie.css" type="text/css" />
<![endif]-->

jQuery

<!--[if lte IE 8]>
    <script type="text/javascript">
        $(function(){
            $(".main :last-child.specialInstructionsNotes").css("padding-bottom", "30px dashed red");
        });
    </script>
<![endif]-->

参考资料: http: //mauzon.com/pseudoclasses-first-child-and-last-child-for-ie6/

于 2013-07-18T16:54:32.570 回答