För att centrera <p> både horisontellt och vertikalt i exemplet nedan;
- Sätt
position till
relative.
- Sätt
top till
50%.
- Sätt
font-size till ett givet värde och förslagsvis även
line-height till ett givet värde.
- Sätt t
ext-align till
center.
- Sätt
margin-top till halva elementets höjd, dvs
halva line-height (line-height 1 betyder 1*fontstorlek).
HTML-kod:
<html>
<head>
<style type="text/css">
html, body {
height: 100%;
}
body {
margin: 0;
padding: 0;
}
p {
position: relative;
top: 50%;
font: 14px/1;
text-align: center;
height: 14px;
margin-top: -7px;
}
</style>
</head>
<body>
<p>Placeholder</p>
</body>
</html>