简体   繁体   English

CSS-在:: after中定位内容时遇到麻烦

[英]CSS - Trouble in positioning content in ::after

Sorry for my bad CSS skills. 对不起,我的CSS技能很差。

Problem: I want to add a positive and negative symbol to all elements of a <ol> using CSS (no images!). 问题:我想使用CSS向<ol>所有元素添加正负符号(无图像!)。

First Attempt 第一次尝试

My solution was to put an empty <span> in the beginning of all <li> with a "positive" or "negative" class that has the corresponding CSS to create a positive or negative symbol. 我的解决方案是在所有<li>的开头放置一个空的<span> ,并带有一个“正”或“负”类,该类具有相应的CSS以创建正号或负号。 The problem is that the <ol> must be contenteditable="true" , that means that when someone adds another line ( <li> ) to the <ol> , it will not come with the <span> that creates the symbol with the class "positive" and with the content: "+"; 问题是<ol>必须为contenteditable="true" ,这意味着当某人向<ol>添加另一行( <li> )时, <span> 不会附带该行,该行会使用类“正”, content: "+"; rule in the pseudo-element positive::after . 伪元素positive::after In sum: when adding a new line, it won't come with a positive symbol. 总结:添加新行时,它不会带有正号。

This can be seen in the following code: 可以在以下代码中看到:

 .positive { position: relative; top: 2px; border-radius: 50%; border: 1px solid green; margin-right: 3px; width: 15px; height: 15px; display: inline-flex; font-weight: bold; background-color: white; color: green; } .positive:after { content: "+"; position: relative; left: 2px; bottom: 2px; } .negative { position: relative; top: 2px; border-radius: 50%; border: 1px solid red; margin-right: 3px; width: 15px; height: 15px; display: inline-flex; font-weight: bold; background-color: white; color: red; text-align: center; } .negative:after { content: "−"; position: relative; left: 2px; bottom: 2px; } ol { padding-left: 1.3em; } 
 <ol contenteditable="true"> <li><span class="positive"></span>1st positive</li> <li><span class="positive"></span>2nd positive</li> <li><span class="positive"></span>3rd positive</li> <li><span class="positive"></span>4th positive, please add more below! contenteditable="true"</li> </ol> <ol contenteditable="true"> <li><span class="negative"></span>1st negative</li> <li><span class="negative"></span>2nd negative</li> <li><span class="negative"></span>3rd negative</li> <li><span class="negative"></span>4th negative, please add more below! contenteditable="true"</li> </ol> 

Second Attempt 第二次尝试

So I thought I'd get rid of the <span> and directly insert all CSS in a li.positive::before . 所以我想我会摆脱<span> ,直接将所有CSS插入li.positive::before This, indeed, makes all new <li> s have prefixed a positive or negative symbol, just like I wanted. 的确,这使得所有新的<li>都以正或负符号为前缀,就像我想要的那样。 But now the "+" and "-" content is misaligned in relation to the circle of the positive or negative symbol. 但是现在"+""-" content相对于正号或负号的圆圈未对齐。

