html - 右对齐父 <p> 内的 <span>

标签 html css

我想将换行文本右对齐到 p 的右侧。

这是我当前代码的简化版本:

p {
  text-align: center
}

span {
  text-align: right
}
<p>
  lorem ipsum lorem ipsum lorem ipsum
  <br />
  <span>-author</span>
</p>

但我的跨度仍然居中。如果我使用 float: right 它会在右边,但它不会与 p 标签的最后一个字符内联。设置 max-width 并不理想,因为 lorem ipsum 可以是任意长度。

这是一个 jsfiddle 当前:https://jsfiddle.net/dxgs3u41/

如何将跨度与 p 的最后一个字符右对齐?

最佳答案

一种非位置的可能性,但同样,该段落需要内联而不是display:block

body {
  text-align: center;
}

p {
  display: inline-flex;
  flex-direction: column;
  text-align: center;
  border: 1px solid red;
}

span {
  text-align: right;
}
  <p>
    lorem ipsum lorem ipsum lorem ipsum
    <br />
    <span>-author</span>
  </p>

关于html - 右对齐父 <p> 内的 <span>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52535277/

相关文章:

javascript - 我需要针对每个不同的选择显示不同的输入,并且需要其他人保持隐藏

javascript - element.children 的 console.log 显示长度为 0,但稍后展开时有三个条目

php - 使用 Bootstrap 网格系统在 Wordpress 中将帖子内容分成两列

css - 具有第一列动态宽度的 3 列 div

javascript - Gmail : What is a jQuery selector for incoming mail senders only? 通常如何使用高度压缩和嵌套的 CSS?

html - 在背景图像下重复背景颜色

javascript - html5 : link to the #id of a div at a certain point on the page

html - 媒体查询不响应正确的屏幕尺寸

javascript - react 停止该元素上的其他事件

php - 如何使用 preg_replace php 删除字符串中的 font-family 样式?