I'm starting up a new project, and I've always had an issue getting vertical alignment right in CSS. Is there anything I could do better that what I've currently come up with? Replacing the header and nav with divs, and changing the rgba values to hex values this seems to work all the way back to IE7 which I'm happy with.


html, * {

body {

nav {
    background-color:rgba(0, 0, 0, 0.1);

nav a.tab {
    padding:0 15px;

    vertical-align:middle;  text-decoration:none;

nav a.tab > span {

nav a.tab:hover {
    background-color:rgba(0, 0, 0, 0.1);

header {

header, header h1, header nav, header nav a.tab {

header nav {

header h1 {
    margin:0 20px;
    text-shadow:2px 2px 2px #333333;


        <a href="/" class="tab active">
        <a href="/projects" class="tab">
        <a href="/" class="tab">



3 Answers 3


Your code is good - both the CSS and the HTML validate at the validators:

HTML validator
CSS validator

However, you should put your wrap your HTML code like this:

<!DOCTYPE html>
        <meta charset="UTF-8">
        <title>Page title here...</title>
        <!-- other links here, such as external CSS/JS files -->
        <!-- your code here -->

I really like how you use the HTML5 header element instead of using the old style <div class="header">

When people write CSS, they often format it slightly different, like this:

nav a.tab > span {
    display:        table-cell;
    vertical-align: middle;

It is just easier to read this way. Also, you should have each element have its own line, not put many on the same line like this:

vertical-align:middle;  text-decoration:none;

Otherwise, this looks good to me.

  • \$\begingroup\$ It actually is inside all the html tags etc, I just posted the relevant part to the question :), and also those rule we're suppose to be on separate lines, I just messed up the copy paste haha :P. I haven't actually seen CSS wrote with the spaces like that, but it is a whole lot easier to read, so I'm gonna change it all over now. Thanks for your tips :) \$\endgroup\$
    – TMH
    Feb 10, 2015 at 20:28
  • \$\begingroup\$ Glad to help. Yes, I thought this might just be the relevant part, but I have seen so much bad HTML that displays different in every browser out there that I always put this in my answers. \$\endgroup\$
    – user34073
    Feb 10, 2015 at 21:22

I only have some nitpicks about formatting and readability. Hopefully somebody else will comment specifically about good approaches for treating vertical alignment.

It would be more readable (and the common convention) to put a space after colons. So instead of:

background-color:rgba(0, 0, 0, 0.1);

Write as:

background-color: rgba(0, 0, 0, 0.1);
text-decoration: underline;

And don't put two definitions on the same line like this:

vertical-align:middle;  text-decoration:none;

It's better when code reads from top to bottom, and your eyes can stay focusing on the left, rather than dipping deeper into the right part, like this:

vertical-align: middle;
text-decoration: none;

There's one more way to center vertically

element { 
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  position: absolute;
  • \$\begingroup\$ If you're hoping to support IE8 or older, this method will be outright broken because the transform property is only supported in IE9+ (note that the OP specifically mentions that IE7 support is a "good thing"). \$\endgroup\$
    – cimmanon
    Feb 13, 2015 at 13:26
  • \$\begingroup\$ yes, that's right \$\endgroup\$ Feb 13, 2015 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.