Я пытаюсь сделать белый div поверх img.
Я пробовал использовать абсолютное и относительное положение, но не смог. Я застрял.
Поле отображается под контейнером img.
Что я делаю неправильно? :)
.img_container {
margin-left: 40px;
margin-right: 40px;
background-size: cover;
background-position: center center;
position: relative;
text-align: center;
}
.img_container .the_img img {
width: 100%;
height: 420px;
}
.img_container .the_img .container_white{
width: 900px;
height: 50px;
margin: 0 auto;
background: white;
position:absolute;
} <div class = "img_container">
<div class = "the_img">
<?php echo get_the_post_thumbnail( get_the_ID(), 'large'); ?>
<div class = "container_white">¨
</div>
</div>
</div>
<div class = "fixfloat"></div>





Единственное, что делает get_the_post_thumbnail, - это вставляет элемент изображения в ваш код.
Например, он сгенерирует что-то вроде этого:
<img width = "" height = "" src = "" class = "" alt = "" large = "" srcset = "" sizes = "">
Конечно, заполнены вашими индивидуальными значениями.
Для лучшей демонстрации я заменил вызов функции php возвращенным тегом img.
If you want to place your div relative to its parent:
- Your parent should be
position: relative- Your children should be
postion: absolute
В вашем случае элемент the_img является родительским.
Оба дочерних элемента, img и container_white, должны быть абсолютными, чтобы удалить их из обычного потока документов. См. Вырезанный код.
.img_container {
margin-left: 40px;
margin-right: 40px;
background-size: cover;
background-position: center center;
position: relative;
/**text-align: center;**/
}
.img_container .the_img img {
width: 100%;
height: 420px;
position: absolute;
}
.img_container .the_img .container_white{
width: 900px;
height: 50px;
margin: 0 auto;
background: white;
position:absolute;
}
.the_img{
position: relative;
}<div class = "img_container">
<div class = "the_img">
<img src = "https://images.pexels.com/photos/386148/pexels-photo-386148.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260" alt = "beach">
<div class = "container_white"></div>
</div>
</div>
<div class = "fixfloat"></div>Итак, единственное, что вам нужно сделать, это
the_img в relativeimg в absoluteПРИМЕЧАНИЕ. Я удалил text-align: center из img_container только для лучшего отображения.
Закройте блок
the_img, до, блокcontainer_white.