Расположенные в ряд иконки с центрированной снизу надписью
Пытаюсь сделать адаптивное меню с квадратными иконками и надписью под ними. Изображение я смог уместить в центре блока, но как добавить под каждый пункт надпись я не могу понять. Всё что я придумал, это прописать тегу
абсолютное позиционирование и сместить его вниз, но разместить в центре его не получается
.square {
width: 76px;
box-shadow: rgba(46, 45, 36, 0.15) 0px 0px 6px, rgba(46, 45, 36, 0.15) 0px 3px 6px;
border-radius: 8px;
height: 76px;
margin: 8px;
font-size: 14px;
margin-bottom: 40px;
position: relative;
}
.square img {
margin: auto;
position: absolute;
top: 0;
left: 5px;
bottom: 0;
right: 0;
}
.square p {
position: absolute;
bottom: -42px;
}
<div class="container-fluid">
<div class="row justify-content-center">
<div class="square">
<img src="/assets/icons/homework.svg" alt="">
<p>My marks</p>
</div>
<div class="square">
<img src="/assets/icons/exams.svg" alt="">
<p>Exams</p>
</div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
</div>
</div>
Ответы (1 шт):
Так?
.square {
width: 76px;
box-shadow: rgba(46, 45, 36, 0.15) 0px 0px 6px, rgba(46, 45, 36, 0.15) 0px 3px 6px;
border-radius: 8px;
height: 76px;
margin: 8px;
font-size: 14px;
margin-bottom: 40px;
position: relative;
}
.square img {
margin: auto;
position: absolute;
top: 0;
left: 5px;
bottom: 0;
right: 0;
}
.square p {
width: 100%;
position: absolute;
left: 50%;
transform: translateX(-50%);
top: calc(100% + 5px);
margin: 0;
text-align: center;
}
<div class="container-fluid">
<div class="row justify-content-center">
<div class="square">
<img src="/assets/icons/homework.svg" alt="">
<p>My marks</p>
</div>
<div class="square">
<img src="/assets/icons/exams.svg" alt="">
<p>Exams</p>
</div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
</div>
</div>
Если требуется объяснение работы кода, пишите, расскажу.
transform: translateX(-50%) позволяет сдвинуть блок по X относительно своей позиции на половину ширины блока (-50%), что в совокупности с left: 50%, визуально разместит блок строго по центру, даже если ширина родителя будет меньше ширины данного блока.
Так же я указал вместо bottom: -42px значение top: calc(100% + 5px), что позволит разместить блок p на 5px от родителя не зависимо от того, какой размер будет у родителя или у самого p.
Т.к. при увеличение размера p блок будет верхнем краем оставаться на позиции и "расти вниз", а в случае bottom: -42px наоборот, нижней частью на позиции и "расти вверх".
body {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
min-height: 100vh;
margin: 0;
}
.test {
display: block;
width: 80px;
height: 50px;
border: 2px dashed #f00;
box-sizing: border-box;
position: relative;
text-align: center;
}
.test:not(:last-child) {
margin-right: 20px;
}
.test .p {
display: block;
width: 50px;
height: 50px;
background-color: #00f;
position: absolute;
left: 50%;
transform: translateX(-50%);
-webkit-animation: resize 2s linear infinite;
animation: resize 2s linear infinite;
}
.i1 .p {
bottom: -57px;
}
.i2 .p {
top: calc(100% + 7px);
}
@-webkit-keyframes resize {
40%,
60% {
height: 100px;
}
10%,
90%,
100% {
height: 50px;
}
}
@keyframes resize {
40%,
60% {
height: 100px;
}
10%,
90%,
100% {
height: 50px;
}
}
<div class="test i1">bottom
<div class="p"></div>
</div>
<div class="test i2">top
<div class="p"></div>
</div>