Solved

center text in div

Posted on 2011-03-02
17
930 Views
Last Modified: 2012-05-11
I have a div purposed set to higher height of 24 px. I want the text inside to be centered.

.center-text {
   vertical-align:middle;
}

did not work. how to do this correctly?
0
Comment
Question by:bhomass
17 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35023508
Can't you use it simply like that:

<div align="center">
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 125 total points
ID: 35023517
If you are talking about vertical aligm=nment, check this:

http://phrogz.net/css/vertical-align/index.html
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35023524
go to the bottom part of the link I posated above for practical recommendation
on how to allign vertically
0
 

Author Comment

by:bhomass
ID: 35023677
for yan:

works in Firefox, but not IE.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35023715
The example they have on their page seems to be working in my IE 6.0

Maybe you could post your example.
0
 
LVL 47

Accepted Solution

by:
for_yan earned 125 total points
ID: 35023742
this one shows cehterd in my IE
vertically-centered.html
vert-centered.JPG
0
 

Author Comment

by:bhomass
ID: 35023918
one problem, if I add float:left to the div, then center text fails

  <style type="text/css">
  .vtext {
      height:80px;
      vertical-align:middle;
      display:table-cell;
      border-width:1px;
      border-style:solid;
      border-color:1;
      float:left;
}
  </style>

any way to leave float in there and still center text?
0
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 35023933
So this depends on what other elements are wrapped around you <div> if you <div> exists by itself then yes this is trivial. Also does your DIV have any CSS classes associated with it? There's a lot that can influence this. Such as the style="float: right/left/center;" and other things. Post your code without it we are just shooting in the wind.

Ah look, I found it: the code I have below worked better for me that is if I understood you properly. I took out some stuff that was just noise in the form of CSS with no purpose.

<body>
<style type="text/css">
#myoutercontainer 
  { 
     text-align: center;
     align: center;
   }
#myinnercontainer 
{ 
  height:10em; 
  margin-top: -5em;
  text-align: center;
  align: center;
}
</style>
...
<div id="myoutercontainer" width="100%">
	<div id="myinnercontainer" width="100%">
		<p>Hey look! I'm vertically centered!</p>
		<p>How sweet is this?!</p>
	</div>
</div>
</body>

Open in new window

0
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.

 

Author Comment

by:bhomass
ID: 35024107
align is not a legal css attribute, AFAIK.
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35024662
Is the div coming under table ?
0
 

Author Comment

by:bhomass
ID: 35024709
no
0
 
LVL 2

Expert Comment

by:seanyc111
ID: 35026218
try using:
<center>
whatever you want here
</center>
0
 

Author Comment

by:bhomass
ID: 35029598
<center> is for horizontal centering, not vertical.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35029621
But you already have example which is working,
What is the issue?
0
 

Author Comment

by:bhomass
ID: 35050014
I finally tried out example by rawinnlnx9: and it does not work. only "how sweet is this?!" shows up and has top chopped off.

code for for_yan: works.
0
 

Author Closing Comment

by:bhomass
ID: 35050022
working for FF and IE
0
 

Author Comment

by:bhomass
ID: 35066904
ignore last comment. it was an error.

the one above it which indicates the problem is the actual last intended post.
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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

932 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

9 Experts available now in Live!

Get 1:1 Help Now