1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Difference between left: 10px and padding-left: 10px

Discussion in 'CSS' started by fatabbot, Sep 9, 2007.

  1. #1
    Does anyone know what the difference between the css code: left:10px and padding-left:10px is?
    It gives both different results. left is also not the same as margin-left appearantly.
     
    fatabbot, Sep 9, 2007 IP
  2. soulscratch

    soulscratch Well-Known Member

    Messages:
    964
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    155
    #2
    As far as I know, top/left/right/bottom are used on elements that have been given a value for property other than static (absolute, fixed, relative).

    So something like

    p { left:10px; } will not work at all since the paragraph's position is by default static. If you give it a value of relative, absolute, or fixed then it would work as expected

    p { position:relative; left:10px; }

    You should not be using left/top/bottom/right but instead margin/padding so that the element is in normal flow.

    I would only resort to absolute positioning if I wanted something that wasn't important in source order but had to appear say, at the top of the page, and with absolute positioning I could do that.. place the item at the bottom of the source but visually with top:0; right:0; and a position value of absolute (relative on the container) it would appear at the top right.
     
    soulscratch, Sep 9, 2007 IP
    fatabbot likes this.
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #3
    Ok, let's break this down:

    padding - adds space TO the render box. Width:300px; padding:10px; = 320px wide box.

    margin - sets a minimum space AROUND the render box. (and with negatives can shrink the space) Width:300px; margin:10px; = 300px box with a minimum of 20px spacing around it.

    top/left/bottom/right when combined with position:relative - moves the whole box WITHOUT changing it's position in flow. Width:300px; position:relative; left:10px; = 300px wide box, the rendered content on screen 10px to the left of where the engine will treat it for determining flow.

    top/left/bottom/right when combined with position:absolute - removes the box from flow and positions it absolutely based on either a relative or absolute positioned container, or the screen if no positioned container is present.

    top/left/bottom/right when combined with position:static - positions the element absolutely to the screen (not available in all browsers)

    Five different behaviors... five different properties.

    Realistically position:relative with left should be used sparingly for minor adjustments, if at all. It's just too unreliable cross browser and leads to more layout headaches than it ever solves.

    Much the same can be said for position:absolute for anything that SHOULD be a flow element. If it's to position something inside a container like an anchor or header, fine... but using it on a column is a total /FAIL/

    99.99% of the time padding or margins will do the job with a LOT less headaches.
     
    deathshadow, Sep 9, 2007 IP
    fatabbot likes this.
  4. fatabbot

    fatabbot Well-Known Member

    Messages:
    559
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    138
    #4
    thx, useful info.
     
    fatabbot, Sep 13, 2007 IP