Q:

How to align text vertically center in a DIV element using CSS

0

How to align text vertically center in a DIV element using CSS

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the CSS line-height property

If you will try to vertically center align text inside a div using the CSS rule vertical-align: middle; you won't succeed. Suppose you have a div element with the height of 50px and you have placed some link inside the div that you want to align vertically center. The simplest way to do it is — just apply the line-height property with value equal to the height of div which is 50px.

The concept behind this trick is, if the value of the line-height property is greater than the value of the font-size for an element, the difference (called "leading") is cut in half and distributed evenly on the top and bottom of the in-line box that align the inline elements vertically center to the element.

Let's try out the following example to understand how it actually works:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Vertically Center Align Text Using CSS</title>
<style>
    .menu{
        height: 20px;
        line-height: 20px;
        padding: 15px;        
        border: 1px solid #666;
    }
</style>
</head>
<body>
    <div class="menu">
        <a href="#">Home</a> | <a href="#">About Us</a> | <a href="#">Contact</a>
    </div>
</body>
</html>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

HTML / CSS Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to remove dotted line around the links or link... >>
<< How to write comments in CSS...