简体   繁体   English

如何将伪元素放置在更高的位置

[英]How to position a pseudo element higher

Very simple question: I have a pseudo element (right-pointing arrows) that I would like position higher than they currently are. 一个非常简单的问题:我有一个伪元素(向右箭头),我希望其位置高于当前位置。 I've tried changing the line-height , but that changes the height of the element itself. 我尝试过更改line-height ,但这会更改元素本身的高度。

A basic outline of my problem: 我的问题的基本概述:

CSS: CSS:

ol
{
    list-style-type: none;
}

ol > li
{
    display: inline;
}

ol > li + li:before 
{
    font-size: 8px;
    content: "➤"; 
    /* Want this to be positioned higher, so that it's right between the vertical height of the list items rather than floated near the bottom */
}

HTML: HTML:

<ol>
   <li>
       <a>List item 1</a>
   </li>
   <li>
       <a>List item 2</a>
   </li>
   <li>
       <a>List item 3</a>
   </li>    
</ol>

And a fiddle: http://jsfiddle.net/hxb5gy5j/ 和一个小提琴: http : //jsfiddle.net/hxb5gy5j/

Add vertical-align: middle; 添加vertical-align: middle; and line-height: 0px; line-height: 0px; to li:before like this: li:before像这样:

DEMO 演示

 ol { list-style-type: none; } ol > li { display: inline; } ol > li + li:before { font-size: 8px; content:"➤"; vertical-align: middle; line-height: 0px; } 
 <ol> <li> <a>List item 1</a> </li> <li> <a>List item 2</a> </li> <li> <a>List item 3</a> </li> </ol> 

One quick solution is to use position: relative and top: -2px : 一种快速的解决方案是使用position: relativetop: -2px

 ol { list-style-type: none; } ol > li { display: inline; } ol > li + li:before { font-size: 8px; content:"➤"; position: relative; top: -2px; } 
 <ol> <li> <a>List item 1</a> </li> <li> <a>List item 2</a> </li> <li> <a>List item 3</a> </li> </ol> 

If you want a higher degree of control over where you can place your bullet, you can use absolute positioning: 如果要对放置子弹的位置进行更高程度的控制,可以使用绝对定位:

  1. Set ol > li + li:before to position:absolute . ol > li + li:beforeposition:absolute
  2. Set its parent, ol > li to position:relative . 将其父级ol > liposition:relative
  3. You'll also need to add some padding to ol > li to clear the bullet: padding-left:10px; 您还需要向ol > li添加一些填充以清除项目符号: padding-left:10px; :

HTML: HTML:

<ol>
   <li>
       <a>List item 1</a>
   </li>
   <li>
       <a>List item 2</a>
   </li>
   <li>
       <a>List item 3</a>
   </li>    
</ol>

CSS: CSS:

ol
{
    list-style-type: none;
}

ol > li
{
    display: inline;
    position:relative; /* add relative positioning to parent */
    padding-left:10px; /* add padding to clear space for bullet */
}

ol > li + li:before 
{
    font-size: 8px;
    content: "➤"; 
    position:absolute; /* absolutely position pseudo element */
    top:.5em; /* you now have lots of control over exact position of bullet */
    left:0;
    /* Want this to be positioned higher, so that it's right between the vertical height of the list items rather than floated near the bottom */
}

As you can see, this requires a little bit more setup, specifically adding extra padding to the parent <li> element to clear space for the bullet. 如您所见,这需要更多的设置,特别是要向父<li>元素添加额外的填充以清除项目符号的空间。 But it also gives you a lot more control and the ability to do something more customized. 但是,它还为您提供了更多的控制权和执行更多自定义功能的能力。 It depends on what you're trying to achieve. 这取决于您要实现的目标。

Fiddle: http://jsfiddle.net/Lecxg3mp/1/ 小提琴: http : //jsfiddle.net/Lecxg3mp/1/

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

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