javascript - 如何将两个 float 的 HTML 标签与自动换行对齐?

标签 javascript html css

我正在尝试对齐两个 float 的 HTML 元素,使其在另一侧面向正确的元素。 下面的 HTML 更清晰。

label{
  float:left;
  width:20%;
  padding-right:2.5%;
  word-wrap:break-word;
  background:red;
}
span{
  float:right;
  width:75%;
  padding-left:2.5%;
  word-wrap:break-word;
  background:green;
}
<div>
  <p>
    <label>OrderId:</label>
    <span>4857924875928475924</span>
  </p>
  <p>
    <label>Comment:</label>
 <span>This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text.</span>
  </p>
  <p>
    <label>User:</label>
    <span>User_Name</span>
  </p>
</div>

当评论足够长时,上面的脚本会错位用户:User_Name。

我在这里遗漏了什么吗?

谢谢

最佳答案

定义你的p 标签 p{overflow:hidden;}

因为你使用了floating element 只需清除你的p tag

label{
  float:left;
  width:20%;
  padding-right:2.5%;
  word-wrap:break-word;
  background:red;
}
p{overflow:hidden;}
span{
  float:right;
  width:75%;
  padding-left:2.5%;
  word-wrap:break-word;
  background:green;
}
<div>
  <p>
    <label>OrderId:</label>
    <span>4857924875928475924</span>
  </p>
  <p>
    <label>Comment:</label>
 <span>This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text.</span>
  </p>
  <p>
    <label>User:</label>
    <span>User_Name</span>
  </p>
</div>

more about this

关于javascript - 如何将两个 float 的 HTML 标签与自动换行对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34058507/

相关文章:

html - Bootstrap 隐藏纵向但显示横向

javascript - 如何将经度和纬度转换为街道地址

html - 隐藏任何 HTML 标签的正确方法是什么?

javascript - 让javascript在任何链接上工作

html - 如何在顶部有一个菜单,链接左对齐和右对齐

html - 在第一个加载页面加载悬停图片

javascript - ExtJS 基于拾色器动态改变按钮背景颜色

jquery - 隐藏特定 Jquery UI 微调器的微调器按钮

javascript - 我如何导入这些 es6 导出的内容?它们是命名为导出还是默认?

javascript - 在 javascript 中使用自定义条件分割字符串(不重复)