Forum Moderators: not2easy

Message Too Old, No Replies

Absolute Positioning

         

Baelavay

9:17 pm on Feb 6, 2011 (gmt 0)

10+ Year Member



Hi!

What I wanted is firstly this:

..which is already exactly working how it should. Thumbs up so far!

BUT now, what I want is to place two graphics behind the table.

The first is this:

The second is this:
Both graphics are supposed to be positioned centered on the screen, with 100% height, on top of each other with the same size. Second above the first.

The second is turning orange just to be visible where it is placed - in the final, it will have the background color.

So...

what I made is this:



And as you can see, neither the positioning nor the overlapping are working properly.

Please help :)

[edited by: alt131 at 1:35 pm (utc) on May 26, 2011]
[edit reason] Thread Tidy - 404 or permission required [/edit]

webprutser

10:25 pm on Feb 11, 2011 (gmt 0)

10+ Year Member



I took a look at your site. It makes me a bit dizzy, but no offense, taste differs.
As to your problem: do you have googles webdevelopers toolbar and firebug? If not, I can recommend it to you, for as you have and you put your cursor on the orange picture, rightclick your mouse and choose "inspect element", you'd see that your image doesn't get its style/lay-out from the specified div.
Unless you specify the img to "display:block" in CSS, to behave like a block element, you will need a span to specify things. Span is used for an inline-element (which an image is), div is used for a block-element. In a website I'm presently working on (almost online) I used an id in the img-tag to specify things and that works great, or a class when more images needed the same kind of lay-out. I'm not that familiar with positioning, but as far as I know when you use absolute positioning you should also specify the width of the element. But first you should take care of your img "listening to" your specifications (the id). Then you can look at the proper CSS to get what you want and if you still have problems, post it over here. Success.

alt131

5:38 am on Feb 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



@webprutser - you have been busy - and good spotting this thread didn't have a response :)
also second/third/fourth .. your suggestions to try the developers bar and fire-bug - especially firebug!

@Baelavay, welcome to WebmasterWorld [webmasterworld.com] :)

There is a lot going on in the page, which means lots of code to work through - one of the reasons for asking for a simplified code sample to make it easier to see where the problem is and help. But I've spotted a couple of things that may help:

The JavaScript is throwing a large number of errors and I had to step over each line to get the page to display, so you'll want to look at that separately.

There are two reasons your images aren't being positioned as you want: The first is that the second div containing the second image has no positioning applied to it - so it is just being drawn in the normal flow.

The second is the way top/left work. Basically left:50% means the browser scoots across 50% of the viewport, then starts to draw the element. So in your case the left edge of #index_welcomer-logo starts 1/2 way across - which means that most of the div - and the image is drawn further to the right. The easy fix is to drag the div back 1/2 its width using a negative margin-left. However, that means you have to state a width for the div or the image. You haven;t done that, but its good parctise anyway as it helps the browser lay out the page faster.

I've provided a code sample similar to what you have on your page, and a solution, but I'm a bit confused about why you want the second image stacked higher than the first image if it is to be a background, and there are other solutions that would work even better.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>My Title</title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<style type="text/css">

/*Give the html/body dimensions so the positioned elements have something to measure from*/
html, body {
height:100%;
}

/*I've made the table 100% to demonstrate how the images are being stacked underneath */
table {
width:100%;
height:100%;
/* a background-color plus opacity (which will only work in browsers other than ie) so you can see the images being stacked underneath*/
background-color:blue;
opacity:0.6;
}

/*Positioning the images.
As you want them in the same position you can use the same code, and the one last in the html will be stacked "on top" of the one before. Make sure you give them a width and a height though*/
div img {
position:absolute;
z-index:-1; /* this stacks the images under the the table without having to position and stack the table itself */
left:50%;
top:50%;
/*the trick to getting the images visually centered - note it is 1/2 the width/height of the images in the html */
margin-top:-100px;
margin-left:-100px;

}
</style>
</head>

