Solved

center text in div

Posted on 2011-03-02
17
935 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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!

 

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
 

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

In this post we will learn different types of Android Layout and some basics of an Android App.
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

696 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