Go Back   CodingForums.com > :: Client side development > HTML & CSS

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-06-2013, 12:43 AM   PM User | #1
kin
New Coder

 
Join Date: Jul 2011
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
kin is an unknown quantity at this point
CSS position question.

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:

Code:
<!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:

Code:
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.
kin is offline   Reply With Quote
Old 01-06-2013, 01:34 AM   PM User | #2
minkoko
New Coder

 
Join Date: Aug 2010
Location: myeik
Posts: 72
Thanks: 4
Thanked 5 Times in 5 Posts
minkoko can only hope to improve
how to want the position?

like this
PHP Code:
<!DOCTYPE html>
<
html>
    <
head>
        
        <
title>Result</title>
                <
style type="text/css">

#outer {
    
height200px;
    
width450px;
    
background-color#45ADA8;
        
position:absolute;
    
margin-left100px;
        
border-radius5px;
}
#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
__________________
Quote:
Myanmar Web Developer & Designer
http://www.cyberoot.com
minkoko is offline   Reply With Quote
Old 01-06-2013, 01:39 AM   PM User | #3
kin
New Coder

 
Join Date: Jul 2011
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
kin is an unknown quantity at this point
That made no sense.
kin is offline   Reply With Quote
Old 01-06-2013, 03:31 AM   PM User | #4
Excavator
Master Coder


 
Excavator's Avatar
 
Join Date: Dec 2006
Location: Alaska
Posts: 9,410
Thanks: 22
Thanked 1,765 Times in 1,749 Posts
Excavator has a spectacular aura aboutExcavator has a spectacular aura aboutExcavator has a spectacular aura about
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 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 -
Code:
<!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>
__________________
Validate often DURING development - Use it like a splelchecker | Debug during Development |Write it for FireFox, ignore IE
Use the right DocType | Validate your markup | Validate your CSS | Why validating is good | Why tables are bad
Excavator is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:22 PM.


Advertisement
Log in to turn off these ads.