Forum Moderators: not2easy
<html>
<head>
<style type="text/css">
body
{ text-align:center;}
#wrapper
{ position:relative;
text-align:left;
margin: 0 auto;
width: 1010px;
}
</style>
</head>
<body>
<div id="wrapper">
<div style="position:relative;left:0px;top:0px;width:1010px;
height:90px;background-color:cyan;color:#000000;">
<form name="Foo" style="position:absolute;left:662px;top:53px;">
<input type="text" name="usedForID" size="43"
value=" Keyword, Product Name, Product Code"
onclick="if(this.value=='Enter Keyword or Product Code')
{this.value='';}"/>
</form>
<div style="position:absolute;left:930px;top:53px;width:45px;height:23px;">
<a href="">
<img src="search_go.gif"
alt="" width="45" height="23"/></a>
</div>
</div>
</div>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> Page Title </title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<style type="text/css" media="screen">
body {text-align:center;}
#wrapper {
position:relative;
text-align:left;
margin: 0 auto;
width: 1010px;
}
#header {
height:90px;
background-color:cyan;
color:#000000;
}
#foo {
position: absolute;
right: 10px;
top: 53px;
width: 320px;
height: 30px;
background: #ffc; /* temporary to see form element */
}
#usedForID {
float: left;
width: 260px;
height: 19px; /* to match up with image height */
line-height: 18px; /* just to centralise text in input box */
}
#foo .go {float: left; width: 45px;}
#foo .go img {border: 0;}
</style>
</head>
<body>
<div id="wrapper">
<div id="header">
<form name="foo" id="foo">
<input type="text" name="usedForID" id="usedForID" size="43" value=" Keyword, Product Name, Product Code" onclick="if(this.value=='Enter Keyword or Product Code'){this.value='';}"/>
<a class="go" href="#"><img src="search_go.gif" alt="#" width="45" height="23"/></a>
</form>
</div>
</div>
</body>
</html>
Firstly, are there any rules as to where to use position:absolute and where to use position:relative?
position:absolute takes the whole element, whichever element you use, and subsequently it's children, out of the flow of the page completely. I used to say to think about the AP element like a post-it note you've lifted from the page and then stuck onto the screen, all other content will ignore it and go under it. So in the above case all I did was take the entire form and remove it, there was then no need to absolutely position the elements inside it, they are just like the writing on the post-it note, they will move with the sticker, if you like..
position: relative serves two quite different purposes really, <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> Offset Paragraph </title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<style type="text/css" media="screen">
#wrap {
width: 500px;
background: #eee;
border: 1px solid 000;
margin: 40px;
}
p {margin: 0; background: #dad;}
.offset {position: relative; left: -20px; top: -20px;}
</style>
</head>
<body>
<div id="wrap">
<p class="offset">This is the offset paragraph</p>
<p>This is the normal paragraph</p>
</div>
</body>
</html>
position:relative; with or without "offset" co-ordinates in order to apply a z-index. position:relative; is to make an element into a "Containing Block [w3.org]. This affects your absolutely positioned (AP) elements.. if you don't specify a containing block your AP elements will position themselves according to the default, the viewport. It's very difficult, if not impossible, to absolutely position elements, to a flexible width design, without it! ...you mentioned that I use too many divs. I have a bad habit of placing each
element inside a div and placing that div on the page. I wonder if there are any rules regarding when to place and not place elements in divs.