javascript - 如何向前而不是向后添加文本

标签 javascript html css

我想将行集合与每行前面的标题对齐。但是行的位置会根据标题的长度而改变。所以我想如果我可以向前而不是向后增加文本的长度,我也许能够解决这个问题。但我不知道该怎么做。感谢您的帮助!

这是我的 HTML:

<div>
  <span class="title">1</span>
  <a href="#" class="cells">one</a>
  <a href="#" class="cells">two</a>
  <a href="#" class="cells">three</a>
  <a href="#" class="cells">four</a>
  <a href="#" class="cells">five</a>
</div>
<div>
  <span class="title">100</span>
  <a href="#" class="cells">one</a>
  <a href="#" class="cells">two</a>
  <a href="#" class="cells">three</a>
  <a href="#" class="cells">four</a>
  <a href="#" class="cells">five</a>
</div>

无论标题长短,我希望它看起来像什么: enter image description here

现在的样子: enter image description here

这是一个 working sample .

最佳答案

一种方法是将 div 显示为表格行,将 span 和链接显示为单元格:

.title, .cells {
  display: table-cell;
}
div {
  display: table-row;
}

.cells {
  min-width: 10px;
  min-height: 10px;
  border: 1px solid black;
  margin-right: 0;
}

.title {
  min-width: 20px;
  text-align: right;
}

.title,
.cells {
  display: table-cell;
}

div {
  display: table-row;
}
<div>
  <span class="title">1</span>
  <a href="#" class="cells">one</a>
  <a href="#" class="cells">two</a>
  <a href="#" class="cells">three</a>
  <a href="#" class="cells">four</a>
  <a href="#" class="cells">five</a>
</div>
<div>
  <span class="title">100</span>
  <a href="#" class="cells">one</a>
  <a href="#" class="cells">two</a>
  <a href="#" class="cells">three</a>
  <a href="#" class="cells">four</a>
  <a href="#" class="cells">five</a>
</div>

关于javascript - 如何向前而不是向后添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46082339/

相关文章:

javascript - 使用 XMLHttpRequest.responseText 解析 XML 文件不会显示在 div 上

html - 升级 Font Awesome 后,字体 'search' 的 Unicode 字符未显示

javascript - 创建一个可重用的函数来打开和发送 Ajax

即时创建 Jquery 元素

javascript - 在javascript中按值传递匿名函数?

jquery - 来自部分的内容正在插入另一个部分

css - 隐藏的溢出无法按预期在内联元素上工作

html - css 不透明度过渡不起作用

javascript - 有没有简单的方法可以找到 jQuery 代码的 "javascript equivalent"?

javascript - 为什么我必须使用 this.canvas?