473,765 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Vertical alignment of text within a DIV

Sorry if this is a dumb question buy my CSS is pretty bad... but how do
I get text to center vertically within a div tag? Here's my code
below... the text is displaying on the far top-right. I would like it
far right centered vertically.

#test
{
width: 100%;
height: 55px;
margin: 0 0 0 0;
padding: 0 0 0 0;
border: solid #3366CC;
border-width: 1px 0 0 0;
text-align: right;
font-size: 14px;
font-weight: bold;
color: #ffffff;
}

<div id="test">woul d like this text centered vertically</div>

Aug 29 '06 #1
18 7945
Els
~john wrote:
Sorry if this is a dumb question buy my CSS is pretty bad... but how do
I get text to center vertically within a div tag?
You don't really, at least not in the same way as in a table cell.
Here's my code
below... the text is displaying on the far top-right. I would like it
far right centered vertically.

#test
{
width: 100%;
height: 55px;
margin: 0 0 0 0;
padding: 0 0 0 0;
border: solid #3366CC;
border-width: 1px 0 0 0;
text-align: right;
font-size: 14px;
font-weight: bold;
color: #ffffff;
}

<div id="test">woul d like this text centered vertically</div>
Add a <paround the text inside that div, and give it a top margin.
It will always stay at that level though, regardless of the text
wrapping or font resizing. Yes, the text will be resizable, in any
other browser than IE - as it should. To give IE users the same
benefits, please don't set a font-size in px, but use % or em.

--
Els http://locusmeus.com/
accessible web design: http://locusoptimus.com/

Now playing: Camel - Lady Fantasy (Bonus track)
Aug 29 '06 #2

thanks
Els wrote:
~john wrote:
Sorry if this is a dumb question buy my CSS is pretty bad... but how do
I get text to center vertically within a div tag?

You don't really, at least not in the same way as in a table cell.
Here's my code
below... the text is displaying on the far top-right. I would like it
far right centered vertically.

#test
{
width: 100%;
height: 55px;
margin: 0 0 0 0;
padding: 0 0 0 0;
border: solid #3366CC;
border-width: 1px 0 0 0;
text-align: right;
font-size: 14px;
font-weight: bold;
color: #ffffff;
}

<div id="test">woul d like this text centered vertically</div>

Add a <paround the text inside that div, and give it a top margin.
It will always stay at that level though, regardless of the text
wrapping or font resizing. Yes, the text will be resizable, in any
other browser than IE - as it should. To give IE users the same
benefits, please don't set a font-size in px, but use % or em.

--
Els http://locusmeus.com/
accessible web design: http://locusoptimus.com/

Now playing: Camel - Lady Fantasy (Bonus track)
Aug 29 '06 #3
Els wrote:
~john wrote:
>Sorry if this is a dumb question buy my CSS is pretty bad... but how
do I get text to center vertically within a div tag?

You don't really, at least not in the same way as in a table cell.
>Here's my code
below... the text is displaying on the far top-right. I would like it
far right centered vertically.

#test
{
width: 100%;
height: 55px;
margin: 0 0 0 0;
padding: 0 0 0 0;
border: solid #3366CC;
border-width: 1px 0 0 0;
text-align: right;
font-size: 14px;
font-weight: bold;
color: #ffffff;
}

<div id="test">woul d like this text centered vertically</div>

Add a <paround the text inside that div, and give it a top margin.
It will always stay at that level though, regardless of the text
wrapping or font resizing. Yes, the text will be resizable, in any
other browser than IE - as it should. To give IE users the same
benefits, please don't set a font-size in px, but use % or em.
Actually this is a major short coming to CSS, as I jsut keep seeing more
and more cases of it jsut makes life difficult and painful for what
shoudl be basic things. Just like the "equal column height" and proper
multi column layout dilemas.

