putting box in center of screen, java

Discussion in 'HTML & Website Design' started by Benjamins, Aug 24, 2009.

  1. #1
    Hi all,

    I'm creating my first website. I want to put a box to middle of the screen. I thought I just need to get the screen-width to get the margin on one side of the box to place it into the middle:

    <script type="text/javascript">
    var embreite = screen.width/16; /* 1em is 16 pixel, isn't it ?? */
    var boxbreite = 70; /* width of the box */
    var rand = embreite-boxbreite;
    var randleft = rand/2;
    </script>

    But when I take "randleft" as the margin-left of the box, it is still not in the center, just a little bit more right.
    What am I doing wrong?
     
    Benjamins, Aug 24, 2009 IP
  2. Benjamins

    Benjamins Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    it was probably a stupid question, but can anyone help?
     
    Benjamins, Aug 26, 2009 IP
  3. Nerv

    Nerv Active Member

    Messages:
    568
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    70
    #3
    Did you try just using <center>your script</center> ?
     
    Nerv, Aug 26, 2009 IP
  4. Benjamins

    Benjamins Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    OK, that was actually a simple solution then. Thanks.
    And this is supported by quite all browsers?
     
    Benjamins, Aug 26, 2009 IP
  5. Nerv

    Nerv Active Member

    Messages:
    568
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    70
    #5
    I believe it is ;)
     
    Nerv, Aug 26, 2009 IP
  6. HighRankingSEO

    HighRankingSEO Well-Known Member

    Messages:
    423
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    125
    #6
    yea and thats a super old school tag...been around since html 1...lol
     
    HighRankingSEO, Aug 26, 2009 IP
  7. Benjamins

    Benjamins Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Or maybe not ...

    <center> Deprecated.

    have a look at:

    http://www.tutorialspoint.com/html/html_tags_ref.htm
     
    Benjamins, Aug 27, 2009 IP
  8. Nerv

    Nerv Active Member

    Messages:
    568
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    70
    #8
    It is deprecated but it works and is's supported by all browsers so what is the problem ?
     
    Nerv, Aug 27, 2009 IP
  9. CCD

    CCD Peon

    Messages:
    330
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #9
    If it's deprecated then browser makers *could* drop support for the element in future.

    I doubt <center> will validate as well-formed HTML if it's deprecated, don't know if you are concerned about that sort of thing?

    Another way to center a box horizontally is to use CSS:

    <div style="width:300px;margin-left:auto;margin-right:auto;">your stuff</div>
     
    CCD, Aug 27, 2009 IP