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 13 of 13
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Unhappy How to avoid Negative values for top parameter in CSS

    Hi,

    I was trying divs in CSS today and stuck with the below problem of using negative values for "top" attribute to make them look how I want them to be.

    The code is as below
    ============================================
    <html>
    <head>
    <style type="text/css">

    body{ background-color: white; height: 600px;}

    h3 {color: white;}

    .Header{
    position: relative;
    top: 0px;
    left: 150px;
    border-style: ridge;
    width: 970px;
    height: 120px;
    }

    .Block1{
    position: relative;
    top: 0px;
    left: 150px;
    border-style: ridge;
    width: 970px;
    height: 120px;
    }

    .Block2{
    position: relative;
    top: 0px;
    left: 150px;
    border-style: ridge;
    width: 550px;
    height: 600px;
    }

    .Block3{
    position: relative;
    top: -600px;
    left: 700px;
    border-style: ridge;
    width: 420px;
    height: 120px;
    }

    .Block4{
    position: relative;
    top: -600px;
    left: 700px;
    border-style: ridge;
    width: 420px;
    height: 480px;
    }

    .TrailerBlock{
    position: relative;
    top: -600px;
    left: 150px;
    border-style: ridge;
    width: 970px;
    height: 120px;
    }

    </style>
    </head>

    <body class="HtmlBody">

    <div class='Header'>



    </div>
    <div class='Block1'>

    </div>
    <div class='Block2'>



    </div>
    <div class='Block3'>



    </div>
    <div class='Block4'>



    </div>
    <div class='TrailerBlock'>


    </div>
    </body>
    </html>
    ============================================

    If we open this html in IE, we can see well formatted div blocks but to achieve this I had to tweak the "top" parameters with negative values of pixels, can I do this without using negative values ?

    Thanks,
    Deepna

  • #2
    Regular Coder
    Join Date
    Jul 2011
    Location
    India
    Posts
    496
    Thanks
    3
    Thanked 57 Times in 56 Posts
    Sorry I dint understand, what you want to achieve and why you are using negative value???

    Can you share mock up of design, how it should look like??

    Please add DOCTYPE at starting of HTML code. Following is the example:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  • #3
    New Coder
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Hi,

    If you see the below div blocks, the top is set as -600px, is this the right way of defining div positions using negative pixel values and relative mode ?

    .Block3{
    position: relative;
    top: -600px;
    left: 700px;
    border-style: ridge;
    width: 420px;
    height: 120px;
    }

    .Block4{
    position: relative;
    top: -600px;
    left: 700px;
    border-style: ridge;
    width: 420px;
    height: 480px;
    }

    Thanks,
    Deepna

  • #4
    Regular Coder
    Join Date
    Aug 2011
    Location
    U.S.A.
    Posts
    233
    Thanks
    2
    Thanked 48 Times in 48 Posts
    when using position:relative, you are positioning the element based on it's position in the layout. So the top: 0 and left: 0 would keep it in the exact same position as it was. If you want to move it up, you have to use a negative value in the "top" and to move it further left, you have to use a negative value in the "left" field.

  • #5
    The Apostate Apostropartheid's Avatar
    Join Date
    Oct 2007
    Posts
    3,215
    Thanks
    16
    Thanked 265 Times in 263 Posts
    Quote Originally Posted by resdog View Post
    when using position:relative, you are positioning the element based on it's position in the layout. So the top: 0 and left: 0 would keep it in the exact same position as it was. If you want to move it up, you have to use a negative value in the "top" and to move it further left, you have to use a negative value in the "left" field.
    Half true.

    The right and bottom properties have the same effect as negative left and top values respectively.

  • #6
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Sorry, but why is it that relative positioning with top/bottom/left/right settings is needed at all for this layout? The only thing you need is a float or two and you can leave everything with static positioning (as it is by default). In my experience using any top/bottom/left/right setting with position relative is a recipe for disaster.

    Give this a look and see what you think:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>Layout Test</title>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <style type="text/css">
    
    *{margin:0;padding:0;}
    
    body{ background-color: white; height: 600px;width:976px;margin:0 auto;}
    
    h3 {color: white;}
    
    .header{
    border-style: ridge;
    width: 970px;
    height: 120px;
    }
    
    .block1{
    border-style: ridge;
    width: 970px;
    height: 120px;
    }
    
    .block2{
    border-style: ridge;
    width: 544px;
    height: 606px;
    float:left;
    }
    
    .block3{
    border-style: ridge;
    width: 420px;
    height: 120px;
    margin-left:550px;
    }
    
    .block4{
    border-style: ridge;
    width: 420px;
    height: 480px;
    margin-left:550px;
    }
    
    .trailer_block{
    border-style: ridge;
    width: 970px;
    height: 120px;
    clear:left;
    }
    
    
    
    </style>
    </head>
    
    <body class="html_body">
    
    <div class='header'>
    
    
    
    </div>
    <div class='block1'>
    
    </div>
    <div class='block2'>
    
    
    
    </div>
    <div class='block3'>
    
    
    
    </div>
    <div class='block4'>
    
    
    
    </div>
    <div class='trailer_block'>
    
    
    </div>
    </body>
    </html>
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    deepna (09-13-2011)

  • #7
    New Coder
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Hi Rowsdower - when i look at your html in IE (Version 7.0), I can see that the block 3 and 4 are out of sync, they are not at the same level as block 2, how can we bring these blocks up to the same level as block 2?

    If you see my html which i had posted earlier, the way these blocks are set is different that what comes out of your layout.

    Thanks,
    Deepna

  • #8
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,753
    Thanks
    41
    Thanked 191 Times in 190 Posts
    I am confused what you are going for as far as layout (and Rows may be as well)... this is essentially 6 blocks placed on a page... can you do a real quick paint jpg of six blocks and label what block is what and then we can help you get them in the right spot the right way

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #9
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Quote Originally Posted by deepna View Post
    Hi Rowsdower - when i look at your html in IE (Version 7.0), I can see that the block 3 and 4 are out of sync, they are not at the same level as block 2, how can we bring these blocks up to the same level as block 2?

    If you see my html which i had posted earlier, the way these blocks are set is different that what comes out of your layout.

    Thanks,
    Deepna
    That's just a problem with the IE box model handling borders differently than Firefox (which is what I used when I wrote up the example). You're going to have issues like that to deal with if you truly want borders on every block of the page.

    In the meantime try this instead:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>Layout Test</title>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <style type="text/css">
    
    *{margin:0;padding:0;border:0;}
    
    body{ background-color: white; height: 600px;width:976px;margin:0 auto;}
    
    h3 {color: white;}
    
    .header{
    border-style: ridge;
    width: 970px;
    height: 120px;
    background-color:#ace;
    }
    
    .block1{
    border-style: ridge;
    width: 970px;
    height: 120px;
    background-color:#f00;
    }
    
    .block2{
    border-style: ridge;
    width: 550px;
    height: 600px;
    float:left;
    background-color:#a4a4a4;
    }
    
    .block3{
    border-style: ridge;
    width: 420px;
    height: 120px;
    margin-left:550px;
    background-color:#0f0;
    }
    
    .block4{
    border-style: ridge;
    width: 420px;
    height: 480px;
    margin-left:550px;
    background-color:#00f;
    }
    
    .trailer_block{
    border-style: ridge;
    width: 970px;
    height: 120px;
    clear:left;
    background-color:#055;
    }
    
    </style>
    </head>
    <body class="html_body">
    <div class='header'>
      <h1>Header</h1>
    </div>
    <div class='block1'>
      <h1>Block 1</h1>
    </div>
    <div class='block2'>
      <h1>Block 2</h1>
    </div>
    <div class='block3'>
      <h1>Block 3</h1>
    </div>
    <div class='block4'>
      <h1>Block 4</h1>
    </div>
    <div class='trailer_block'>
      <h1>Trailer Block</h1>
    </div>
    </body>
    </html>
    Same idea, but a different way of showing you where the blocks are since borders are a bit dodgy to work with...
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    deepna (09-13-2011)

  • #10
    New Coder
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Hi Rowsdower - this is exactly what i was looking for, but I dont understand how the block 4 fit in below block 3 ? instead of block 4 fitting immediately after block 3 in the same level ?

    I was using top attribute to bring the block 4 below block 3.

    Alykins - the html that Rowsdower has created is the format i was looking for but I was using borders to differentiate div's instead of colors.

    Thanks,
    Deepna

  • #11
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Quote Originally Posted by deepna View Post
    Hi Rowsdower - this is exactly what i was looking for, but I dont understand how the block 4 fit in below block 3 ? instead of block 4 fitting immediately after block 3 in the same level ?

    I was using top attribute to bring the block 4 below block 3....
    If we were to remove block 2 from the layout - or even just to give it display:none; for now - we would see that block 3 and block 4 are still stacked one entirely above the other. There is no overlap and they don't slide in side-by-side. This is the natural behavior of block-level elements (i.e. elements with display:block). Your use of posibiton:relative with top/bottom/left/right values was disturbing the natural course of the block element behavior by artificially rolling boundaries up above where they should have been.

    You can break this layout again by doing something like this, which will show you again why postion:relative with top/bottom/left/right values is not usually a good idea:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>Layout Test</title>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <style type="text/css">
    
    *{margin:0;padding:0;border:0;}
    
    body{ background-color: white; height: 600px;width:976px;margin:0 auto;}
    
    h3 {color: white;}
    
    .header{
    border-style: ridge;
    width: 970px;
    height: 120px;
    background-color:#ace;
    }
    
    .block1{
    border-style: ridge;
    width: 970px;
    height: 120px;
    background-color:#f00;
    }
    
    .block2{
    border-style: ridge;
    width: 550px;
    height: 600px;
    float:left;
    background-color:#a4a4a4;
    }
    
    .block3{
    border-style: ridge;
    width: 420px;
    height: 120px;
    margin-left:550px;
    background-color:#0f0;
    position:relative;
    top:100px;
    }
    
    .block4{
    border-style: ridge;
    width: 420px;
    height: 480px;
    margin-left:550px;
    background-color:#00f;
    position:relative;
    }
    
    .trailer_block{
    border-style: ridge;
    width: 970px;
    height: 120px;
    clear:left;
    background-color:#055;
    }
    
    </style>
    </head>
    <body class="html_body">
    <div class='header'>
      <h1>Header</h1>
    </div>
    <div class='block1'>
      <h1>Block 1</h1>
    </div>
    <div class='block2'>
      <h1>Block 2</h1>
    </div>
    <div class='block3'>
      <h1>Block 3</h1>
    </div>
    <div class='block4'>
      <h1>Block 4</h1>
    </div>
    <div class='trailer_block'>
      <h1>Trailer Block</h1>
    </div>
    </body>
    </html>
    Here suppose we gave block 3 a top position to "fix" something that we wanted to fix. But now block 4 will overlap block 3, but it's not block 4's "fault" that this happens. So when you try to "fix" block 4 you end up messing up the trailer block, and then when you try to fix that... well the whole thing becomes a death spiral.

    Does that help to explain it?
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    deepna (09-13-2011)

  • #12
    New Coder
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thanks it helps, though I need to read more on CSS to get a grip of it, CSS is not my forte - any good links that you suggest reading on absolute and relative positioning in CSS ?

    Thanks,
    Deepna

  • #13
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    I do make use of position:absolute; (and even position:fixed; from time to time). But, to be perfectly honest, I can't think of any time that I use position:relative; for anything OTHER THAN:

    1) making an element contain a child element that I want to use position:absolute; on
    2) giving z-index to something that needs it

    Those two things are pretty much the only reasons that will ever give an element position:relative;. And even when I do that, under no circumstances do I ever use top/bottom/left/right with position:relative;. It just doesn't happen when I code and I have never found a problem that couldn't be solved a better way.

    So I don't think that you have much to study up on for position:relative;. In my opionion it is just a "helper" style that allows you to make use of other CSS features. As for position:absolute; - well...

    I learned by reading and playing, basically. You can always take a look here:
    http://www.w3schools.com/cssref/pr_class_position.asp

    and dive into the examples to see what happens when you try one thing or another. Unforunately a lot of finer points to CSS are just learning through trial and error what works in which situations.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting


  •  

    Posting Permissions

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