Я хочу поставить .e div в .second div на 50%. Моя цель - избавиться от пробела между двумя .e divs.Как мне сделать этот макет CSS?
Я хочу сделать это без явного перемещения div вверх (например, position: relative; top: -50%;). Я заметил, что делать float: right; на .first div исправляет проблему макета, но уничтожает порядок содержимого HTML.
Вот картина того, что я пытаюсь сделать:
Как я могу сделать эту работу, сохраняя свой HTML в том же порядке?
html, body {
height: 100%;
width: 100%;
margin: 0;
}
h1, h2, h3, h4, h5, h6 {
margin: 0;
}
body {
font-family: 'Montserrat', Helvetica, Arial, sans-serif;
}
body .a {
background: #f04576;
}
body .b {
background: #53a2e3;
}
body .c {
background: #ef4658;
text-align: right;
}
body .d {
background: #f4ad49;
text-align: left;
}
body .e {
background: #69de92;
}
body .first {
height: 100%;
width: 100%;
}
body .second {
height: 100%;
width: 100%;
}
body .tall:nth-child(2n) {
position: relative;
top: -50%;
}
body .tall, body .long, body .square {
float: left;
}
body .tall {
height: calc(100% - 4rem);
width: calc(25% - 4rem);
padding: 2rem;
font-size: 2.5rem;
text-transform: uppercase;
}
body .long {
height: calc(50% - 4rem);
width: calc(50% - 4rem);
padding: 2rem;
font-size: 2.5rem;
text-transform: uppercase;
}
body .square {
height: calc(50% - 4rem);
width: calc(25% - 4rem);
padding: 2rem;
font-size: 2.5rem;
text-transform: uppercase;
text-align: center;
}
<div class='first'>
<div class='a tall'>A</div>
<div class='c square'>B</div>
<div class='b long'>C</div>
<div class='d long'>D</div>
<div class='e square'>E</div>
</div>
<div class='second'>
<div class='a square'>A</div>
<div class='c long'>B</div>
<div class='b long'>C</div>
<div class='d square'>D</div>
<div class='e tall'>E</div>
</div>
Edit 1: Я не хочу делать position: relative; top: -50%;
, потому что если я повторю свой HTML, я остался с огромным зазором после каждого .second дел. Вот пример:
html,
body {
height: 100%;
width: 100%;
margin: 0;
}
h1,
h2,
h3,
h4,
h5,
h6 {
margin: 0;
}
body {
font-family: 'Montserrat', Helvetica, Arial, sans-serif;
}
body .a {
background: #f04576;
}
body .b {
background: #53a2e3;
}
body .c {
background: #ef4658;
text-align: right;
}
body .d {
background: #f4ad49;
text-align: left;
}
body .e {
background: #69de92;
}
body .first {
height: 100%;
width: 100%;
}
body .second {
height: 100%;
width: 100%;
}
body .tall:nth-child(2n) {
position: relative;
top: -50%;
}
body .tall,
body .long,
body .square {
float: left;
}
body .tall {
height: calc(100% - 4rem);
width: calc(25% - 4rem);
padding: 2rem;
font-size: 2.5rem;
text-transform: uppercase;
}
body .long {
height: calc(50% - 4rem);
width: calc(50% - 4rem);
padding: 2rem;
font-size: 2.5rem;
text-transform: uppercase;
}
body .square {
height: calc(50% - 4rem);
width: calc(25% - 4rem);
padding: 2rem;
font-size: 2.5rem;
text-transform: uppercase;
text-align: center;
}
<div class='first'>
<div class='a tall'>A</div>
<div class='c square'>B</div>
<div class='b long'>C</div>
<div class='d long'>D</div>
<div class='e square'>E</div>
</div>
<div class='second'>
<div class='a square'>A</div>
<div class='c long'>B</div>
<div class='b long'>C</div>
<div class='d square'>D</div>
<div class='e tall'>E</div>
</div>
<!-- Repeating the code -->
<div class='first'>
<div class='a tall'>A</div>
<div class='c square'>B</div>
<div class='b long'>C</div>
<div class='d long'>D</div>
<div class='e square'>E</div>
</div>
<div class='second'>
<div class='a square'>A</div>
<div class='c long'>B</div>
<div class='b long'>C</div>
<div class='d square'>D</div>
<div class='e tall'>E</div>
</div>
Почему вы не хотите использовать позиционирование? –
Разрыв остается после каждого .секундного div, если я повторяю свой HTML. –
Мое решение исправляет это. Повторяется 8 раз и пробелов нет. –