我想要做的是最后一个元素(例如段落<p>
或无序列表<ul>
)在header1 <h1>
元素后面有一个名为{{1的类使用类specialInstructionsNotes仅对 last 元素应用额外填充(30px)。
我尝试使用specialInstructionsNotes
和:first-of-type
CSS3选择器,但是这些选择器没有转移到IE8(这是我将要去的最低IE兼容浏览器)。我的目标是能够在所有最新的浏览器和IE8 +中显示CSS设置(将填充应用于仅最后一个具有类:nth-of-type(1)
的元素)。
注意:可以有3个或更多元素应用此类,只要具有类specialInstructionsNotes
的最后一个元素在最底部有填充。抱歉有任何困惑。
这是我到目前为止的完整CSS:
.specialInstructionNotes
对于HTML,元素位于一个div中,并且是div中包含的最顶层元素。对于我遇到的一些页面/场景,这是我目前的HTML。
第1页:
.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;
}
第2页:
<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>
第3页:
<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>
第4页:
<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>
如果我需要进一步解释,请告诉我。
答案 0 :(得分: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 */
}
答案 1 :(得分:0)
如果你需要你的解决方案工作超过两次.specialInstructionsNotes
并且你想要第一个元素上的填充,如果没有其他的,那么js解决方案会派上用场,即使它是不喜欢你;)
<强> JS 强>
$('h1').each(function() {
$(this).nextAll('.specialInstructionsNotes:last').addClass('last-note');
});
然后很容易解决CSS中的问题:
<强> CSS 强>
.specialInstructionsNotes.last-note {
padding: 0 0 30px;
}
<强> See the exemplary Fiddle 强>
答案 2 :(得分:0)
将它们添加到您的<head>
部分:
使用CSS
(脏但有效!):
<!--[if lte IE 8]>
<style type="text/css">
.main .specialInstructionsNotes{
padding-bottom: expression(this.nextSibling==null?'30px':'3%');
}
</style>
<![endif]-->
您还可以创建一个包含上述CSS的ie.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/