我想将margin top:15px应用于此div的第一个元素
我正在从数据库中提取ckeditor数据,因此第一个元素是不可预测的
这是我的div结构,
<div class="ckeditordata">
//1st element is unknown
//can be <p>, <ul>, <ol>
</div>
我想将margin top:15px应用于此div的第一个元素
我正在从数据库中提取ckeditor数据,因此第一个元素是不可预测的
这是我的div结构,
<div class="ckeditordata">
//1st element is unknown
//can be <p>, <ul>, <ol>
</div>
:first-child
。.ckeditordata > :first-child {
margin-top: 15px;
}
>
子选择器和 :nth-child(1)
来仅应用于第一个子元素的css,如下所示:
.ckeditordata > *:nth-child(1) {
color: red;
margin-top: 15px;
}
<div class="ckeditordata">
<p>TEXT</p>
<p>TEXT</p>
<p>TEXT</p>
</div>
.ckeditordata :first-child {
margin top:15px
}
.selector> *:nth-child(1) {
color: green;
margin-top: 20px;
font-weight: 600;
}
<div class="selector">
<p>TEXT</p>
<p>TEXT</p>
<p>TEXT</p>
</div>
你也可以通过jQuery来使用。检查一下这段代码,看看它是否有效:
$('.ckeditordata').children(':nth-child(1)').css('margin-top','15px');
.ckeditordata > :first-child
会更好。因为它可以是<ul>
等。 - c-smile