Solved

How to vertically center an image?

Posted on 2009-06-30
10
319 Views
Last Modified: 2012-05-07
I'd like to vertically center an image within a div.  I am using the HTML below but with no luck.  What am i doing wrong?
<div style="float:left; height:50%; margin-bottom:-250px;"></div>

            <div style="position:relative;left:20px;clear:both; height:500px;">

                <img alt="logo" src="images/logo.png" />

            </div>

Open in new window

0
Comment
Question by:abemiester
10 Comments
 
LVL 5

Expert Comment

by:Jagarm
ID: 24749948
use:

      vertical-align:middle;
0
 

Author Comment

by:abemiester
ID: 24749957
That doesn't work either
0
 
LVL 8

Expert Comment

by:lharrispv
ID: 24750124
what about margin:0 auto; text-align:center; or perhaps the same but with a fixed width
0
 
LVL 15

Accepted Solution

by:
Daydreams earned 500 total points
ID: 24750412
Use the background-position property:

http://www.w3schools.com/css/pr_background-position.asp
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 5

Expert Comment

by:sscotti
ID: 24750637
Look at this and see if this helps:

http://www.dynamicsitesolutions.com/css/vertically-centering-with-line-height/

You should be able to center text, images and in-line elements using line-height.
0
 
LVL 5

Expert Comment

by:sscotti
ID: 24778364
BTW,  the DOCTYPE needs to be specified as:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">  

or

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

in order for line-height and vertical-align:middle to work.

0
 
LVL 1

Expert Comment

by:Nati4jc
ID: 24781650
i just put the image in a table and it works fine in FF, IE6, Opera and Safari:

<div style="float:left; height:50%; margin-bottom:-250px;"></div>
            <div id="div" style="position:relative;left:20px;clear:both; height:500px; background-color:#FF3300;">
             <table width="100%" height="100%" cellpadding="0" cellspacing="0"><tr><td valign="middle">   <img src="images/logo.png" alt="logo" name="image" id="image" />   </td></tr></table>         </div>
0
 
LVL 15

Expert Comment

by:Daydreams
ID: 24940670
Hi abemiester, I'm glad my solution helped. I wonder why the B grade? If you needed any additional help I would have been happy to give you additional assistance.

About grading and the opportunity to answer your needs: http://www.experts-exchange.com/help.jsp?hi=403

You can change the grade by clicking the "request attention" button: http://www.experts-exchange.com/help.jsp?hi=404

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now