html - 如何使用css使文本与列表中的文本垂直对齐

标签 html css

我正在为我的列表使用支票的背景图像。问题在于,出于某种原因,列表项下一行的文本与其上方的文本不对齐,而是支票图像。我试过使用 list-style-position: outside; 但它似乎不起作用。

这是我的代码:

li {
  list-style: none;
  margin-bottom: 0.5em;
  text-indent: 1.2em;
  background-image: url(https://placehold.it/15x15);
  background-repeat: no-repeat;
  letter-spacing: 1px;
  font-weight: 200;
  font-size: 12px;
  list-style-position: outside;
  line-height: 1.2;
}
.checklist-left {
  width: 50%;
  float: left;
  text-align: left;
}
.checklist-right {
  width: 50%;
  float: right;
  text-align: left;
}
<ul class="checklist-left">
  <li>Improve clarity</li>
  <li>Check formatting</li>
  <li>Improve logical flow</li>
</ul>
<ul class="checklist-right">
  <li>Eliminate irrelevant words</li>
  <li>Verify adherence to stylistic guidelines</li>
</ul>

这是它的样子:

enter image description here

感谢任何帮助。

最佳答案

问题是您使用的 text-indent 只会调整文本的第一行。

The text-indent property specifies how much horizontal space should be left before the beginning of the first line of the text content of an element.

文本缩进 ( https://developer.mozilla.org/en-US/docs/Web/CSS/text-indent )

要修复,请对您的 CSS 进行以下修改:

  • li
  • 中删除 text-indent: 1.2em;
  • 添加 padding-left: 1.2em;li

这会将 padding 应用到 li 的整个左侧,插入内容并允许 background-image 使用空白空间。

li {
  list-style: none;
  margin-bottom: 0.5em;
  padding-left: 1.2em;
  background-image: url(https://placehold.it/15x15);
  background-repeat: no-repeat;
  letter-spacing: 1px;
  font-weight: 200;
  font-size: 12px;
  list-style-position: outside;
  line-height: 1.2;
}
.checklist-left {
  width: 50%;
  float: left;
  text-align: left;
}
.checklist-right {
  width: 50%;
  float: right;
  text-align: left;
}
/*Added to make the issue visible in the snippet*/
.checklist-left, .checklist-right {
  padding: 0;
  width: 30%;
}
<ul class="checklist-left">
  <li>Improve clarity</li>
  <li>Check formatting</li>
  <li>Improve logical flow</li>
</ul>
<ul class="checklist-right">
  <li>Eliminate irrelevant words</li>
  <li>Verify adherence to stylistic guidelines</li>
</ul>

list-style-position 在使用背景图像时不会有任何效果,因为它只会影响实际复选标记的位置。

The list-style-position property specifies the position of the marker box in the principal block box.

列表样式位置 ( https://developer.mozilla.org/en/docs/Web/CSS/list-style-position )

关于html - 如何使用css使文本与列表中的文本垂直对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35055639/

相关文章:

css - CSS 三 Angular 形是如何工作的?

javascript - 基于浏览器jQuery触发点击

css - 创建具有多行和多列的 Html 边框

javascript - Vuetify 密码确认不起作用

javascript - Jquery index() 找不到我的元素

HTML CSS 响应式段落标签

css - 按钮上的阴影框停留在不活动的 html 页面上

HTML 类未在浏览器中加载

javascript - 将元素设置为事件状态,将其他元素设置为非事件状态(动态)

html - Tailwind - 绝对位置与相对位置下拉列表