Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

vertically align image in center of div

  • 17-01-2010 05:12PM
    #1
    Registered Users, Registered Users 2 Posts: 112 ✭✭


    Folks,
    Any one has any idea, how can we make an image vertically centered in a div?
    Tagged:


Comments

  • Registered Users, Registered Users 2 Posts: 490 ✭✭Pablod


    Not 100%
    But try this
    {
     background-image: url(image-location);
     background-repeat: no-repeat;
     background-position: center;
    }
    

    Hope this helps


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    Officially
    {
     background-image: url(image-location);
     background-repeat: no-repeat;
     background-position: 50% 50%;
    }
    


  • Registered Users, Registered Users 2 Posts: 490 ✭✭Pablod


    Liam Byrne wrote: »
    Officially
    {
     background-image: url(image-location);
     background-repeat: no-repeat;
     background-position: 50% 50%;
    }
    

    Cheers Liam, still getting my own head around css.
    (self teaching is a *itch) ;)


  • Registered Users, Registered Users 2 Posts: 112 ✭✭leom


    Folks,
    I need to use img tag. I do not want to use css for backgroud image in div.
    I need to have alt tag with image as well.


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    leom wrote: »
    Folks,
    I need to use img tag. I do not want to use css for backgroud image in div.

    The only 3 ways to achieve this would be as follows:

    1) Server-side (e.g. PHP)

    - check the height of the image using getimagesize
    - subtract that from the height of the div, and divide the result by 2 to give a top margin for the IMG tag

    2) Javascript

    - Load the image with transparency 0
    - do roughly the same calculation after (img onload), and set the margin

    3) Put a single-cell table into the div and set vertical-align to center
    I need to have alt tag with image as well.

    Putting a title on the div should pretty much achieve this.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 112 ✭✭leom


    thanks Liam
    Liam Byrne wrote: »
    leom wrote: »
    Folks,
    I need to use img tag. I do not want to use css for backgroud image in div.

    The only 3 ways to achieve this would be as follows:

    1) Server-side (e.g. PHP)

    - check the height of the image using getimagesize
    - subtract that from the height of the div, and divide the result by 2 to give a top margin for the IMG tag

    2) Javascript

    - Load the image with transparency 0
    - do roughly the same calculation after (img onload), and set the margin

    3) Put a single-cell table into the div and set vertical-align to center



    Putting a title on the div should pretty much achieve this.


Advertisement