I need to use the html <img>
tag as a background image for a <div>
. I will then be placing a <p>
of content over this. I've tried this but cant seem to get them to display correctly. I've used position relative, margin with negative values.
Any suggestions or point me in the right direction would be appreciated.
<div>
<img src="http://www.shiaupload.ir/images/77810787432153420049.png" />
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book
</p>
</div>
The img could also use object-fit: cover; to replicate a background image with background-size: cover; . You can play with the absolute positioning properties top , right , bottom , left . In combination with setting the min-width and max-width to 100% instead of the width and height .
Use IMG if you rely on browser scaling to render an image in proportion to text size. Use IMG for multiple overlay images in IE6. Use IMG with a z-index in order to stretch a background image to fill its entire window. Note, this is no longer true with CSS3 background-size; see #6 below.
The most common & simple way to add background image is using the background image attribute inside the <body> tag. The background attribute which we specified in the <body> tag is not supported in HTML5. Using CSS properties, we can also add background image in a webpage.
It is widely known that using the image tag is better SEO practice than using background images because of the screen reader implications. But we know too well how difficult it can be sometimes using image tags within constrained proportions and responsive environments.
To make an almost perfectly duplicate of the features used for background-image, we've to consider the features of the img
include:
div
.The result would be something like this:
div {
display: inline-block;
overflow: hidden;
position: relative;
width: 100%;
}
img {
pointer-events: none;
position: absolute;
width: 100%;
height: 100%;
z-index: -1;
}
p {
padding: 25px;
}
<div>
<img src="http://via.placeholder.com/720x480/ddd/007" />
<p>
Lorem Ipsum is simply dummy text of the printing and
typesetting industry. Lorem Ipsum has been the industry's
standard dummy text ever since the 1500s, when an unknown
printer took a galley of type and scrambled it to make a type
specimen book
</p>
</div>
You might modify the width and height to your needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With