Seriously, I cannot fathum how the inventors of CSS (at least the
position part; the formating parts of CSS are not a prolbem at all and
work great) could not of made it straight foward. Would it really have
been so horrible to having something like align-vertical,
align-horizontal, and proper height control (notice how 100% heights on
DIVs inside DIVs sometimes push out of the confines of the outer div,
when the spec CLEARLY states the height % should of the CONTAINING DIV's
space. This strangeness happens is virtually any CSS supporting browser
I've tested (Moz, FF, IE, Opera...)

I'm sorry, but it really seems CSS has not yet come of age. don't get me
wrong, CSS is a triuth in many areas, but in the area of layouts,
anything advanced (or many things easily doable with, gasp, tables) it
just seems so lacking. At the very least, it makes many things easy
(having styles that can be easily reused) but at the same time it makes
many thing a heck of a lot harder than they should be, or even near
impossible without some wild or strange hackery. (Keep in mind I don't
mean cross browsers hacks.)
Aug 30 '06 #4
~john wrote:
Show do
I get text to center vertically within a div tag?
Here is a page where someone has gone to the trouble to explain methods:

<http://www.student.oul u.fi/~laurirai/www/css/middle/>

--
Gus
Aug 30 '06 #5
"Wayne Poe" <lo***@h4h.comw rote:
>Actually this is a major short coming to CSS, as I jsut keep seeing more
and more cases of it jsut makes life difficult and painful for what
shoudl be basic things. Just like the "equal column height" and proper
multi column layout dilemas.
Equal "column" height is possible with CSS2 methods, but IE doesn't
support that part of CSS2.

Multi column layouts (text ends at the bottom of one column and
continues at the top of the adjacent right column) make no sense at all
on the web, the fact that this cannot be done with CSS2 is a good thing.
(Regrettably it looks like CSS3 may provide authors with a method to
create such monstrosities.)
>Seriously, I cannot fathum how the inventors of CSS (at least the
position part; the formating parts of CSS are not a prolbem at all and
work great) could not of made it straight foward. Would it really have
been so horrible to having something like align-vertical,
align-horizontal,
Although not implemented in a straight forward manner, again this is
possible using CSS2 methods, but again not supported by IE.

The fact that IE doesn't support these bits of CSS2 could be considered
as a blessing in disguise because the way that it is provided for in
CSS2 has all the drawbacks (minus one) of HTML tables: reflows, or
nothing being rendered until all content has been downloaded.
>and proper height control (notice how 100% heights on
DIVs inside DIVs sometimes push out of the confines of the outer div,
when the spec CLEARLY states the height % should of the CONTAINING DIV's
space.
You misunderstood the phrase "containing block"
http://www.w3.org/TR/CSS21/visuren.h...ntaining-block
>This strangeness happens is virtually any CSS supporting browser
I've tested (Moz, FF, IE, Opera...)
Moz, FF and Opera more than likely render such according to spec, your
understanding is flawed.
>I'm sorry, but it really seems CSS has not yet come of age. don't get me
wrong, CSS is a triuth in many areas, but in the area of layouts,
anything advanced (or many things easily doable with, gasp, tables) it
just seems so lacking. At the very least, it makes many things easy
(having styles that can be easily reused) but at the same time it makes
many thing a heck of a lot harder than they should be, or even near
impossible without some wild or strange hackery. (Keep in mind I don't
mean cross browsers hacks.)
Sadly CSS2 does not offer a good method for creating layouts suitable
for the web, this is indeed a shortcoming. But layouts that work on the
web are a complex problem if the serious drawbacks of table layouts are
to be avoided

--
Spartanicus
Aug 30 '06 #6
Spartanicus schrieb:
"Wayne Poe" <lo***@h4h.comw rote:
>Seriously, I cannot fathum how the inventors of CSS (at least the
position part; the formating parts of CSS are not a prolbem at all and
work great) could not of made it straight foward. Would it really have
been so horrible to having something like align-vertical,
align-horizontal,

Although not implemented in a straight forward manner, again this is
possible using CSS2 methods, but again not supported by IE.
Would you care to elaborate on that (or maybe just post a link?)

Thanks a lot

Aug 30 '06 #7
Christian Kirsch <ck@bru6.dewrot e:
>>Seriously, I cannot fathum how the inventors of CSS (at least the
position part; the formating parts of CSS are not a prolbem at all and
work great) could not of made it straight foward. Would it really have
been so horrible to having something like align-vertical,
align-horizontal,

Although not implemented in a straight forward manner, again this is
possible using CSS2 methods, but again not supported by IE.

Would you care to elaborate on that (or maybe just post a link?)
http://www.w3.org/TR/CSS21/visuren.html#display-prop
http://homepage.ntlworld.ie/spartanicus/css-table.htm should work in any
modern browser but IE

--
Spartanicus
Aug 30 '06 #8
Spartanicus wrote:
"Wayne Poe" <lo***@h4h.comw rote:
>Actually this is a major short coming to CSS, as I jsut keep seeing
more and more cases of it jsut makes life difficult and painful for
what shoudl be basic things. Just like the "equal column height" and
proper multi column layout dilemas.

Equal "column" height is possible with CSS2 methods, but IE doesn't
support that part of CSS2.
Well, yes and no. Correct me if I'm wrong, but when IE6 first came out,
wasn't CSS2 not yet finalized?
Multi column layouts (text ends at the bottom of one column and
continues at the top of the adjacent right column) make no sense at
all on the web,
I wasn't refering to that, but actually sidbar type setups, which are
incredibly easy with html <tables>. But saying it is no suitable, is
more of your opinion than stone carved fact I fret. NOthing wrong with
that, but it would be nice if you said so instead of pushing as what
everyone should be doing.

Actually have a two column news-paper style layout is rather well suited
for, well, news sites, as many seem to do just.
>Seriously, I cannot fathum how the inventors of CSS (at least the
position part; the formating parts of CSS are not a prolbem at all
and work great) could not of made it straight foward. Would it
really have been so horrible to having something like align-vertical,
align-horizontal,

Although not implemented in a straight forward manner, again this is
possible using CSS2 methods, but again not supported by IE.

The fact that IE doesn't support these bits of CSS2 could be
considered as a blessing in disguise because the way that it is
provided for in CSS2 has all the drawbacks (minus one) of HTML
tables: reflows, or nothing being rendered until all content has been
downloaded.
How about this one, going the other way:

Take this code:
############### ############### ############### ############### #############
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style type="text/css">
body { margin: 4px; }
#outter {
background: #66AAFF;
color: #007700;
height: 300px;
border: #000000 2px solid;
padding: 5px;
}
#outter .inner {
background: #BBBBBB;
height: 100%;
width: 90%;
border: #DD0000 2px solid;
padding: 50px 0px 50px 0px;
}
</style>
<title>um...</title>
</head>
<body>
<div id="outter" align="center">
<div class="inner">1 11</div>
</div>
</body>
</html>
############### ############### ############### ############### #############

