Solved

center text in div

Posted on 2011-03-02
17
929 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

746 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