Thread: Left % - px ??

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    33
    Rep Power
    4

    Left % - px ??


    I want to keep my div in the center of the page so I used
    left: 50%

    but that keeps the left side at 50% rather than the corner, so I was thinking of subtracting half the div which is 300px to make it centered. so like
    left: 50% - 150px

    but that doesnt work. So any ideas how to make this work or a better way to do it.

    EDIT: Ah nevermind I figured it out, I had somehow embedded a position: absolute in my html file which was screwing everything off. Thanks for the help.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    there is no subtraction (or any other calculation) in CSS. Not sure where you got this from.

    Block elements are centered by setting the left and the right margin to "auto", as a Google search would have told you.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    33
    Rep Power
    4
    Originally Posted by Jacques1
    Hi,

    there is no subtraction (or any other calculation) in CSS. Not sure where you got this from.

    Block elements are centered by setting the left and the right margin to "auto", as a Google search would have told you.
    I know very well how to use google, I set the left and right margin to "auto" and they did not work either. Also I know that css does not do calculation I was simply showing you my logic so you could understand what I was trying to accomplish.

    EDIT: Nevermind I found out there was a position: absolute hiding in my html file that was messing up everything. Thanks for the help.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by harrjm
    I know very well how to use google, I set the left and right margin to "auto" and they did not work either.
    Well, that would have been important info.

    The problem is that many people actually do not know how to Google (or they're simply too lazy). So when you ask a very general question like "How do I center a div element?", you're likely to get a RTFM.

    But when you ask a specific question with concrete info, it's much easier and more appealing to give a specific answer. I mean a problem description like this: "I tried centering a div with margin: auto, but it's still aligned to the left. Here's the full CSS: ..., Here's a screenshot: ..."

    Remember that we don't know what you know or see what you see. So it all comes down to your problem description.

    Just a suggestion ...
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo