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
  • Fark
  • Google Bookmarks
  • Facebook
  • MisterWong.DE
  • Twitter
  • Twitthis
  • Yahoo! Bookmarks
  • Yigg

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


  • subbu: Hey, Thanks a lot. I was searching for something similar. Subbu
  • Maik: Habe jetzt für ein eigenes (schon ewig geplantes) Projekt auch mit dem ZF(version 1.10) angefangen und das tutorial hier hat trotzdem noch ganz gut g
  • Andreas: Auf jeden Fall eine gelungene Einführung, danke. Vielleicht gibt es ja 2010 den zweiten Teil ;)
  • Hartmut: Genau das Leserecht war bei mir das Problem. Danke.
  • admin: Hi! For more information go to http://extjs.com/ . There is a preview video of this Tool called Ext.De
  • jagocoding: nice info bro... where can i download this tools ?? can u give me ulr, this is free or not ?
  • admin: Of course! This should be rm -rf /tmp/*
  • Christian: rm -rf /tmp* bad idea. at least do rm -rf /tmp/* but if things go worse it will skrew your X Session
  • Aleksandr: Hi! Thank you, it really works, you could even do it in one line like here: http://www.devhands.com/2010/01/how-to-get-the-full-path-from-the-running
  • matt: thanks for the tip. freeing up the distfiles freed me up 5GB of diskspace. My root is only 20GB which I thought should be plenty but my disk hit 100