views:

135

answers:

3

Hi All,

i have a very simple html. due to some limitations, i cannot modify the html content. I want to center the text vertically only using css.

<html>
<head>...</head>
<body>
<div>Ops, the webpage is currently not available</div>
</body>
</html>

Note that the size of the html can be variable.

In additional, if the text cannot be displayed in one line, it should be broken into multiple lines.

Is it possible?

+2  A: 

I think vertical-align only works for content which is in a table. For your simple page you could put the content in a table instead of a div to avoid this problem.

There are some workarounds, see http://phrogz.net/css/vertical-align/index.html

The brave new world of CSS. You have to use `position: absolute` or `line-height` to do something as complex as vertically align some content. (Not criticizing you @matschie, just not understanding what the committee that put together this standard was *thinking*)
Pekka
Well really IE's fault otherwise using `display: table` would work fine.
Jakub Hampl
"line-height" has a problem actually. if the text is too long, the rest of them is out of the visible area. Anyway, I have given up. I use a table instead.
stanleyxu2005
Pekka I totally agree with you, those solutions are not nice but I cant really think of anything else right now.
+1  A: 
<html>
<head>...
   <style type="text/css">
    div{
        width: 300px;
        height: 300px;
        border: solid blue;
        display: table-cell;
        vertical-align: middle;
    }
   </style>
</head>
<body>
<div>This works fine!</div>
</body>
</html>
Bakhtiyor
You can skip width, height, border attributes. They are used just to show visually the task.
Bakhtiyor
This won't work in IE 6 and 7.
Pekka
Though `table-cell` is not supported by IE <= 7.
Jakub Hampl
+2  A: 

Another possible solution:

<html>
<head>
    <title>Title</title>
    <style>
        body {height:100%}
    </style>
</head>
<body>
<div style="height:100%;">
    <div style="position:relative;top:50%;text-align:center">Ops, the webpage is currently not available</div>
</div>
</body>
</html>
ungarida