CSS 将内联元素浮动到 div 的右侧

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2430074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 21:42:02  来源:igfitidea点击:

Floating an inline element to the right of a div

csscss-float

提问by Rajat

I want to right-align an inline element to the right of a div. I have seen float="right" applied on a span to right align it but it seems semantically incorrect to me as floats are supposed to move "boxes" or block elements to the right or left of a container element.

我想将内联元素右对齐到 div 的右侧。我已经看到 float="right" 应用于跨度以使其右对齐,但在我看来这在语义上是不正确的,因为浮动应该将“框”或块元素移动到容器元素的右侧或左侧。

Is my understanding of Float wrong or is there another way of right-aligning inline elements in a container DIV.

我对 Float 的理解是错误的还是有另一种方法可以在容器 DIV 中右对齐内联元素。

采纳答案by David says reinstate Monica

Floating an element automatically makes it into a box. (See: http://css.maxdesign.com.au/floatutorial/introduction.htm) However, there is nothing semantically incorrect about it. Semantics don't dictate what should be displayed as a block and what should be displayed inline--that's one reason we can declare that arbitrarily. (It certainly becomes messywhen you start taking words out of their order--but I doubt that's what you're talking about here, as I can't imagine a case when you'd want to do that andhave it remain inline.)

浮动元素会自动使其成为一个框。(请参阅:http: //css.maxdesign.com.au/floatutorial/introduction.htm)但是,它在语义上没有任何错误。语义并没有规定什么应该显示为块,什么应该显示为内联——这就是我们可以任意声明的原因之一。(当您开始不按顺序使用单词时,它肯定会变得混乱- 但我怀疑这就是您在这里谈论的内容,因为我无法想象您想要这样做使其保持内联状态的情况。 )

Moreover, if you need to align a bit of inline text independent from the flow of normal text (which is what a float does), you really want to display it as a block anyway. If it's really an inline element, all you need is text-align:right--anything you need a float or a position declared on you are already treating as a block element, so there's nothing wrong with declaring it as such.

此外,如果您需要对齐一些独立于普通文本流的内联文本(这就是浮点数所做的),无论如何您真的希望将其显示为块。如果它真的是一个内联元素,那么你所需要的只是——text-align:right任何你需要在上面声明的浮点数或位置的东西都已经被视为块元素,所以这样声明它没有错。

回答by David says reinstate Monica

float: rightis perfectly okay applied to all elements, block-level or inline it doesn't matter, either semantically or according to the spec (so far as I'm aware).

float: right完全可以应用于所有元素,块级或内联它无关紧要,无论是语义上还是根据规范(据我所知)。

If you want to right-align something without using floatthen there's the possibility of margin-right: 90%;(assuming you know that what it's right-aligned from/against fits into the other 10%.

如果你想在不使用的情况下右对齐某些东西,float那么有可能margin-right: 90%;(假设你知道它从/反对右对齐的东西适合其他 10%。

Or direction: rtl;but that never works like I think it should, plus it'd likely complicate things.

或者direction: rtl;但这从来没有像我认为的那样工作,而且它可能会使事情复杂化。

position: absolute; right: 0;would do as you need (but it'd be removed from the document's flow, and it would be positioned against the first of its parent elements that has a defined position: relative;(or at least defined position).

position: absolute; right: 0;会按照你的需要做(但它会从文档的流中删除,并且它会被定位在它的第一个具有定义position: relative;(或至少定义position)的父元素上。

You could, possibly, use text-align: right, but that seems like such a simple solution that I'm sure you'll have already tried it.

您可能可以使用text-align: right,但这似乎是一个如此简单的解决方案,我相信您已经尝试过了。

If you could provide a use-case, some code and an indication of your expected end-result we might be able to help you more.

如果您可以提供用例、一些代码和预期最终结果的指示,我们可能能够为您提供更多帮助。