У меня есть изображение и текст в двух разных div с цветом фона, текст в одном блоке длинный, а во втором - короткий, но ширина обоих блоков одинакова, чего я не хочу. Вот мой код:
.banner{
position: relative;
}
.banner img{
width: 100%;
}
.image-text{
position: absolute;
top: 40%;
left: 8%;
}
.image-text h2{
font-size: 22px;
font-weight: bolder;
color: #fff;
margin: 0px;
}
.upper-text{
background: #2b96c3;
padding: 5px 10px;
}
.lower-text{
background: #0d729c;
padding: 5px 10px;
}<section>
<div class = "banner">
<img src = "https://i.imgur.com/xTeFbiv.jpg">
<div class = "image-text">
<div class = "upper-text">
<h2>Excellent Successfull</h2>
</div>
<div class = "lower-text">
<h2>Sustainable</h2>
</div>
</div>
</div>
</section>Все, что мне нужно, это как на картинке, желаемый результат:
Я перепробовал много уловок, но не понял, как это сделать.
о, спасибо, дружище. у меня это сработало!
Нет проблем - с таким же успехом можно закрыть этот вопрос, поскольку сомнительно, что он будет полезен для будущих пользователей;)






Сделайте отображение второго блока div inline-block так, чтобы он не занимал 100% ширины.
Вот пример:
.banner{
position: relative;
}
.banner img{
width: 100%;
}
.image-text{
position: absolute;
top: 40%;
left: 8%;
}
.image-text h2{
font-size: 22px;
font-weight: bolder;
color: #fff;
margin: 0px;
}
.upper-text{
background: #2b96c3;
padding: 5px 10px;
}
.lower-text{
background: #0d729c;
padding: 5px 10px;
display: inline-block;
}<section>
<div class = "banner">
<img src = "https://i.imgur.com/xTeFbiv.jpg">
<div class = "image-text">
<div class = "upper-text">
<h2>Excellent Successfull</h2>
</div>
<div class = "lower-text">
<h2>Sustainable</h2>
</div>
</div>
</div>
</section>
.lower-text {display:inline-block;}