Я пытаюсь добиться следующего результата: Желаемый результат Однако я зашел так далеко и застрял. Что я получаю
Это мой код:
.base {
background: #e2e2e2;
width: 1020px;
height: 350px;
position: absolute;
top: 100px;
left: 130px;
}
<div class = "section5" style = "width:100%;background-color:#f5f5f5;height:550px;position: relative;">
<div class = "base"></div>
<div class = "square1" style = "width:255px;height:193px;background-color:#969696; z-index:1;"></div>
</div>
🤔 А знаете ли вы, что...
HTML5 - последняя версия HTML, внедряющая новые элементы и функциональность, такие как <canvas> и <video>.
Возможно поможет, выведено с помощью z-index
, остальные поля вы можете настроить.
.section5{
width:100%;
background-color:#f5f5f5;
height:550px;
position: relative;
z-index:0;
}
.square1{
width:255px;
height:193px;
background-color:#969696;
position: relative;
z-index:1;
}
.base{
background:#e2e2e2;
width:1020px;
height:350px;
position: absolute;
top: 100px;
left:130px;
z-index: -1;
}
<div class = "section5">
<div class = "base"></div>
<div class = "square1"></div>
</div>
Начнем с основы нашего HTML:
<div class = "section5">
<div class = "base"></div>
<div class = "square"></div>
<div class = "square"></div>
<div class = "square"></div>
<div class = "square"></div>
<div class = "square"></div>
<div class = "square"></div>
</div>
И стиль:
.section5 {
position: relative;
width: 800px;
height: 600px;
background-color: #f5f5f5
}
.base {
position: relative;
width: 600px;
height: 400px;
background-color: #e2e2e2;
left: 100px;
top: 100px;
}
.square {
position: absolute;
width: 150px;
height: 120px;
background-color: #969696;
}
Теперь мы можем использовать nth-of-type
, чтобы сделать квадраты odd
белыми:
.square:nth-child(odd) {
background-color: white;
}
Чтобы вычислить абсолютное положение квадратов, если мы хотим, чтобы первый и последний квадрат имели зазор 50px
от квадрата .section5
, и у нас есть квадраты n
, мы можем вычислить расстояние d
между двумя соседними квадратами следующим образом:
d = (W - 2x - w) / (n - 1)
где W
— ширина .section5
, а x
— зазор 50px
, о котором мы упоминали ранее. Таким образом, d
в данном случае будет 110px
, поэтому left
каждого квадрата нужно увеличить на 110px
по сравнению с предыдущим.
Та же логика применима и к top
.
И применив их, получаем конечный результат (открываем как на ладони)
.section5 {
position: relative;
width: 800px;
height: 600px;
background-color: #f5f5f5;
}
.base {
position: relative;
width: 600px;
height: 400px;
background-color: #e2e2e2;
left: 100px;
top: 100px;
}
.square {
position: absolute;
width: 150px;
height: 120px;
background-color: #969696;
}
/* Apply alternating background color to odd squares */
.square:nth-child(odd) {
background-color: white;
}
/* Positioning the squares */
.square:nth-child(2) {
left: 50px;
top: 50px;
}
.square:nth-child(3) {
left: 160px;
top: 126px;
}
.square:nth-child(4) {
left: 270px;
top: 202px;
}
.square:nth-child(5) {
left: 380px;
top: 276px;
}
.square:nth-child(6) {
left: 490px;
top: 354px;
}
.square:nth-child(7) {
left: 600px;
top: 430px;
}
<div class = "section5">
<div class = "base"></div>
<div class = "square"></div>
<div class = "square"></div>
<div class = "square"></div>
<div class = "square"></div>
<div class = "square"></div>
<div class = "square"></div>
</div>
Альтернативный подход с использованием переменных CSS:
.square {
position: absolute;
width: 150px;
height: 120px;
background-color: #969696;
left: calc(50px + 110px * var(--i));
top: calc(50px + 76px * var(--i));
}
.square:nth-child(2) { --i: 0; }
.square:nth-child(3) { --i: 1; }
.square:nth-child(4) { --i: 2; }
.square:nth-child(5) { --i: 3; }
.square:nth-child(6) { --i: 4; }
.square:nth-child(7) { --i: 5; }