HTML Tutorial


 /help/HTML Help Forum   FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
RegisterRegister - Not registered yet? Got something to say? Join HTML Code Tutorial!
Centering text on image in same container
Post new topic   This topic is locked: you cannot edit posts or make replies.    HTML Help Forum -> Member Tutorials
View previous topic :: View next topic  
Author Message
sticks464



Joined: 31 Dec 2006
Posts: 2629

PostPosted: Sat Sep 12, 2009 10:44 pm     Centering text on image in same container Reply with quote

Getting text to vertically center on an image can be a little tricky at times. Here are two methods for centering.

Text and image in same container without using floats:

The first thing needed is a container with an image and some text.
Code:
<p class="test">
<img src="image.gif" alt="" width="55" height="25" alt="" />Some text here
</p>

The text now appears at the bottom right corner of the image. So let's add a css rule to bring it up to center. But we don't add the rule for the text, rather to the image.
Code:
.test img {
vertical-align:middle;
}

Now the text has moved up to vertically center on the image. Add a little margin to the image to move the text away on the right side.
Code:
.test img {
vertical-align:middle;
margin-right:10px;
}

And there you have text vertically centered on an image without using floats or padding.

-------------------------------------------------------------

Text left and right with image vertically and horizontially centered:

This time were going to need floats, spans and a little ie trick.
When using floats in a container they must be cleared or they go outside the container. To clear the floats, the centered image must comes after the floats.
Code:
<p class="test">
<span class="left"><a href="">Link #1</a> | <a href="">Link #2</a></span>
<span class="right"><a href="">Link #3</a> | <a href="">Link #4</a></span>
<img src="image.gif" alt="" width="55" height="25" />
</p>

Notice the text links I'm using are to the bottom left of the image. We will have to apply a class to the container and to the text we want to be on the left and right, as the example text above. Applying some css rules to each class will make the text move to either side of the image while it remains centered.
Our first rule will be for the paragraph itself which will center the image.
Code:
.test {
text-align:center;
}

Now let's get the text in the right locations.
Code:
.left {float:left;}
.right {float:right;}

Now we have text to the left and right of a centered image but the text is aligned with the top of the image. That's fine if that's what you want but we want the text to be vertically centered on the image. So we add another attribute to the paragraph class. Setting the line height the same as the image height will vertically center the text.
Code:
.test {
text-align:center;
line-height:25px;
}

But wait, there's a small gap at the bottom of the container in IE. To see the gap add a border around the paragraph.
Code:
.test {
text-align:center;
line-height:25px;
border:1px solid #000;
}

The easiest and quick way to get rid of this space is to remove browser defaults for padding and margins.
Code:
* {
padding:0;
margin:0;
}

Well that made things look better but the gap in IE is still there. This is where the trick come into play to get rid of the gap. We must set a height for the paragraph equal to the image height.
Code:
.test {
text-align:center;
line-height:25px;
height:25px;
}

Now you have left and right text vertically centered on a horizontally centered image.


If you desire more control on how your pages display, use css resets like the ones here. Remember to always use a doctype.

If you want two images with text in the center, put image links inside the spans and add some text. CSS doesn't have to change except for line height to match your images height.
Code:
<p class="test">
<span class="left"><img class="" src="image.gif" alt="" width="55" height="25" /></span>
<span class="right"><img class="" src="image.gif" alt="" width="55" height="25" /></span>
Some text here
</p>
Display posts from previous:   
Post new topic   This topic is locked: you cannot edit posts or make replies.    HTML Help Forum -> Member Tutorials All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
HTML Help Archive
Powered by phpBB © 2001, 2005 phpBB Group
HTML Help topic RSS feed 

 
DARFUR
HOSTING / DESIGN
MAKE MONEY

Home
  |   Tutorials   |   Forum   |   Quick List   |   Link Directory   |   About
Copyright ©1997-2002 Idocs and ©2002-2007 HTML Code Tutorial