How can I align two inline-blocks so that one is left and the other is right on the same line? Why is this so hard? Is there something like LaTeX’s \hfill that can consume the space between them to achieve this?
I don’t want to use floats because with inline-blocks I can line up the baselines. And when the window is too small for both of them, with inline-blocks I can just change the text-align to center and they will be centered one atop another. Relative(parent) + Absolute(element) positioning has the same problems as floats do.
The HTML5:
<header>
<h1>Title</h1>
<nav>
<a>A Link</a>
<a>Another Link</a>
<a>A Third Link</a>
</nav>
</header>
The css:
header {
//text-align: center; // will set in js when the nav overflows (i think)
}
h1 {
display: inline-block;
margin-top: 0.321em;
}
nav {
display: inline-block;
vertical-align: baseline;
}
Thery’re right next to each other, but I want the nav on the right.

Edit: 3 years has passed since I answered this question and I guess a more modern solution is needed, although the current one does the thing 🙂
1.Flexbox
It’s by far the shortest and most flexible. Apply
display: flex;to the parent container and adjust the placement of its children byjustify-content: space-between;like this:Can be seen online here – http://jsfiddle.net/skip405/NfeVh/1073/
Note however that flexbox support is IE10 and newer. If you need to support IE 9 or older, use the following solution:
2.You can use the
text-align: justifytechnique here.The working example can be seen here: http://jsfiddle.net/skip405/NfeVh/4/. This code works from IE7 and above
If inline-block elements in HTML are not separated with space, this solution won’t work – see example http://jsfiddle.net/NfeVh/1408/ . This might be a case when you insert content with Javascript.
If we don’t care about IE7 simply omit the star-hack properties. The working example using your markup is here – http://jsfiddle.net/skip405/NfeVh/5/. I just added the
header:afterpart and justified the content.In order to solve the issue of the extra space that is inserted with the
afterpseudo-element one can do a trick of setting thefont-sizeto 0 for the parent element and resetting it back to say 14px for the child elements. The working example of this trick can be seen here: http://jsfiddle.net/skip405/NfeVh/326/