Notice how the bottom of the outer div gets pushed down properly in IE6,
but in Moz 1.7 and FF 1.5.0.6, NS 7, and Opera 8, the inner div's bottom
simply gets pushed out of the bounds of the outter div. If no pading and
borders are used on inner, then it's uniform in all those browsers.

Granted, according to O'Reilly CSS pocket ref, height "defines the
height of an element's content area, outside of which padding, borders,
and margins are added." But it doesn't say ANYTHING about pushing
OUTSIDE it's container (which seems rather undesirable for a
flow/non-positionsed layout. In fact, it only makes sense for absolute
positioning, where the container no longer matters in that respect.)

So you tell me, is this a design flaw of CSS (and the fact it doesn't
seem to give you very much control over weather content should "push"
it's container's height so that everything "fits" ? Other than overflow,
which DOES NOT solve this problem, but instead only allows to either
hide it and make the container scrollable.)
>and proper height control (notice how 100% heights on
DIVs inside DIVs sometimes push out of the confines of the outer div,
when the spec CLEARLY states the height % should of the CONTAINING
DIV's space.

You misunderstood the phrase "containing block"
http://www.w3.org/TR/CSS21/visuren.h...ntaining-block
>This strangeness happens is virtually any CSS supporting browser
I've tested (Moz, FF, IE, Opera...)

Moz, FF and Opera more than likely render such according to spec, your
understanding is flawed.
Actually, with strict on, I've witnessed many anomalies on the Moz/FF/NS
side of things, just as much as I've seen on the IE side. Browser bias
is not something you want to get into here.
>I'm sorry, but it really seems CSS has not yet come of age. don't
get me wrong, CSS is a triuth in many areas, but in the area of
layouts, anything advanced (or many things easily doable with, gasp,
tables) it just seems so lacking. At the very least, it makes many
things easy (having styles that can be easily reused) but at the
same time it makes many thing a heck of a lot harder than they
should be, or even near impossible without some wild or strange
hackery. (Keep in mind I don't mean cross browsers hacks.)

Sadly CSS2 does not offer a good method for creating layouts suitable
for the web, this is indeed a shortcoming. But layouts that work on
the web are a complex problem if the serious drawbacks of table
layouts are to be avoided
Yes, but if CSS actually made it even half as easy to do many layouts
you could easily hack out with <tablebased layout, as frowned upon as
they may be, you wouldn't have half as many posts concerning how to do
such layouts in CSS only.

I just wish the CSS advocates here would simply admit CSS has not yet
arrived to the point where it completely replaces "old fashioned"
layouts, if you will. I too wish CSS would come of age, and I hope CSS3
resolves many the glaring issues. But I fear it will take some time
before it will be safe to broadly embrace the new spec when it comes
out, for fear of leaving previous gen browsers in the cold, just like
when CSS2 first arived. You can not expect the shift to CSS3 to change
over night.

This is why I find CSS2, which it's infinate potential to really be a
failure in that it should of come out a lot stronger and compatibility
among browsers should of been ensured.
Aug 30 '06 #9
On 2006-08-30, Wayne Poe wrote:
Spartanicus wrote:
>"Wayne Poe" <lo***@h4h.comw rote:
>Multi column layouts (text ends at the bottom of one column and
continues at the top of the adjacent right column) make no sense at
all on the web,

I wasn't refering to that, but actually sidbar type setups, which are
incredibly easy with html <tables>. But saying it is no suitable, is
more of your opinion than stone carved fact I fret. NOthing wrong with
that, but it would be nice if you said so instead of pushing as what
everyone should be doing.

Actually have a two column news-paper style layout is rather well suited
for, well, news sites, as many seem to do just.
Spartanicus is right. A 2-column layout which flows from the bottom
of the first column to the top of the second column only makes
sense when you have a fixed column height that is entirely visible.
You can never be sure of having that in any browser window.

--
Chris F.A. Johnson <http://cfaj.freeshell. org>
=============== =============== =============== =============== =======
Author:
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Aug 30 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
66046
by: Markus Ernst | last post by:
Hi I have a strange problem with vertical-align. The case can be viewed at http://www.brainput.info/geschichte.html. HTML code: <div id="bild"><img src="geschichte.gif" width="274" height="29" alt="Wir schreiben die Geschichte Ihres Unternehmens."></div>
22
12483
by: Mel | last post by:
i have a div with with 300 and height 100 i need to place a text centered withing this box, can someone please help ? thankx Mel
1
7901
by: Kenneth | last post by:
Okay, I've been scouring Google for hours looking for a solution to this problem, but as of yet I can't find one. XHTML Transitional seems to specify that I can no longer set a table's height to 100% and vertically center content inside of it. Therefore, I'm seeking a way, preferably in CSS to vertically center a site. Here's more of a visual to what I have: /********************************
2
19404
by: Kay | last post by:
Hello, I have a asp lable control, which I use to display text in, I would like to have the text display aligned in the center and vertical alignment left however when I add the following style to the lable it does not work <asp:label id="lblBanner" style="text-aligh:left;vertical-align:middle;" runat="server" Width="100%" ></asp:label> could someone please tell me how to display the text valign middle and text
11
19229
by: C.W.Holeman II | last post by:
I what to hide an input element and the following text. I have the selector for the input working and just need to grab the text following it. CSS: form{ display:table; text-align:center; }
2
15565
by: esteuart | last post by:
I need to get the right combination for a div to clip any text inside and allow vertical alignment. I only have this problem in FireFox. I have 3 divs nested within each other. The outer div has display of table, the first inner div has display of table-cell and the inner-most div has the text. The outer div has a set width and height. So here's the problem: Without the inner divs having a width and height set, the overflow:hidden doesn't...
8
3553
by: ayamopamo | last post by:
Hi- I am trying to center a web page. It seems like this should be very simple to do, but apparently it isn't my day. I have successfully centered the background by calling it in the css body tag: body { font-family: Trebuchet MS, Arial; cursor : default; background:#000000 url("Images/long-template.jpg"); background-repeat:no-repeat; background-position: center; background-attachment:scroll; height: 1228px;
13
1662
by: Bill | last post by:
Hi How can I have IE7 act correctly on that ? The rules make the link text go down when hovered. It works in FF , I had to add a hard space right after the LI tag to have OP9 work but I can't figure how to have IE7 work. Why? How can I have it work in all 3 browsers? http://nrap.selfip.com
4
2691
by: lxa1801 | last post by:
Okay, so here is my issue. I have done a W3C validation and the errors I get are mostly alt or shorttag, doesn't appear to be anything connected to this problem. You can view the site at www.michiganequality.org/index.htm Basically, the site views properly in IE, however, in Firefox the rightsidemain div does not align back at the top of the containerbody div, but rather aligns where the leftside div left off. Now, the site was working...
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10008
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.