简体   繁体   English

将自定义 svg 列表项项目符号点与文本垂直对齐

[英]Vertically aligning custom svg list item bullet points with text

I have created a custom SVG bullet point icon per the suggested answer in this stackoverflow post and now I am trying to "vertically align" the li bullet point such that it is vertically centered with the item text.我已经根据此stackoverflow 帖子中的建议答案创建了一个自定义 SVG 项目符号图标,现在我尝试“垂直对齐” li项目符号点,使其与项目文本垂直居中。

Here is the current outcome displaying the list with the custom svg bullet points:这是显示带有自定义 svg 要点的列表的当前结果:

要点太高了

Expected outcome: bullet point icons are "middle aligned" with each li 's text预期结果:项目符号图标与每个li的文本“中间对齐”

I have tried multiple permutations of how to do this, notably referencing code from here , here , and here and in each attempted case, when I refresh the page (local file) for the changes, the SVG bullets are no longer visible in the list.我已经尝试了如何做到这一点的多种排列,特别是从这里这里这里引用代码,在每个尝试的情况下,当我刷新页面(本地文件)进行更改时,SVG 项目符号在列表中不再可见. (they're not visible anywhere on the page) (它们在页面上的任何地方都不可见)

Here is a sample of what I have tried:这是我尝试过的示例:

HTML layout: HTML布局:

<div class="main-container">
<div class = "list-section">
<ul>
    <li>This is my first list item, padded out for double line test!</li>
    <li>This is the second list item, also suitably padded out for testing</li>
    <li>Third item list, about same level of padding!</li>
</ul>
</div>
</div>

Attempt 1尝试 1

li.list-section:before{
    content: url("data:image/svg+xml, %3Csvg width='20' height='20' viewBox='0 0 20 20' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M7 10L9 12L13 8M19 10C19 14.9706 14.9706 19 10 19C5.02944 19 1 14.9706 1 10C1 5.02944 5.02944 1 10 1C14.9706 1 19 5.02944 19 10Z' stroke='%2396D8A0' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    position:relative;
    top:10px;
    left:-10px;
}

.list-section li{
    font-size: 14px;
    color: #6D6D6D;
    margin: 16px 0px;

Attempt 2尝试 2

.list-section li{
    list-style-image: url("data:image/svg+xml, %3Csvg width='18' height='18' viewBox='0 0 20 20' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M7 10L9 12L13 8M19 10C19 14.9706 14.9706 19 10 19C5.02944 19 1 14.9706 1 10C1 5.02944 5.02944 1 10 1C14.9706 1 19 5.02944 19 10Z' stroke='%2396D8A0' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    font-size: 14px;
    color: #6D6D6D;
    margin: 16px 0px;
    position: relative;
    display:flex;
    align-items: center;
}

Your train of thought is correct.你的思路是对的。 Only the code contains typos.只有代码包含错别字。

If I understand correctly, then the result should be as follows ( run the snippet and resize the block by grabbing it by the lower right corner ):如果我理解正确,那么结果应该如下(运行代码片段并通过抓住它的右下角来调整块的大小):

  • With flex and transform使用 flex 和转换

 .main-container { /* For example only */ resize: both; overflow: scroll; min-height: 100px; min-width: 100px; } .list-section ul { list-style-type: none; margin: 0; } .list-section ul li { position: relative; margin: 1em 0; display: flex; align-items: center; font-size: 14px; color: #6D6D6D; } .list-section ul li::before { content: url("data:image/svg+xml, %3Csvg width='20' height='20' viewBox='0 0 20 20' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M7 10L9 12L13 8M19 10C19 14.9706 14.9706 19 10 19C5.02944 19 1 14.9706 1 10C1 5.02944 5.02944 1 10 1C14.9706 1 19 5.02944 19 10Z' stroke='%2396D8A0' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E"); transform: translatex(-50%); }
 <div class="main-container"> <div class="list-section"> <ul> <li>This is my first list item, padded out for double line test!</li> <li>This is the second list item, also suitably padded out for testing</li> <li>Third item list, about same level of padding!</li> </ul> </div> </div>

  • With position and transform带有位置和变换

 .main-container { /* For example only */ resize: both; overflow: scroll; min-height: 100px; min-width: 100px; } .list-section ul { list-style-type: none; margin: 0; } .list-section ul li { position: relative; margin: 1em 0; font-size: 14px; color: #6D6D6D; } .list-section ul li::before { content: url("data:image/svg+xml, %3Csvg width='20' height='20' viewBox='0 0 20 20' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M7 10L9 12L13 8M19 10C19 14.9706 14.9706 19 10 19C5.02944 19 1 14.9706 1 10C1 5.02944 5.02944 1 10 1C14.9706 1 19 5.02944 19 10Z' stroke='%2396D8A0' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E"); position: absolute; top: 50%; left: 0; transform: translate(-150%, -50%); }
 <div class="main-container"> <div class="list-section"> <ul> <li>This is my first list item, padded out for double line test!</li> <li>This is the second list item, also suitably padded out for testing</li> <li>Third item list, about same level of padding!</li> </ul> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM