Hi

I have some pictures and I want to have a piece of text, like a label, for each one. I have tried floating both image and paragraph (<p>) and then assigning a negative margin to the <p> and setting its z-index: 100

but it continues to say below the image.

how do I get it ontop?

thanks
lworks

Dani AI

Generated

— you tried floats, negative margins and a high z-index, and got the label stuck under the image. sketched the markup order, suggested baking the caption into the image, and pointed at positioning. The missing piece most people hit here is not “more z-index” but the browser’s stacking rules and the element context that determines which layer actually sits on top.

Browsers only honor z-index within stacking contexts. A parent with its own positioning, a transform, opacity < 1, filter, isolation, or some other properties will create a stacking context that isolates its children from siblings. That means a label can have a large z-index yet still be under an image if they live in different stacking contexts. For a clean reference, see MDN’s explanations of z-index and stacking contexts.

Practical approaches that avoid fragile negative margins:

  • Put the image as a container background (or use a semantic <figure> with a positioned caption). Then text naturally sits above the image and stays readable across sizes.
  • If you overlay a text element, make the image and label live in the same stacking context: give the wrapper a positioning context and place the label as an overlay inside it. Also check that no ancestor is creating an unexpected stacking context.
  • Use a semi-opaque background behind the label (via a child or pseudo-element) for legibility rather than relying on color contrast alone.

Quick debugging checklist: inspect computed styles in DevTools for position, z-index, transform, opacity, or overflow on parents; try temporarily removing transforms/opacity to see stack changes; ensure the label isn’t floated or clipped by overflow:hidden. For static labels, exporting into the image (as said) is fine; for responsive layouts, prefer overlay techniques that keep text selectable and accessible.

Recommended Answers

All 3 Replies

Member Avatar for Member #117553

Post some code that we can read, however, it should look something like
<div>
<p>Your text here</p>
<img.../>
</div>

Well if you had set the p's z-index to 100 then it should work but anyway you could just add text with an image editor(Gimp, inkscape).

set position: absolute for <p>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.