This can be seen in the following code: 可以在以下代码中看到:

 .positive:before { border-radius: 50%; border: 1px solid green; margin-right: 3px; width: 15px; height: 15px; display: inline-flex; font-weight: bold; background-color: white; color: green; content: "+"; position: relative; top: 2px; } .negative:before { //border-radius: 50%; border: 1px solid red; margin-right: 3px; width: 15px; height: 15px; display: inline-flex; font-weight: bold; background-color: white; text-align: center; content: "−"; color: red; position: relative; top: 2px; } ol { padding-left: 1.3em; } 
 <ol contenteditable="true"> <li class="positive">1st positive</li> <li class="positive">2nd positive</li> <li class="positive">3rd positive</li> <li class="positive">4th positive, please add more below! contenteditable="true"</li> </ol> <ol contenteditable="true"> <li class="negative">1st negative</li> <li class="negative">2nd negative</li> <li class="negative">3rd negative</li> <li class="negative">4th negative, please add more below! contenteditable="true"</li> </ol> 

Final Question 最后问题

How can I put all CSS in the ::before pseudo-element (just like in my Second Attempt, above) but align the "+" and "-" in the symbols so to put them in the middle of the white circle that surrounds them? 如何将所有CSS放在::before伪元素中(就像上面的“第二次尝试”一样),但是将符号中的“ +”和“-”对齐,以便将它们放在包围的白色圆圈中间他们?

Thank you very much! 非常感谢你!

Simply add justify-content: center to pseudo element (as you already made them display: inline-flex ). 只需在伪元素上添加justify-content: center (因为您已经使它们display: inline-flex )。

You may also adjust the line-height to fix vertical alignment. 您也可以调整line-height以固定垂直对齐方式。

 .positive:before { border-radius: 50%; border: 1px solid green; margin-right: 3px; width: 15px; height: 15px; display: inline-flex; font-weight: bold; background-color: white; color: green; content: "+"; position: relative; top: 2px; justify-content: center; line-height:16px; } .negative:before { //border-radius: 50%; border: 1px solid red; margin-right: 3px; width: 15px; height: 15px; display: inline-flex; font-weight: bold; background-color: white; text-align: center; content: "−"; color: red; position: relative; top: 2px; justify-content: center; line-height:16px; } ol { padding-left: 1.3em; } 
 <ol contenteditable="true"> <li class="positive">1st positive</li> <li class="positive">2nd positive</li> <li class="positive">3rd positive</li> <li class="positive">4th positive, please add more below! contenteditable="true"</li> </ol> <ol contenteditable="true"> <li class="negative">1st negative</li> <li class="negative">2nd negative</li> <li class="negative">3rd negative</li> <li class="negative">4th negative, please add more below! contenteditable="true"</li> </ol> 

3 Ways to Create an Ordered List with Font Icons 3种方法来创建带有字体图标的有序列表

Using so many alignments seems fragile. 使用如此多的对齐方式似乎很脆弱。 As an alternative, you could use font icons instead. 或者,您可以改用字体图标。

Pseudo-class Font 伪字体

Demo 1 演示1

Use \\2295 and \\229f albeit they are not as well polished as OP's but they are one piece. 使用\\2295\\229f尽管它们的抛光度不如OP的好,但它们是一体的。

 li { line-height: 1; vertical-align: baseline; margin: 1.2ex 0; } .positive:before { content: '\\2295\\a0'; color: green; font-weight: 100; line-height: 1; vertical-align: text-top; display: inline } .negative:before { content: '\\229f\\a0'; color: tomato; line-height: 1; vertical-align: text-top; display: inline } ol { padding-left: 1.3em; } 
 <ol contenteditable="true"> <li class="positive">1st positive</li> <li class="positive">2nd positive</li> <li class="positive">3rd positive</li> <li class="positive">4th positive, please add more below! contenteditable="true"</li> </ol> <ol contenteditable="true"> <li class="negative">1st negative</li> <li class="negative">2nd negative</li> <li class="negative">3rd negative</li> <li class="negative">4th negative, please add more below! contenteditable="true"</li> </ol> 


Pseudo-class Font Awesome 伪类字体很棒

Demo 2 演示2

If appearance is the main concern, take it to the next level with Font Awesome (see Demo 2) and use their icons by pseudo-class. 如果外观是主要考虑因素,请使用Font Awesome (请参见演示2)将其提升到一个新的水平,并按伪类使用其图标。 Use this site for reference . 参考网站 One problem is that they do not have a line circle with plus icon, but strangely enough they do have a line square with plus, a filled square with plus, and a filled circle with plus. 一个问题是他们没有带有加号图标的线圆,但是奇怪的是,他们确实有带有加号的线正方形,带有加号的填充正方形和带有加号的填充圆圈。

 li { line-height: 1; vertical-align: baseline; margin: 1.2ex 0; } .positive:before { font-family: FontAwesome; content: '\\f196\\a0'; color: green; font-weight: 100; line-height: 1; vertical-align: text-top; display: inline } .negative:before { font-family: FontAwesome; content: '\\f147\\a0'; color: tomato; line-height: 1; vertical-align: text-top; display: inline } .positive.inv::before { content: '\\f0fe\\a0'; } .negative.inv::before { content: '\\f146\\a0'; } .positive.orb::before { content: '\\f055\\a0'; } .negative.orb::before { content: '\\f056\\a0'; } ol padding-left: 1.3em; } 
 <link href="https://cdn.jsdelivr.net/fontawesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"> <ul class="fa-ul"> <li><i class="fa-li fa fa-plus-circle"></i>can be used</li> <li><i class="fa-li fa fa-minus-circle"></i>can be used</li> <li><i class="fa-li fa fa-plus-square"></i>as bullets</li> <li><i class="fa-li fa fa-minus-square"></i>in lists</li> <li><i class="fa-li fa fa-plus-circle-o"></i>can be used</li> <li><i class="fa-li fa fa-minus-circle-o"></i>can be used</li> <li><i class="fa-li fa fa-plus-square-o"></i>as bullets</li> <li><i class="fa-li fa fa-minus-square-o"></i>in lists</li> </ul> <ol contenteditable="true"> <li class="positive orb">1st positive</li> <li class="positive inv">2nd positive</li> <li class="positive">3rd positive</li> </ol> <ol contenteditable="true"> <li class="negative orb">1st negative</li> <li class="negative inv">2nd negative</li> <li class="negative">3rd negative</li> </ol> 


Font Awesome Standard with CSS Counters 带有CSS计数器的Font Awesome Standard

Demo 3 演示3

There's also the standard method using .fa-* classes. 还有使用.fa-*类的标准方法。 with the use of CSS counters to compensate for the lack of an <ol> alternative (see Demo3). 使用CSS counters来弥补<ol>替代方案不足 (请参见Demo3)。

 .fa-ul { counter-reset: fa; line-height: 1.3 } .fa-ul li { counter-increment: fa; position: relative } .fa-ul li::before { content: counter(fa)'.\\a0'; position: absolute; left: -4.5ch } 
 <link href="https://cdn.jsdelivr.net/fontawesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"> <ul class="fa-ul"> <li><i class="fa-li fa fa-plus-circle"></i>fa-plus-circle</li> <li><i class="fa-li fa fa-minus-circle"></i>fa-minus-circle</li> <li><i class="fa-li fa fa-plus-square"></i>fa-plus-square</li> <li><i class="fa-li fa fa-minus-square"></i>fa-minus-square</li> <li><i class="fa-li fa fa-plus-circle-o">?</i>fa-plus-circle-o</li> <li><i class="fa-li fa fa-minus-circle-o">?</i>fa-minus-circle-o</li> <li><i class="fa-li fa fa-plus-square-o"></i>fa-plus-square-o</li> <li><i class="fa-li fa fa-minus-square-o"></i>fa-minus-square-o</li> </ul> 

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

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