...

View Full Version : CSS position question.



kin
01-06-2013, 01:43 AM
I read that the position property uses the first parent it finds that is not static to position from. The website I read this off of was codeacademy, and so I decided to test this. I have the following HTML:


<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles/index.css" />
<title>Result</title>
</head>
<body>
<div id="outer"><div id="inner"></div></div>
</body>
</html>


So there is an outer div and an inner div, I set the outer div to absolute so that I can test absolute first. I then have the following CSS:


div {
border-radius: 5px;
border: 2px solid black;
}

#inner {
height: 75px;
width: 75px;
background-color: #547980;
position:absolute;
margin-left:200px;
}

#outer {
height: 500px;
width: 150px;
background-color: #45ADA8;
position:absolute;
margin-left: 100px;
}


Now this site told me to use margin-left to position it. So I followed what the site showed me, and I set outer to absolute. I then set inner to absolute also, and made the margin go 200px from the left. Now absolute is supposed to use the first parent that is not static, and so I decided to test this and remove the position:absolute from the outer scope. I did this and the styling did not change, which I found quite awkward since the site told me that it would be positioned relative to the first parent that was not static. The next part told me relative will position based on where it would be when static, and this to my amazement did not function like they said, and functioned just like absolute did. So I'm confused at this point, and have no clue why this does not work. So I came here to ask how does position work, and is this site correct with the results?



tl;dr : Read it, and quit being lazy.

minkoko
01-06-2013, 02:34 AM
how to want the position?

like this

<!DOCTYPE html>
<html>
<head>

<title>Result</title>
<style type="text/css">

#outer {
height: 200px;
width: 450px;
background-color: #45ADA8;
position:absolute;
margin-left: 100px;
border-radius: 5px;
}
#inner
{
width:100px;
height:100px;
background-color: #333333;
border-radius:5px;
margin:10px 10px;
position:absolute;
}

</style>

</head>
<body>
<div id="outer"><div id="inner"></div></div>
</body>
</html>

kin
01-06-2013, 02:39 AM
That made no sense.

Excavator
01-06-2013, 04:31 AM
Hello kin,
I wonder what site your getting your information from. I think margin is not the way to position an ap element.

Have a quick look at this positioning tutorial (http://www.barelyfitz.com/screencast/html-training/css/positioning/) to see what I mean.

In this example, remove position: relative; from the parent and the child element positions itself relative to the body of the document -
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Result</title>
<style type="text/css">
div {
border-radius: 5px;
border: 2px solid black;
}
#outer {
height: 500px;
width: 150px;
margin-left: 100px;
position: relative;
background: #45ADA8;
}
#inner {
height: 75px;
width: 75px;
position: absolute;
top: 0;
left: 200px;
background: #547980;
}
</style>
</head>
<body>
<div id="outer">
<div id="inner">
<!--end inner--></div>
<!--end outer--></div>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum