I have a div containing a paragraph. I want to add a translucent background image to the paragraph but only upto height same as height of the paragraph. I have:
<div id = "phase1box">
<div id = "phase1content" class = "phasecontent">
<h1>Phase 1</h1>
<p1>
• some text.<br/>
• some text.<br/>
• some text.<br/>
• some text.<br/>
• some text.<br/>
</p1>
</div>
</div>
CSS:
#phase1box {
position: absolute;
top: 0%;
width:100%;
height:18%;
background-image: url("../assets/phase1box.png");
}
I have hard coded height: 18%
but instead i want the background image to take height of the div (paragraph). If i do height: auto
no background image shows up.
2
Answers
To make the background image height the same as the height of the div content, you can use the CSS property background-size with the value contain. This will ensure that the background image scales to fit within the container while maintaining its aspect ratio.
Here’s an updated version of your code with the necessary CSS modifications:
CSS