?
Solved

center text in div

Posted on 2011-03-02
17
Medium Priority
?
940 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 500 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 500 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
 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Find out what you should include to make the best professional email signature for your organization.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

807 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