Forum Moderators: not2easy
The alignment of text and inline-level content is defined in [CSS3TEXT] and [[CSS3LINEBOX]].
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<style type="text/css">
div {
display:table-cell;
width:400px;
height:400px;
border:1px solid #000;
margin:auto;
vertical-align:middle;
text-align:center;
}
p {
display:inline-block;
width:200px;
height:200px;
line-height:200px;/* one line of text only*/
background:red;
margin:auto;
}
</style>
</head>
<body>
<div>
<p>vertically centred</p>
</div>
</body>
</html>
Why doesn't CSS have a simple way to vertically align within a div ...
(vertical-align isn't what I'm talking about)
... I can't figure out why such a basic layout function would be left out.
[edited by: incrediBILL at 7:42 am (utc) on Aug 7, 2012]
[edit reason] removed URL, no sig links [/edit]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<style type="text/css">
div {
display:table-cell;
width:100%;
height:400px;
border:1px solid #000;
margin:auto;
vertical-align:middle;
text-align:center;
}
p {
display:inline-block;
width:50%;
background:red;
margin:auto;
}
</style>
</head>
<body>
<div>
<p>Lorem ipsum text Lorem ipsum text Lorem ipsum text Lorem ipsum text Lorem ipsum text Lorem ipsum text Lorem ipsum text Lorem ipsum text Lorem ipsum text Lorem ipsum text Lorem ipsum text Lorem ipsum text Lorem ipsum text Lorem ipsum text </p>
</div>
</body>
</html>
However, except in table cells, vertical alignment was not possible. As CSS3 adds further capabilities, the ability to align boxes in various dimensions becomes more critical. This module attempts to create a cohesive and common box alignment model to share among all of CSS.
Why doesn't CSS have a simple way to vertically align within a div similar to the way valign works in a table?
He's asking, philosophically, "Why...?"
"bottom" = align the bottom of your text et cetera (this is basically the default)
Conversely, "display: table-cell" flatly refuses to do what I want when it comes to left or right alignment :
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<style type="text/css">
div {
display:table-cell;
width:100%;
height:400px;
border:1px solid #000;
margin:auto;
vertical-align:middle;
text-align:right;
}
p {
display:inline-block;
width:50%;
background:red;
margin:auto;
}
</style>
</head>
<body>
<div>
<p>Lorem ipsum text Lorem ipsum text Lorem ipsum text Lorem ipsum text Lorem ipsum text Lorem ipsum text Lorem ipsum text Lorem ipsum text Lorem ipsum text Lorem ipsum text Lorem ipsum text Lorem ipsum text Lorem ipsum text Lorem ipsum text </p>
</div>
</body>
</html>