Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use the HTML <img> tag as a background image instead of the CSS background-image property?

Tags:

html

css

image

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>
like image 905
Getek99 Avatar asked Jul 07 '15 11:07

Getek99


People also ask

Can I use IMG tag as background image?

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 .

When should you use an IMG tag and when should you use background image CSS property to display an image?

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.

How do you use an image as a background tag in HTML?

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.

What is better IMG or background image?

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.


1 Answers

To make an almost perfectly duplicate of the features used for background-image, we've to consider the features of the img include:

  • It doesn't have any pointer events.
  • It's rendered at least one layer below the 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.

like image 138
luukvhoudt Avatar answered Sep 19 '22 03:09

luukvhoudt