tags:

views:

50

answers:

2

Hello,

I want to be able to keep a text on the left, but in the middle of a div.

<div id=sel>text goes here</div>

and my CSS for the same

sel{
    text-align:left;
    vertical-align:middle;
}

The characters and lines of the text may vary. I am more focussed on the text with a single line that sits on the top. I do not want to use position:absolute/relative.

Appreciate all help.

Thanks Jean

+3  A: 

Firstly, a side note: vertical-align only works with table cells. As a result, this problem is trivial with table cells. See Understanding vertical-align, or "How (Not) To Vertically Center Content".

Otherwise you will need to put that content inside another block and then vertically center that block within the outer block. See Vertical Centering in CSS.

cletus
Tried: top: -50%, display: table-cell; vertical-align: middle; separately, but not working
Jean
@Jean the linked trick works. You must be missing something. Do you have the *three* divs? Do you have a `DOCTYPE`?
cletus
This is how my code is<div><span>text goes here to middle and left</span></div>
Jean
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Jean
@Jean read the example from the second link carefully. There are (and need to be) **three** divs. Secondly, without starting a second topic I'll summarize: don't use XHTML. There's no need. It adds no value. It just creates problems.
cletus
cletus now you have confused me totally
Jean
@Jean that `DOCTYPE` is XHTML. I can only assume your markup is XHTML. If so, go back to HTML 4.01 `DOCTYPE` and markup. XHTML just causes problems. If not that, what are you confused by?
cletus
To start off, we are moving our office....next...I am about 20ms off from my desk...next...why do I require 3 divs?sorry I am all fuzzed up in my head..
Jean
+1  A: 

If you are dealing with a single line only, the easiest way is probably to set the line-height equal to the height of the div, for example:

#sel {
    text-align: left;
    height: 4em;
    line-height: 4em;
}

For other scenarios, have a look at this page

Tom Bartel
That would get all text out of place, if the div contains more than one line
Jean
That's why I wrote "if you are dealing with a single line only". I must have misunderstood your sentence "I am more focussed on the text with a single line that sits on the top." I do not know what exactly you mean by this.
Tom Bartel