Solved

Center the logo picture

Posted on 2013-10-28
2
204 Views
Last Modified: 2013-10-29
I have some html that looks like this:


<div style="position:relative;">
     <img style="position:absolute; left:0; top:0;" src="background.png" />
     <img style="position:absolute; left:0; top:0;z-index:1" src="logo.png" />
</div>

How can I get the logo.png to be centered to the middle?

How can I calibrate where I want it in the up down diretction? I don't want to have it in the absolut top of the page, as with the background.png, but pushed down abit.
0
Comment
Question by:mdoland
  • 2
2 Comments
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 500 total points
ID: 39607922
You have to play with the image width.

So first thing get the image width. For the purpose of this example I'll assume it's 100px.
<div style="position:relative;">
     <img style="position:absolute; left:0; top:0;" src="background.png" />
     <img style="position:absolute; left:50%; top:10px;margin-left:-50px;z-index:1" src="logo.png" />
</div>

Open in new window

So lets see what I did...
Still position absolute inside a relative positioned container like you had.
Left: 50%; will put the image to start at the center of the container
margin-left:-50px; will move half of the image size to the left making it perfectly centered.

So, this negative margin-left if the main trick here and is where you need to know the correct size of the image so you can divide it in half and apply here.

Another foot note, use CSS files instead of in-line style.
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39607929
Another thing, the z-index shouldn't be needed as the logo.png already comes after the backgound.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

914 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

16 Experts available now in Live!

Get 1:1 Help Now