Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    kin
    kin is offline
    New Coder
    Join Date
    Jul 2011
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    New Coder
    Join Date
    Aug 2010
    Location
    myeik
    Posts
    81
    Thanks
    6
    Thanked 5 Times in 5 Posts
    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

  • #3
    kin
    kin is offline
    New Coder
    Join Date
    Jul 2011
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That made no sense.

  • #4
    Master Coder Excavator's Avatar
    Join Date
    Dec 2006
    Location
    Alaska
    Posts
    9,672
    Thanks
    22
    Thanked 1,827 Times in 1,811 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •