Поместите блок div абсолютно под другой блок div, не зная его высоты
Я создал веб-сайт, на котором я поместил несколько картинок в ящики для дел и поместил их абсолютно в положение, в котором я хочу их видеть
.char1 - .char5
уже есть правильная позиция. Теперь я хочу .char6 - .char10
выровнять прямо под .char1 - .char5
, Какой бы умный способ сделать это? В моем коде это работает только до тех пор, пока я не изменю размер экрана.
Также все мои .char
div имеют почти одинаковые значения, кроме позиций left и top. Есть ли способ сократить это?
Большое спасибо.
* {
background-color: #151515;
}
.h {
align-self: center;
width: 96%;
height 100%;
border-style: solid;
border-width: 0px;
border-color: 817B6F;
}
.chars {
display: flex;
width: 10%;
height: auto;
background-color: 817B6F;
position: absolute;
top: 10%;
left: 25%;
}
.char1 {
justify-content: center;
display: flex;
background-color: 817B6F;
position: absolute;
width: 10%;
top: 10%;
height: auto;
left: 21.5%;
border-top-left-radius: 5px;
padding: 0.7%;
border-width: 1px;
border-color: black;
border-style: solid;
}
.char2 {
justify-content: center;
display: flex;
background-color: 817B6F;
position: absolute;
width: 10%;
top: 10%;
height: auto;
left: 32.9%;
padding:0.7%;
border-width: 1px;
border-color: black;
border-style: solid;
}
.char3 {
justify-content: center;
display: flex;
background-color: 817B6F;
position: absolute;
width: 10%;
top: 10%;
height: auto;
left: 44.3%;
padding:0.7%;
border-width: 1px;
border-color: black;
border-style: solid;
}
.char4 {
justify-content: center;
display: flex;
background-color: 817B6F;
position: absolute;
width: 10%;
top: 10%;
height: auto;
left: 55.7%;
padding:0.7%;
border-width: 1px;
border-color: black;
border-style: solid;
}
.char5 {
justify-content: center;
display: flex;
background-color: 817B6F;
position: absolute;
width: 10%;
top: 10%;
height: auto;
left: 67.1%;
padding:0.7%;
border-width: 1px;
border-color: black;
border-style: solid;
border-top-right-radius: 5px;
}
.char6 {
justify-content: center;
display: flex;
background-color: 817B6F;
position: absolute;
width: 10%;
top: 33%;
height: auto;
left: 21.5%;
border-bottom-left-radius: 5px;
padding: 0.7%;
border-width: 1px;
border-color: black;
border-style: solid;
}
.char7 {
justify-content: center;
display: flex;
background-color: 817B6F;
position: absolute;
width: 10%;
top: 33%;
height: auto;
left: 32.9%;
padding:0.7%;
border-width: 1px;
border-color: black;
border-style: solid;
}
.char8 {
justify-content: center;
display: flex;
background-color: 817B6F;
position: absolute;
width: 10%;
top: 33%;
height: auto;
left: 44.3%;
padding:0.7%;
border-width: 1px;
border-color: black;
border-style: solid;
}
.char9 {
justify-content: center;
display: flex;
background-color: 817B6F;
position: absolute;
width: 10%;
top: 33%;
height: auto;
left: 55.7%;
padding:0.7%;
border-width: 1px;
border-color: black;
border-style: solid;
}
.char10 {
justify-content: center;
display: flex;
background-color: 817B6F;
position: absolute;
width: 10%;
top: 33%;
height: auto;
left: 67.1%;
padding:0.7%;
border-width: 1px;
border-color: black;
border-style: solid;
border-bottom-right-radius: 5px;
}
<div class="char1">
<img class= "h" name= Warrior src="http://placehold.it/256" />
</div>
<div class="char2">
<img class= "h" name= Warrior src="http://placehold.it/256" />
</div>
<div class="char3">
<img class= "h" name= Warrior src="http://placehold.it/256" />
</div>
<div class="char4">
<img class= "h" name= Warrior src="http://placehold.it/256" />
</div>
<div class="char5">
<img class= "h" name= Warrior src="http://placehold.it/256" />
</div>
<div class="char6">
<img class= "h" name= Warrior src="http://placehold.it/256" />
</div>
<div class="char7">
<img class= "h" name= Warrior src="http://placehold.it/256" />
</div>
<div class="char8">
<img class= "h" name= Warrior src="http://placehold.it/256" />
</div>
<div class="char9">
<img class= "h" name= Warrior src="http://placehold.it/256" />
</div>
<div class="char10">
<img class= "h" name= Warrior src="http://placehold.it/256" />
</div>
1 ответ
Это, случайно, то, что вы хотите достичь? Пожалуйста отметьте position
нет, где можно найти в моем коде.
body {padding: 0; margin: 0;}
.placer {
min-height: 100vh;
background-color: #212121;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.chars {
max-width: 60%;
display: flex;
align-items: center;
flex-wrap: wrap;
padding: 60px 0;
}
.chars img {
flex: 0 1 20%;
max-width: calc(20% - 10px);
object-fit: contain;
margin: 5px;
}
@media (max-width: 800px) {
.chars img {
max-width: calc(50% - 10px);
}
}
<div class="placer">
<div class="chars">
<img class= "h" name= Warrior src="http://placehold.it/256" />
<img class= "h" name= Warrior src="http://placehold.it/256" />
<img class= "h" name= Warrior src="http://placehold.it/256" />
<img class= "h" name= Warrior src="http://placehold.it/256" />
<img class= "h" name= Warrior src="http://placehold.it/256" />
<img class= "h" name= Warrior src="http://placehold.it/256" />
<img class= "h" name= Warrior src="http://placehold.it/256" />
<img class= "h" name= Warrior src="http://placehold.it/256" />
<img class= "h" name= Warrior src="http://placehold.it/256" />
<img class= "h" name= Warrior src="http://placehold.it/256" />
</div>
</div>
Также обратите внимание flexbox
нуждается в префиксе, чтобы получить от 85%
поддержка браузера (без префикса) для 97.38%
(в настоящее время полностью префикс). Итак, вот префиксная версия:
.placer {
min-height: 100vh;
display: -webkit-box;
display: -webkit-flex;
display: -moz-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-webkit-flex-direction: column;
-moz-box-orient: vertical;
-moz-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-align: center;
-webkit-align-items: center;
-moz-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-webkit-justify-content: center;
-moz-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}
.chars {
-webkit-box-flex: 0;
-webkit-flex: 0 0 33%;
-moz-box-flex: 0;
-ms-flex: 0 0 33%;
flex: 0 0 33%;
max-width: 60%;
display: -webkit-box;
display: -webkit-flex;
display: -moz-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-webkit-align-items: center;
-moz-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-flex-wrap: wrap;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
}
.chars img {
-webkit-box-flex: 0;
-webkit-flex: 0 1 20%;
-moz-box-flex: 0;
-ms-flex: 0 1 20%;
flex: 0 1 20%;
max-width: -webkit-calc(20% - 10px);
max-width: -moz-calc(20% - 10px);
max-width: calc(20% - 10px);
-o-object-fit: contain;
object-fit: contain;
margin: 5px;
}
@media (max-width: 800px) {
.chars img {
max-width: -webkit-calc(50% - 10px);
max-width: -moz-calc(50% - 10px);
max-width: calc(50% - 10px);
}
}
Использование абсолютного позиционирования для размещения обычно является плохой идеей, особенно в адаптивном дизайне. Вам лучше использовать относительное или статическое (по умолчанию) позиционирование и использовать поля для достижения желаемого результата, поскольку ваши блоки будут естественным образом отображаться в DOM.
Также вы должны изучить принцип СУХОГО, как в "Не повторять себя". В вашем случае вы можете просто использовать один и тот же класс для всех ваших блоков и стиля соответственно.