<body>
<div>
<img src="index_welcomer-logo.jpg" width="200" height="200" alt="logo " >
<img src="index_welcomer-color.gif" width="200" height="200" alt="logo colour" >
</div>

<table>
<tr>
<td>cell</td>
<td>cell</td>
<td>cell</td>
<td>cell</td>
</tr>
</table>
</body>
</html>


@webprutuser. Absolute positioning can be handy, so a couple of points - applying absolute to an element converts the display to a form of block, so it behaves a bit like a block element without explicitly setting display:block, and even if originally inline (like a span or image) Rather than "having" to specify a width, this means the element will obey a width/height if it is explicitly set. You can see how I've taken advantage of that in the code sample.

Also, as you can see, there is no requirement to id the images as they can be accessed via the div. In this case it isn't even necessary to wrap them in a div - they could be coded directly into body - but that would be invalid html, so of course they are wrapped - and as I want block-ish behaviour, I've used a div as you explained.

webprutser

8:31 am on Feb 12, 2011 (gmt 0)

10+ Year Member



@alt131. The reason I responded to some threads, that got no anwer yet was primarily because I got such good help myself over here.
I can imagine the more "seasoned" CSS-er doesn't feel challenged by simpler problems, so I decided to see if I could be of any help.
Also thanks for the above explanation. I'm in the middle of changing from positioning by tables to the box-model and by bits and pieces I learn to understand it. I'll have to reread your input and try some things out to get a better understanding and I appreciate your comment.

alt131

5:34 am on Feb 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi webprutuser,
I really hope my comment didn't make you feel you had to justify posting - not at all, and I do apologise if it sounded like that.

The issue with answering is probably more about time than whether a question is "simple" (is any question ever simple in css ;) ). It is awful is a post gets missed, but we are all so short of time - which is why you taking time out of your day to post to unanswered threads semed such a nice thing to do - and makes an important and valuable contribution to the community as well.

... but please don't limit yourself to "simple" questions - please! I think many "regulars" get the greatest enjoyment from other member's posts - the more input ... the more ideas and perspectives ... the more interesting reading and thought provoking discussions.
Post away :)

webprutser

8:51 am on Feb 14, 2011 (gmt 0)

10+ Year Member



No, you didn't cause any bad-feelings at all. I just wanted to explain my motivation. Since I experienced the value of being helped, I thought it might be nice to see if I could be of help to someone else. Sticking to the simpler problems might be good practise, at least at this time. I'm not new to CSS, but have a lot to learn still especially when it comes to positioning (still haven't get rid of tables, although I'm somewhere in the middle of changing to the box model).

You make me laugh about the funny typing error you made in addressing me. It's webprutser, so without the second 'u' and the reason that's funny is because my name probably means nothing to English speaking persons. I felt like it when choosing a membername over here. That's all I'm going to say. :-)

alt131

9:22 am on Feb 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



At risk of a mod swooping in and deleting me for being off-topic.
I am relieved - and thank you for taking my typo with such good humour.
... my name probably means nothing to English speaking persons
For this one: didn't before ... does now ;)

Baelavay

10:08 pm on Feb 14, 2011 (gmt 0)

10+ Year Member



Thanks so much for your thorough replies!

The only thing that irritated me was that you said JavaScript is throwing a large number of errors. I can't simulate this.

Anyway, it's really heart-warming that there are helping people like you! I can't thank you enough :)

Baelavay

11:09 pm on Feb 14, 2011 (gmt 0)

10+ Year Member



Hey just for your interest: I have solved this with 2 separate pages now!

[edited by: alt131 at 6:44 am (utc) on May 19, 2011]
[edit reason] Thread Tidy [/edit]

alt131

9:07 am on Feb 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



@Baelavay Thanks for posting an update - and fantastic it's resolved.

webprutser

10:02 am on Feb 16, 2011 (gmt 0)

10+ Year Member



@alt131: about that name, I knew I took a risk by mentioning it. Internet can be your friend and your enemy. :-)

@ Baelavay. Great, things are solved now.