代码之家  ›  专栏  ›  技术社区  ›  Blorgbeard

将一些文本右对齐到上面文本的右边缘?

  •  0
  • Blorgbeard  · 技术社区  · 14 年前

    我有几行文字。我希望最后一个右对齐,这样文本就形成了一个很好的列。

    这正是我想要实现的目标:

    Once upon a midnight dreary, while I pondered, weak and weary,
    Over many a quaint and curious volume of forgotten lore,
    While I nodded, nearly napping, suddenly there came a tapping,
    As of some one gently rapping, rapping at my chamber door.
    "'T is some visiter," I muttered, "tapping at my chamber door—
                                     Only this, and nothing more."
    

    我能告诉你最后一行吗:“把你自己和你上面的文字的右边对齐”?

    em s) ,因为如果用户调整或更改字体,我不希望换行。

    1 回复  |  直到 14 年前
        1
  •  3
  •   Jon Purdy    14 年前

    float 包含段落的元素, float: left float: right 最后一行。浮动的容器将与整个段落一样宽,因此右浮动最后一行将使其右边缘与前面文本的右边缘对齐。

    例子:

    <div style="float: left">
    <span style="float: left">
    Once upon a midnight dreary, while I pondered, weak and weary,<br/>
    Over many a quaint and curious volume of forgotten lore,<br/>
    While I nodded, nearly napping, suddenly there came a tapping,<br/>
    As of some one gently rapping, rapping at my chamber door.<br/>
    "'T is some visiter," I muttered, "tapping at my chamber door—<br/>
    </span>
    <span style="float: right; clear: left">Only this, and nothing more."</span>
    </div>
    

    它的 父容器,可以将最上面的设置为 text-align: center display: inline-block 对于文本容器。如果你用这种方法,我认为你不需要浮动它:

    <div style="text-align: center">
    <div style="display: inline-block; text-align: left">
    <span style="float: left">
    Once upon a midnight dreary, while I pondered, weak and weary,<br/>
    Over many a quaint and curious volume of forgotten lore,<br/>
    While I nodded, nearly napping, suddenly there came a tapping,<br/>
    As of some one gently rapping, rapping at my chamber door.<br/>
    "'T is some visiter," I muttered, "tapping at my chamber door—<br/>
    </span>
    <span style="float: right; clear: both">Only this, and nothing more."</span>
    </div>
    </div>