html - 在同一行上对齐文本而不 float

标签 html css

我试图在不使用 float 的情况下在同一行对齐文本,因为我正在使用的 html2pdf 转换器 不支持它。

代码如下:

.text1 {
    padding: 10px 20px 0px 20px;
    text-align: left;
}
.go-right {
    display: inline-block;
    text-align: right;
}
 <div class='text1'>
    Bill to: <span class="go-right">text</span> <br/>
    Amount:  <br/>
    Date: 
 </div>

如果我将 display 更改为右侧,文本将在右侧对齐,但是在单独的一行上,这不是想要的结果。

最佳答案

您可以简单地向 span 添加固定宽度并根据需要调整值:

.text1 {
    padding: 10px 20px 0px 20px;
    text-align: left;
}
.go-right {
    display: inline-block;
    text-align: right;
    width:calc(100% - 70px); /*Replace with fixed value if calc not supported*/
}
<div class='text1'>
    Bill to: <span class="go-right">text</span> <br/>
    Amount:  <br/>
    Date: 
 </div>

你也可以考虑 padding-left 来推送内容:

.text1 {
  padding: 10px 20px 0px 20px;
  text-align: left;
}

.go-right {
  display: inline-block;
  text-align: right;
  padding-left: 120px;
}
<div class='text1'>
  Bill to: <span class="go-right">text</span> <br/> Amount: <br/> Date:
</div>

更新

由于转换器不能很好地支持上述两种方法,您可以考虑像这样的经典表结构:

table {
 width:100%;
}
<table>
  <tr>
    <td>Bill to:</td>
    <td>text</td>
  </tr>
  <tr>
    <td>Amount:</td>
    <td></td>
  </tr>
  <tr>
    <td>Date:</td>
    <td></td>
  </tr>
</table>

关于html - 在同一行上对齐文本而不 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47920575/

相关文章:

javascript - 无法使用 jQuery 按钮链接连接外部 HTML 文件

html - 自动在新窗口中打开每个外部链接(html、css)

javascript - "Tab"移动网络应用程序菜单栏快速切换

html - 对话框组件中的primefaces日历问题

javascript - 单击按钮添加 php include

iphone - Zxing for iphone 使用 HTML5/javascript

javascript - 安全错误 : The operation is insecure - window. history.pushState()

jquery - 无法滚动到网页中div的底部

html - 计算出的字体大小大于 Asus Nexus 7 上 CSS 中定义的大小

html - Angular 使固定显示元素与 "sibling"大小相同