Is there a way to align things a set # of pixels from center?

Discussion in 'CSS' started by BoldlyGo, May 9, 2008.

  1. #1
    I want to set an image a few pixels right of center. Is there a way to do this?

    I don't want to use a percentage(say 60%). Because I have too objects that I want to align from center. If I use a percentage instead of pixels the objects will appear farther apart form eachother on larger displays.

    Originally I had an image that was text anda rocket. Now I'm trying to just have the rocket as an image, so that the image that has to be loaded is smaller.

    I'm having difficulty getting the title and image to be beside eachother in the center of the page.

    Any suggestions?
     
    BoldlyGo, May 9, 2008 IP
  2. Erind

    Erind Peon

    Messages:
    663
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    id do it:

    <div style="width:50%;float:left;">Div 1</div</div>
    <div style="width:100px;float:left;margin-left:10px;">Div 2</div>

    Place your stuff inside div2, and leave div 1. its juts a space filler that fills up the left side.
     
    Erind, May 9, 2008 IP
  3. BoldlyGo

    BoldlyGo Well-Known Member

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    106
    #3
    hmm.. I can't get that to work.

    This is what I have in my css at the moment:

    #h1 { font-size: 2.6em;
    color: #ffffff;
    background: url(images/boldlygotitle.png) 70% no-repeat;
    text-align: center;
    }


    #h1 h1 a {text-decoration: none;
    color: #ffffff;
    letter-spacing: -3px;
    font-size: 2.6em;}


    #h1 a:hover {text-decoration: none;
    color: #ffffff;}
     
    BoldlyGo, May 9, 2008 IP
  4. Erind

    Erind Peon

    Messages:
    663
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #4
    do u want to align an <h1> inside a div with id="h1"????

    What I gave you works... it sets a div on the left half of the screen and on the right sets another with width 100pixels, and 5pixels off the center.

    you can try doing this, which just splits the page in two.

    <div style="width:50%;float:left;text-align:left;">Div 1</div>
    <div style="width:50%;float:right;text-align:left;">Div 2</div>

    Placing stuff inside Div 2 will align stuff to the immediate right of the "center-line" of a page. Give the things inside this Div 2 a margin-left and they will be the "set" amount of pixels right of the center.
     
    Erind, May 9, 2008 IP
  5. BoldlyGo

    BoldlyGo Well-Known Member

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    106
    #5
    I only want to get it to work. :).

    Sorry for my ignorance, I don't understand what you are asking.

    This is supposed to go in my css right? Just making sure because the only divs that I have are in my php files.

    I tried placing the image in the Div 2 section. The image stopped showing up on my site.
     
    BoldlyGo, May 9, 2008 IP
  6. Erind

    Erind Peon

    Messages:
    663
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #6
    try this just in case you are floating other divs.
    <div style="width:100%">
    <div style="width:50%;float:left;text-align:left;"></div>
    <div style="width:50%;float:right;text-align:left;">
    <img src="IMAGE URL HERE" alt="" style="margin-left:5px;"/>
    </div>
    </div>

    if that doesnt work, post your website with my code in it so I see wher eit is and what is causing it not to work.
     
    Erind, May 9, 2008 IP
  7. Hades

    Hades Well-Known Member

    Messages:
    1,873
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    150
    #7
    You can set a width to your box, set it to
    margin:0 auto;
    Code (markup):
    and give it a position relative of X pixels.

    
    #box {
       width:300px;
       margin:0 auto;
       position:relative;
       left:Xpx; /* This can be right if you want. Doesn't really matter */
    }
    
    Code (markup):
     
    Hades, May 9, 2008 IP
  8. Erind

    Erind Peon

    Messages:
    663
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #8
    He said he didnt want them centered because hed put other stuff and the spacing would be wrong...

    something like that.
     
    Erind, May 9, 2008 IP
  9. Hades

    Hades Well-Known Member

    Messages:
    1,873
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    150
    #9
    Uhm..It's not centered. It is offset. It's centered using margin:0 auto; but then you offset it by how many pixels you want from the center using the combination of position:relative; and either left:XXpx; or right:XXpx;

    Regards,
    Nick
     
    Hades, May 9, 2008 IP
  10. Stomme poes

    Stomme poes Peon

    Messages:
    3,195
    Likes Received:
    136
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Yes, the element itself is centered and then it is visually offset (looks like it's off-center now, though not really).

    However, I noticed that the original code posted by BoldlyGo had only text-align: center, so unless margin: 0 auto was not used because he didn't really want it centered exactly, I dunno, but it occured to me there might be no doctype or one with something above the doctype like a space or text or something... in which case, at least in IE (even IE7) margin: 0 auto won't work... so, make sure there's a doctype too : ) or the technique Nick posted won't work.
     
    Stomme poes, May 13, 2008 IP