Fritz Thomas

18 Feb, 2009

How to vertically align text and block elements

Posted by: Fritz Thomas In: Overall

You may know the CSS declarations text-align and vertical-align. Perhaps you tried alredy to vertically align inline span elements within a div block element but did not succeed yet. I will try explain how to achieve this with a simple button example.
Before:screenshot14
After: screenshot13

Lets begin with the markup for the buttons:

1
2
3
4
5
6
7
8
<div class="buttons">
  <div class="add button">
    <span class="icon"><img src="add.png" alt="" /></span><span class="text">Add</span>
  </div>
  <div class="delete button">
    <span class="icon"><img src="delete.png" alt="" /></span><span class="text">Delete</span>
  </div>
</div>

Each Button is within a single div. Every button has its icon and a button text. Image and Text are in a span tag. the “add” and “delete” class is added to differentiate the buttons. This is useful if you want to add some behavior in JavaScript.

Next, the CSS styles:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
.buttons .button {
  line-height:16px; /** important **/
  height: 16px;
  padding: 1px;
  padding-right:5px;
  margin:0;
  margin-right:4px;
  background-color:#a1a1a1;
  color: #ffffff;
  border-radius: 2px; /** CSS3 **/
  -moz-border-radius: 2px; /** Mozilla specific **/
  -webkit-border-radius: 2px; /** Make a guess ;-) **/
  float: left;
  cursor: hand;
  cursor: pointer;
}
 
.buttons .button .icon {
  display: inline-block; /** important **/
  vertical-align:middle; /** important **/
  padding:0;
  margin: 0;
  margin-right:2px;
}
 
.buttons .button .text {
  display: inline-block; /** important **/
  vertical-align:middle; /** important **/
  padding:0;
  margin: 0;
}

The important css declarations and attributes are “display: inline-block”, “vertical-align: middle” and “line-height: XXpx”. The line-height tells every .button block the exact height of the inline elements underneath. The inline elements .icon and .text are handled and displayed as an “inline-block”. Firefox 3 and most of the other major browsers supporting it so it is safe to use it. Read more on this.

The result should look like this:screenshot13
I think the example is simple enough to adapt it to your needs and other use cases. Please keep in mind that in the example i use -moz-border-radius. This declaration is mozilla specific. So if you want rounded corners in IE you have to wait until IE is implementing this. Take a look when this is scheduled and follow my suggestion.

  • Digg
  • del.icio.us
  • DZone
  • MisterWong
  • Reddit
  • Slashdot
  • Technorati
  • Google Bookmarks
  • Facebook
  • Twitter
  • Yahoo! Bookmarks
  • Yigg
  • LinkedIn
  • MySpace
  • PDF
  • RSS

2 Responses to "How to vertically align text and block elements"

1 | Chris Thurrott

April 21st, 2009 at 9:21 pm

Avatar

Good tutorial. Have you been able to make this work with multi-line text on a button? That is, some buttons have long labels that wrap and others have short labels that don’t?

2 | thomasr

October 1st, 2009 at 2:06 pm

Avatar

Cools stuff! Have been wondering for quite a while on how to get that fixed! Thanks a lot!

Comment Form



Latest Tweets