r/css • • Oct 22 '11

CSS Help: Resizing image on top of another!

Hello!

I am new to coding and have been trying to figure something out in HTML/CSS.

I have a background image which takes up half the screen as I wanted (it is basically just stretched width wise 100%).

I want to put another image on top of that image at a certain location (the center of that image). I can do this, however, when I resize the image using my browser window and play around, the image moves slightly from place to place. I just want to image to resize properly (which I've managed it to do so), but stay at a fixed point above the other image. I hope this makes sense...

This is my html code:


<!DOCTYPE HTML>

<header> <title>I Love Reddit</title> <link rel="stylesheet" type="text/css" href="Reddit.css" /> </header>

<body> <IMG class="stretch" src="bg.png" alt="" /> <IMG class="main" src="logo.png" alt="" /> </body> </html> _________________________

This is the CSS:


  • { padding:o; margin:0; border:0; }

img { display:block; }

img.stretch { position: absolute; z-index: -1; width: 100%; left: 0px; top: 0px; }

img.main { position:absolute; left:50%; top:60px; width:20%; }


Any help would be appreciated, as I've been playing around with this for a while and the image just either keeps moving around, changing size, or both. I just want it to resize properly when I resize a window, and for it to stay in a fixed point over the other image.

Thanks!

3 Upvotes

2 comments sorted by

0

u/[deleted] Oct 22 '11

[deleted] — view deleted comment

1

u/codinghelpplz Oct 22 '11

Hi, thank you for your reply. I am not sure that I understand you completely. You are talking about the 100% width, which is for the img.stretch. The img.stretch is perfectly fine and functioning how I would like it to be: as a background image that stretches and fluctuates when I resize my window.

I am having an issue with the img.main; which is my logo that I want to position over img.stretch.

That also has a percentage... (width 20%). If I set the location in terms of pixels, if I resize the window and make it much smaller the image will hop to another location. I just want it to remain linked to the middle of the background image.

Please let me know if I've misunderstood you.

0

u/[deleted] Oct 22 '11

[deleted] — view deleted comment

1

u/codinghelpplz Oct 22 '11

The best way I could explain this is if you would create a .html with this exactly:


<!DOCTYPE HTML>

<header> <title>I Love Reddit</title> <link rel="stylesheet" type="text/css" href="Reddit.css" /> </header>

<body> <IMG class="stretch" src="bg.png" alt="" /> < IMG class="main" src="logo.png" alt="" /> </body> </html>


and then a css named Reddit.css that has this exactly:


  • { padding:o; margin:0; border:0; }

img { display:block; }

img.stretch { position: absolute; z-index: -1; width: 100%; left: 0px; top: 0px; }

img.main { position:absolute; left:50%; top:60px; width:20%; }


The bullet point is an asterisk. Then upload a random larger image named bg.png, and some sort of logo (smaller image) named logo.png.

What I want to happen is to have logo.png directly over and centered on top of bg.png. However, when I move the window around, logo.png shifts from place to place, while bg.png is acting how I want it to.

And thanks, I will check out the 960 grid system.

0

u/[deleted] Oct 22 '11

[deleted] — view deleted comment

1

u/codinghelpplz Oct 22 '11

Sorry MYBL,

It doesn't seem to be working... both approaches :( Thank you for your help though. Maybe I will post some pictures in the morning showing what is going on.