Hey I'm having a bit of a weird problem.

I have an image contains in a div (#rel-content) and that div inside of another (#releases). Here is my HTML code:

<div class="releases">
            	<div id="rel-topbar"><img src="images/blackrightbar.jpg" width="312" height="39" alt="Release Navigation Bar">
                </div> <!-- rel-topbar -->
                <div id="rel-content"><a href="" target="itunes_store" ><img src="images/album-cover.jpg" width="300" height="300" alt="Hope of the Hated - Erased Album Cover" style="border-style: none" /></a>
                <h2><span>Buy The Album:<br/ >Available now on iTunes!</span></h2>
                </div> <!-- rel-content -->
            </div><!-- releases -->

As you can see inside #rel-content I have an image and a header inside. What I want is the header (h2) to overlap the image. Whereas what I'm receiving is the header flying to the other side of the page.

Here is my CSS for the div's:

#rel-content{
	margin: 10px 0px 10px 5px;
}
#rel-content img{
	position: relative;
	width:100%;
}
#rel-content h2{
	position:absolute;
	top:200px;
	left:0;
	width:100%;
	display:block;
}
#rel-content h2 span{
	color:white;
	font: bold 24/25px Helvetica, Sans-serif;
	letter-spacing: -1px;
	background: rgb(0, 0, 0);
	background: rgba(0, 0, 0, 0.7);
	padding:10px;
}

Here is an image of what is showing up:
[IMG][/IMG]

I hope this makes sense as to what I'm trying to achieve. Really appreciate any responses.

Reece Cropley

Dani AI

Generated

The header is escaping because the absolutely positioned h2 is looking for its nearest positioned ancestor. Since #rel-content is not positioned, the h2 uses the page as its containing block and drifts on resize. is right that z-index matters, but it only takes effect on positioned elements.

Make the image wrapper the containing block, then anchor the heading to the bottom of that wrapper. Avoid negative margins; they are fragile when the image scales.

Add/adjust these rules:

/* make the parent the containing block */
#rel-content { position: relative; }

/* stop inline-img gaps and scale safely */
#rel-content img { display: block; max-width: 100%; height: auto; }

/* keep your h2 absolute; anchor it to the bottom of the image */
#rel-content h2 { bottom: 0; left: 0; right: 0; margin: 0; padding: 10px;
  background: rgba(0,0,0,.7); color: #fff; line-height: 1.2; z-index: 2;
}

Notes:

  • If the overlay blocks the album link, either move the h2 inside the <a> so the whole card is clickable, or add pointer-events: none; to the h2 so clicks pass through.
  • Use bottom: 0 (not a fixed top) so the caption stays pinned even as the image resizes.
  • The .rightside { float: right; } on an outer wrapper will not affect the positioning once #rel-content is position: relative, but give #rel-content an explicit width (e.g., 300px) if you want predictable layout alongside floats.

This will keep the heading inside the image container and reliably overlaid across viewport changes.

Recommended Answers

All 5 Replies

it may be either that your left is 0 and not 0px. The second thing may be that you do not have z-index.

Cheers skald89, but 0 rather than 0px should just cause validation error. Tried them both however just in case and no change.

If I change the values for top and left I can get it to display in the right area, but once the browser size has changed it goes off on its own, and that's no good is it??

The DIV class that .releases is contained in .rightside has a float value of right. Just a shot in the dark but could this have anything to do with it? I'm forever having problems with structure once I use float values.

.rightside{
	/*width:365px;*/
	float:right;
	/*margin-right:15px;*/
}
#rel-content h2{
	position:absolute;
	top:200px;
	[B]left:0px;[/B]
	width:100%;
	display:block;
	[B]z-index: 1;[/B]
}

So I have just worked out a way around it but it doesn't seem to be a very good technique as im using negative values. I removed the top and the left values on h2 and simply replaced with a margin-top with a negative value of -55px.

Any other methods or techniques would be brilliant to hear, why have one solution when you can have many.

#rel-content h2{
	position:absolute;
	width:100%;
	display:block;
	z-index: 1;
	margin:-55px 0px;	
}

Cheers skald89, but 0 rather than 0px should just cause validation error. Tried them both however just in case and no change.

If I change the values for top and left I can get it to display in the right area, but once the browser size has changed it goes off on its own, and that's no good is it??

The DIV class that .releases is contained in .rightside has a float value of right. Just a shot in the dark but could this have anything to do with it? I'm forever having problems with structure once I use float values.

.rightside{
	/*width:365px;*/
	float:right;
	/*margin-right:15px;*/
}
#rel-content h2{
	position:absolute;
	top:200px;
	[B]left:0px;[/B]
	width:100%;
	display:block;
	[B]z-index: 1;[/B]
}

Im also a new at this stuff. I think float right means that it will float to the right of something and allow you to set up things up on one line.

Lets say you wanted to do this.

1 2 3 4 5 6 7

and have them all on one line and each to be able to go their own link or do something and you dont want each to be in their own div. You would set the float to left and have a right margin of Xpx. Thats what I did for a class project the other day, I didnt try float right it may do the same thing or do something completely different.

commented: Helpful in my thread. +2

I tried both 'float:left' and 'float:right' same effect.

I believe 'display:inline' is a good way to stop the browser rendering new lines and keeping them all on the same line.

I'm going to leave this thread at that, deadline to tight to dwell on the same part. Thanks for your help.

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.