Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is text displayed behind absolute positioned image with CSS?

Using this HTML, I can't figure why the text displays behind the image:

<div style="position: relative">
<img src="image_url_here" style="position:absolute; top: 10px; left: 10px;" />  
This is some text, drawn on the page after the image, why is it drawn behind the image?
</div>

?

Tested in Chrome on Mac & PC and IE on PC.

like image 550
Claud Avatar asked Dec 09 '22 13:12

Claud


2 Answers

This article can answer your question: z-index explained

TL;DR: Positioned elements are stacked over non-positioned elements, so just add position: relative to the text you want stacked on top.

<div style="position: relative">
    <img src="https://via.placeholder.com/150" style="position:absolute; left: 10px;" />  
    <span style="position:relative">This is some text</span>
</div>
like image 154
Jimmy Avatar answered Dec 11 '22 11:12

Jimmy


Refer this https://developer.mozilla.org/en-US/docs/Web/CSS/position

Try this or set as per your requirement

<div style="position: relative">
<img src="image_url_here" style="position:relative; top: 10px; left: 10px;" />  
This is some text, drawn on the page after the image, why is it drawn behind the image?
</div>
like image 34
Satyam Koyani Avatar answered Dec 11 '22 12:12

Satyam Koyani