Centering Vertically

Discussion in 'CSS' started by dcristo, Aug 18, 2009.

  1. #1
    Everything I have tried has failed miserably.

    I want to vertically align some text and an image within an element.

    I need it to be compatible in both IE and Firefox.

    Thanks
     
    dcristo, Aug 18, 2009 IP
  2. AssistantX

    AssistantX Peon

    Messages:
    173
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Without a link to your website, I do not know how your page is structured. However using CSS property "vertical-align: middle;" for the parent element of whatever elements you want vertically centered should work.
     
    AssistantX, Aug 18, 2009 IP
  3. dcristo

    dcristo Illustrious Member

    Messages:
    19,776
    Likes Received:
    1,200
    Best Answers:
    7
    Trophy Points:
    470
    Articles:
    7
    #3
    I tried that and it didn't work.

    Let's say the code is structured like this.

    
    <div>
    <p><img src="/images/pic.jpg" align="left">Some text here which I want to be vertically centered.</p>
    </div>
    
    Code (markup):
     
    dcristo, Aug 18, 2009 IP
  4. AssistantX

    AssistantX Peon

    Messages:
    173
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    "vertical-align: center;" works best for tables. For other element, the below should work.

    -Edit: Revised
    
    <div style="height: 200px;">
      <p style="position: relative; top: 50%;">
        <span style="position: relative; top: -50%;">
          <img src="http://www.google.com/favicon.ico">
          Some text here which I want to be vertically centered.
        </span>
      </p>
    </div>
    
    HTML:
     
    Last edited: Aug 18, 2009
    AssistantX, Aug 18, 2009 IP
  5. dcristo

    dcristo Illustrious Member

    Messages:
    19,776
    Likes Received:
    1,200
    Best Answers:
    7
    Trophy Points:
    470
    Articles:
    7
    #5
    I can't get anything to work. If it was a table and I apply it to the td element, what code would I use exactly?
     
    dcristo, Aug 18, 2009 IP
  6. AssistantX

    AssistantX Peon

    Messages:
    173
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The code I posted above should work in most situations if you implement it properly. What code did you use?

    For tables:
    
    <table>
      <tr>
        <td style="height: 200px; width: 20px; vertical-align: middle;">
          Hello test
        </td>
      </tr>
    </table>
    
    HTML:
    The reason I set a height in both situations is because in order to see the vertical aligning, you must have a height. By default, table cell are vertically aligned middle.
     
    AssistantX, Aug 18, 2009 IP
  7. dcristo

    dcristo Illustrious Member

    Messages:
    19,776
    Likes Received:
    1,200
    Best Answers:
    7
    Trophy Points:
    470
    Articles:
    7
    #7
    The problem is when you align="left" the image the text alignment moves to the top of the image, it's not vertically centered.
     
    dcristo, Aug 18, 2009 IP
  8. AssistantX

    AssistantX Peon

    Messages:
    173
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #8
    align="left" is not valid HTML4/XHTML. CSS "text-align: center;" would be the proper replacement to align inline elements. Also, you cannot align direct child elements to different sides in the same child element using align properties. To do this, you can use floats. The code code below floats the image left, and centers the text.

    
    <div style="height: 200px;">
      <p style="position: relative; top: 50%; text-align: center;">
        <span style="position: relative; top: -50%; overflow: auto;">
          <img style="float: left;" src="http://www.google.com/favicon.ico">
          <span style="text-align: center;">Some text here which I want to be vertically centered.</span>
        </span>
      </p>
    </div>
    
    HTML:
     
    AssistantX, Aug 18, 2009 IP
  9. dcristo

    dcristo Illustrious Member

    Messages:
    19,776
    Likes Received:
    1,200
    Best Answers:
    7
    Trophy Points:
    470
    Articles:
    7
    #9
    I appreciate the help, but I give up dude. All that did was center the text horizontally.
     
    dcristo, Aug 18, 2009 IP
  10. AssistantX

    AssistantX Peon

    Messages:
    173
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #10
    It would really help the process if you showed the exact code/page you were working with. One of the methods above should work fine. However, if you are incorporating other elements and controls into the page, then I need to see those and adapt to those.

    If you look here: Test Output
    You will so both methods above at work.

    Editable Version
     
    AssistantX, Aug 18, 2009 IP
  11. kk5st

    kk5st Prominent Member

    Messages:
    3,497
    Likes Received:
    376
    Best Answers:
    29
    Trophy Points:
    335
    #11
    Please see vertical centering of inline elements. This demo/tutorial works on IE6-8, and modern browsers.

    In the Practical Examples, Simple Text and Images, remove the br element, and style the image to float. You'll have exactly what you asked for.

    Do read the explanation. The work-around for IE6/7 is not intuitive, and will require some thought. For modern browsers, this is trivial.

    cheers,

    gary
     
    kk5st, Aug 18, 2009 IP