Я использую сетку с 12 столбцами. Я хочу дать каждому div margin-top: 20px, кроме div в первой строке. Но мне трудно узнать, какие divs находятся в первой строке, потому что это не определено. Первая строка может содержать от 1 до 12 div.Выделить все divs в первой строке в динамической сетке
Я использую sass и попробовал следующее решение, но это работает только с div с одинаковой шириной. Первый пример не работает, потому что второй div не получает поля.
// max. 12 rows.
@for $colWidth from 1 through 12 {
// example: 3 divs in a row (colWidth = 4), 12/4+1 = 4.
// Set margin from the fourth element to the last element.
$number: (12/$colWidth) + 1;
// set margin only for banner-component
div.col-xs-#{$colWidth}:nth-child(n+#{$number}) section.banner-component {
margin-top: 20px;
}
div.col-sm-#{$colWidth}:nth-child(n+#{$number}) section.banner-component {
margin-top: 20px;
}
}
Селекторы Ohter css также не работают. первый ребенок, n-й ребенок. Любая идея, как я могу выбрать divs в первой строке?
Вот некоторые примеры:
Пример 1: Первая строка содержит 1 Div (Col-LG-12)
<div> class="col-xs-12 col-lg-12">
<section class="banner-component"></section>
</div>
<div> class="col-xs-6 col-lg-6">
<section class="banner-component"></section>
</div>
<div> class="col-xs-6 col-lg-6">
<section class="banner-component"></section>
</div>
<div> class="col-xs-6 col-lg-6">
<section class="banner-component"></section>
</div>
Пример 2: Первая строка содержит 2 дивы (Col-LG-6)
<div> class="col-xs-6 col-lg-6">
<section class="banner-component"></section>
</div>
<div> class="col-xs-6 col-lg-6">
<section class="banner-component"></section>
</div>
<div> class="col-xs-6 col-lg-6">
<section class="banner-component"></section>
</div>
<div> class="col-xs-6 col-lg-6">
<section class="banner-component"></section>
</div>
Пример 3: Первая строка содержит 3 дивы (COL-LG-4)
<div> class="col-xs-4 col-lg-4">
<section class="banner-component"></section>
</div>
<div> class="col-xs-4 col-lg-4">
<section class="banner-component"></section>
</div>
<div> class="col-xs-4 col-lg-4">
<section class="banner-component"></section>
</div>
<div> class="col-xs-6 col-lg-6">
<section class="banner-component"></section>
</div>
Пример 4: Первая строка содержит 3 дивы (Col-LG-4, Col-LG-6, Col-LG-2)
<div> class="col-xs-4 col-lg-4">
<section class="banner-component"></section>
</div>
<div> class="col-xs-4 col-lg-6">
<section class="banner-component"></section>
</div>
<div> class="col-xs-4 col-lg-2">
<section class="banner-component"></section>
</div>
<div> class="col-xs-6 col-lg-6">
<section class="banner-component"></section>
</div>
Спасибо вам обоим за ваши ответы. Оба варианта работали для меня. Я выбрал версию JQuery и преобразовал ее в JSP. – chansang