简体   繁体   English

如何以渐变样式在列表的左侧留下自定义项目符号?

[英]How to make a border left with custom bullet for my list, in a gradient style?

This is what I want to achieve: 这是我要实现的目标:

在此处输入图片说明

Items with white background are my list items. 白色背景的项目是我的列表项目。 On the left side I want to have a border, with custom bullets (custom, since we cannot change the default list bullet color -black- AFAIK). 在左侧,我想有一个带有自定义项目符号的边框(自定义,因为我们无法更改默认列表项目符号颜色-black- AFAIK)。 The upper part of the border should have a gradient color, from transparent to grey color for instance. 边框的上部应具有渐变颜色,例如,从透明到灰色。 As you also see in the pic, the length of the border should be longer than the actual list height (until the plus button, not till the last item ends.) 正如您在图片中所看到的,边框的长度应大于列表的实际高度(直到加号按钮,直到最后一项结束为止。)

I have actually achieved some parts but i would like to know a better, cleaner way of doing it. 我实际上已经取得了一些成就,但是我想知道一种更好,更清洁的方式。

This is what I have so far: https://jsfiddle.net/6esLm8q1/ 到目前为止,这是我所拥有的: https : //jsfiddle.net/6esLm8q1/

 .list { list-style: none; border-width: 2px; border-style: solid; border-image: linear-gradient(to bottom, rgba(0, 0, 0, 0), rgba(179,179,179)) 0 0 0 1; } .item { margin-bottom: 1em; margin-left: -1.7em; } .item::before { content: "\\2022"; color: grey; font-weight: bold; display: inline-block; width: 1em; margin-left: -1em; } 
 <ul class="list"> <li class="item"> Test1 </li> <li class="item"> Test2</li> </ul> <button>Plus </button> 

Problems are still: Aligning of bullets to item text, even if I align the bullets on the border, when I resize the window, the bullets slides to left or right slightly. 问题仍然存在:即使将项目符号对齐在边框上,项目符号与项目文本的对齐方式,当我调整窗口大小时,项目符号也会稍微向左或向右滑动。

The gradient line is far more transparent at the beginning, not actually like in the "Target" pic. 渐变线在开始时更加透明,实际上不像“目标”图片中那样。 And the border ends where the list items end, so it does not reach until the button. 边框在列表项的结尾处结束,因此直到按钮才到达边界。

I appreciate any help till I get something close to my target pic! 在获得接近目标图片的帮助之前,我将不胜感激!

You got yourself 99% there. 您有99%的数据在那里。 For the linear-gradient , if you define the end-point to be lower than 100% , then you get it to fade earlier. 对于linear-gradient ,如果将端点定义为低于100% ,则可以使其淡入淡出。 Otherwise, it calculates the end point to be 100% and thus half of your border looks faded. 否则,它将计算终点为100% ,因此边框的一半看上去已变淡。

For the before psuedo-element, since in your markup you put a p tag within each li , you should put the pseudo-element on the p tag itself. 对于before psuedo元素,由于在标记中您在每个li都放置了p标签,因此应将伪元素放在p标签本身上。

See the snippet below. 请参见下面的代码段。

 .list { list-style: none; border-width: 2px; border-style: solid; border-image: linear-gradient(to bottom, rgba(0, 0, 0, 0) 0%, rgba(179,179,179) 40%) 0 0 0 1; /* add % to have gradient end earlier than end */ } /* add before pseudo-element on p tag (since you include it in your markup */ .item>p:before { content: "\\2022"; color: grey; font-weight: bold; margin-left: -2.7em; } 
 <ul class="list"> <li class="item"> <p> Test1 </p> </li> <li class="item"> <p> Test2 </p> </li> </ul> <button>Plus</button> 

I think your solution is already pretty good. 我认为您的解决方案已经不错。 I would create the circles with border-radius , as you have more control over the sizing and position. 我将使用border-radius创建圆,因为您可以更好地控制尺寸和位置。

Here is an example: 这是一个例子:

 .list { list-style: none; border-width: 2px; border-style: solid; border-image: linear-gradient(to bottom, rgba(0, 0, 0, 0), rgba(179, 179, 179)) 0 0 0 1; margin: 0 0 0 1em; } .item { position: relative; } .item:before { position: absolute; content: ""; width: 6px; height: 6px; border-radius: 50%; background-color: grey; left: -2.75em; top: .4em; } button { display: inline-block; width: 2em; height: 2em; margin: -.2em 0 0 .25em; border-radius: 50%; background: #c00; color: white; border: none; } 
 <ul class="list"> <li class="item"> <p>Test1<br>test with line break</p> </li> <li class="item"> <p>Test2<br>test with<br>two line breaks</p> </li> </ul> <button>+</button> <ul class="list"> <li class="item"> <p>Test1</p> </li> <li class="item"> <p>Test2</p> </li> </ul> <button>+</button> 

Is it critical to be plain css? 普通的CSS至关重要吗? From my point of view you're trying to solve the issue with the wrong instruments. 从我的角度来看,您正在尝试使用错误的工具来解决问题。

What if you split the element, making the line on the left and the bullet with separate elements, positioned absolutely in the padding region of the container, and the content in subcontinent? 如果您拆分元素,将左边的线和项目符号分别放在容器的填充区域中,并将内容放在子大陆中,则将左侧的线和带有单独元素的项目符号分开,该怎么办?

Something like this: 像这样:

 body { background-color: #ededed; } .tail { border-left-style: solid; border-left-width: 2px; border-image: linear-gradient(to bottom, rgba(0, 0, 0, 0), rgba(179,179,179)) 0 0 0 1; height: 20px; margin-left: 20px; } .list { font-size: 20px; color: rgba(179,179,179); list-style: disc; border-left: 2px solid rgba(179,179,179); margin: 0 0 0 20px; box-sizing: border-box; padding-bottom: 20px; padding-left: 15px; } .item { margin-bottom: 5px; } .item > div { color: #555; background-color: white; padding: 5px; font-size: 14px; font-family: sans; border-radius: 5px; } button { border-radius: 50%; background-color: #b30920; border: none; color: white; box-sizing: border-box; padding: 0; width: 40px; height: 40px; overflow: hidden; vertical-align: top; font-size: 33px; margin: 0; } 
 <div class="tail"></div> <ul class="list"> <li class="item"><div>Test1</div></li> <li class="item"><div>Test2<br>test</div></li> </ul> <button>+</button> 

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

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