Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

center text in div

Posted on 2011-03-02
17
Medium Priority
?
939 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 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
Technology Partners: 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

609 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