如何显示:after元素外部元素

Ben*_*Ben 12 html css

在下面的例子中,我怎样才能使得生成的文本:after在span的框外?(它目前在内部渲染,即它使span元素更宽)

HTML

<span class="my">Boxtext</span>
Run Code Online (Sandbox Code Playgroud)

CSS

span.my {
    padding:            4px 8px;
    background-color:   red;
    display:            inline-block;
    border-radius:      10px;
    margin-left:        20px;
    }   
span.my:after {
    content:            " text that is supposed to come after the box";
    }
Run Code Online (Sandbox Code Playgroud)

我想这有点类似于list-style-position,你可以在里面或外面选择......

Joe*_*oel 8

我相信CSS内容被认为是呈现它的对象的一部分.您可以创建:after应该已命名的参数:append.

对于您的情况,您可以尝试在其中添加一个额外元素span.my:

<span class="my"><span>Boxtext</span></span>
span.my span { ... }
span.my:after { ... }
Run Code Online (Sandbox Code Playgroud)

或具体设计内容样式.

span.my:after { 
    content:            " text that is supposed to come after the box";
    background-color:   white;
    position:           absolute;
    margin-left:        10px;
}
Run Code Online (Sandbox Code Playgroud)


Dav*_*mas 5

只需position: absolute::after {}伪元素的 css 中使用:

span.my:after {
    content: " text that is supposed to come after the box";
    position: absolute;
    left: 100%;
}?
Run Code Online (Sandbox Code Playgroud)

当然,请记住在父元素上使用position: relative;(或任何其他position值)span.my

JS小提琴演示

还请记住,由于::after(和::before)伪元素从它“附加”的范围继承,它将继承width,因此可能需要在 CSS 中为该/那些元素显式覆盖。