Go Back   CodingForums.com > :: Client side development > HTML & CSS

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-25-2012, 12:11 PM   PM User | #1
eetec
New to the CF scene

 
Join Date: Dec 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
eetec is an unknown quantity at this point
Vertical Center text in a div

I am trying vertical center my text in a div but it always float at top in div. I also tried using a span inside the div and apply css on span but it too not works. I used this css property (vertical-align:middle)

This is Html

Code:
<div id="topsmallmenucontainer">
<div class="topmenuleft"></div>
<div class="topmenumiddle"><span> Thhis is text inside div</span></div>
<div class="topmenuright"></div>
<div class="clearBox"></div>
</div>

This is CSS

Code:
#topsmallmenucontainer
{
	width:406px;
	height:41px;
	position:absolute;
	top:53px;
	right:30px;
}
.topmenuleft 
{
	width:7px;
	height:41px;
	float:left;
	position:relative;
	background-image: url(../images/top_small_menu_left.png);
	background-repeat: no-repeat;
	background-position: 0 0;
}
.topmenumiddle 
{
	width:393px;
	height:41px;
	position:relative;
	float:left;
	background-image: url(../images/top_small_menu_middle.png);
	background-repeat: repeat-x;
	background-position: 0 0;
	vertical-align:middle;
	color:#FFF;
}

.topmenuright 
{
	width:6px;
	height:41px;
	position:relative;
	float:left;
	background-image: url(../images/top_small_menu_right.png);
	background-repeat: no-repeat;
	background-position: 0 0;
}
.clearBox
{
	clear:both;	
}
This is output div snap
eetec is offline   Reply With Quote
Old 12-25-2012, 05:08 PM   PM User | #2
Excavator
Master Coder


 
Excavator's Avatar
 
Join Date: Dec 2006
Location: Alaska
Posts: 9,410
Thanks: 22
Thanked 1,765 Times in 1,749 Posts
Excavator has a spectacular aura aboutExcavator has a spectacular aura aboutExcavator has a spectacular aura about
Hello eetec,
A single line of text is easy to vertically centered with line-height, like this -
Code:
.topmenumiddle {
	width: 393px;
	line-height: 41px;
	float: left;
	position: relative;
	/*vertical-align: middle;*/
	color: #fff;
	background: url(../images/top_small_menu_middle.png) repeat-x 0 0;
}
vertical-align
line-height

It gets more complicated if you want to center a paragraph or div element. See some vertical center examples here.
My favorite is the last one which uses a float and a clear.
__________________
Validate often DURING development - Use it like a splelchecker | Debug during Development |Write it for FireFox, ignore IE
Use the right DocType | Validate your markup | Validate your CSS | Why validating is good | Why tables are bad
Excavator is offline   Reply With Quote
Users who have thanked Excavator for this post:
eetec (12-26-2012)
Old 12-26-2012, 05:25 AM   PM User | #3
eetec
New to the CF scene

 
Join Date: Dec 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
eetec is an unknown quantity at this point
Excavator,

This answer is very helpful for me.
Thank You.
eetec is offline   Reply With Quote
Reply

Bookmarks

Tags
vertical align text, vertical center of a text

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:26 AM.


Advertisement
Log in to turn off these ads.