Home  >  Article  >  Web Front-end  >  How to Embed an Image within a Div Using Pure CSS?

How to Embed an Image within a Div Using Pure CSS?

DDD
DDDOriginal
2024-11-01 03:14:02662browse

How to Embed an Image within a Div Using Pure CSS?

How to Place an Image within a Div Using CSS

Many developers resort to setting images as background images within CSS, a solution that doesn't allow the div to conform to the image's dimensions. To replicate the following HTML code using pure CSS:

<code class="html"><div><img src="..." /></div></code>

Solution:

Implement the following code:

<code class="html"><div class="image"></div></code>

CSS:

<code class="css">div.image::before {
   content: url(http://placehold.it/350x150);
}</code>

Additional Notes:

  • This technique has been tested on Chrome, Firefox, and Safari.
  • Refer to the following resource for further information on CSS content: http://css-tricks.com/css-content/

The above is the detailed content of How to Embed an Image within a Div Using Pure CSS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn