如何将两个项目向右对齐,而不进行手动高度/边距计算?
这个小提琴演示了这个问题。
我正在尝试将图像和按钮在两条单独的行上向右对齐,作为联合单位。我创建了一个带有position:relative的包装div(“right-stuff”),所以我可以在它的子按钮上使用position:absolute。
问题是我不知道有什么好方法可以将按钮向右对齐而不影响高度计算。
我所做的是给它 position:absolute
和 right:0
,但这会将其从流程中删除,并导致容器(“right-stuff”)不包含它 - 看看红色背景如何没有到达它,尽管它“应该”。
另一个问题是,“right-stuff”之后的流中的下一个项目将需要一个 margin-top 才能处于正确的位置,否则我必须给“right-stuff”一个我自己计算的宽度,或者一个人工的宽度边距(考虑按钮高度)。这里的知识太多了。
有没有更好的方法让这两个项目充当一个连贯的单元,即“右对齐”,但又不脱离流程?
显然,这不是第一次有人问这个问题,但我在这里还没有找到解决这些特定问题的答案,而无需丑陋的黑客(例如手动添加等于按钮高度的 margin-bottom)。
编辑:文本对齐是一个不错的解决方案(无论如何比我想象的要好)。需要注意的是:它假设按钮确实是文本的,并且不适用于图像本身。在我的示例中这是可以的,因为图像是容器中最宽的东西 - 但是如果容器中有另一个比图像宽的元素怎么办?如何使图像保持右侧对齐?
This fiddle demonstrates the problem.
I'm trying to align an image and a button to the right, on two separate lines, as a joint unit. I created a wrapper div ("right-stuff"), with position:relative, so I can use position:absolute on its child, the button.
The problem is I don't know of a good way to align the button to the right without hurting the height calculations.
What I did is give it position:absolute
and right:0
, but this removes it from the flow, and causes the container ("right-stuff") not to include it - see how the red background doesn't reach it, although it "should".
Another problem is that the next item in the flow after "right-stuff" will need a margin-top to be in the correct position, or otherwise I have to either give "right-stuff" a width I calculate myself, or an artifical margin (that takes into account the button height). Too much knowledge here.
Is there a better way to get both items to act as a coherent unit, that is "right aligned", but without taking things out of the flow?
Obviously this is not the first time someone has asked this question, but I haven't found an answer here that addresses these specific concerns without ugly hacks (like manually adding a margin-bottom equal to the button's height).
Edit: text-align is a decent solution (better than what I thought of anyway). One caveat: It assumes the button is indeed textual, and doesn't work on the image itself. This is ok in my example because the image is the widest thing in the container - but what if I had another element in the container that was wider than the image? How would I keep the image aligned to the right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,由于这两个元素(
img
和button
)都是inline-block
,因此您可以简单地使用text-align: right< /代码>。
Yes, since both of those elements (
img
andbutton
) areinline-block
you can simply usetext-align: right
.文本右对齐有什么问题?
小提琴
What's wrong with text-align right?
Fiddle
查看编辑后的小提琴 http://jsfiddle.net/HXH5Z/4/
基本上我已经只是将按钮带回流程中,但将其包含在 div 中,将内容(文本对齐)向右对齐。只需将
text-align: right
规则添加到#right-stuff
div 即可实现相同的效果,但我不知道您是否也希望图像与该 div 内的右侧对齐...check out the editted fiddle at http://jsfiddle.net/HXH5Z/4/
basically i've just brought the button back in the flow, but enclosed it in a div, aligning the content (text-align) to the right. The same could be achieved by just adding the
text-align: right
rule to the#right-stuff
div, but i don't know if you also want the image to be aligned to the right inside that div...