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.
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.
它们相邻,但是我想要右边的nav。
7 个解决方案
#1
105
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
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 by justify-content: space-between; like this:
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:after part and justified the content.
In order to solve the issue of the extra space that is inserted with the after pseudo-element one can do a trick of setting the font-size to 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/
If you don't want to use floats, you're going to have to wrap your nav:
如果你不想使用浮动,你需要将你的nav包裹起来:
Title
...and add some more specific css:
…并添加一些更具体的css:
header {
//text-align: center; // will set in js when the nav overflows (i think)
width:960px;/*Change as needed*/
height:75px;/*Change as needed*/
}
h1 {
display: inline-block;
margin-top: 0.321em;
}
#navWrap{
position:absolute;
top:50px; /*Change as needed*/
right:0;
}
nav {
display: inline-block;
vertical-align: baseline;
}
You may need to do a little more, but that's a start.
你可能需要做更多,但这只是一个开始。
#3
3
Taking advantage of @skip405's answer, I've made a Sass mixin for it:
It accepts 3 parameters. The container, the left and the right element. For example, to fit the question, you could use it like this:
它接受三个参数。容器,左边和右边的元素。例如,为了符合问题,你可以这样使用:
@include inline-block-lr('header', 'h1', 'nav');
#4
1
If you're already using Javascript to center stuff when the screen is too small (as per your comment for your header), why not just undo floats/margins with Javascript while you're at it and use floats and margins normally.