Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display the :after content outside element

Tags:

html

css

In the following case, how can I make it such that the text generated by the :after is outside the box of the span? (It currently is rendered inside, i.e. it makes the span element wider)

HTML

<span class="my">Boxtext</span>

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";
    }

I guess this is somewhat similar to list-style-position, where you can chose inside or outside...

like image 494
Ben Avatar asked Apr 09 '12 17:04

Ben


People also ask

How do you display an element on top of another element?

You can use the CSS position property in combination with the z-index property to overlay an individual div over another div element. The z-index property determines the stacking order for positioned elements (i.e. elements whose position value is one of absolute , fixed , or relative ).

What is after pseudo-element?

In CSS, ::after creates a pseudo-element that is the last child of the selected element. It is often used to add cosmetic content to an element with the content property. It is inline by default.

How do you use before and after pseudo elements?

CSS ::before and ::after pseudo-elements allow you to insert “content” before and after any non-replaced element (e.g. they work on a <div> but not an <input> ). This effectively allows you to show something on a web page that might not be present in the HTML content.

Can buttons have pseudo elements?

Yes you can use it – as long as you as don't need to support some very old browsers, e.g. MS IE 7 or lower. I don't know of any other browser that doesn't understand pseudo elements on empty HTML tags.


2 Answers

I believe CSS content is considered part of the object against which it was rendered. You could make the argument that :after should have been named :append.

For your case you can try putting an extra element inside span.my:

<span class="my"><span>Boxtext</span></span>
span.my span { ... }
span.my:after { ... }

Or styling the content specifically.

span.my:after { 
    content:            " text that is supposed to come after the box";
    background-color:   white;
    position:           absolute;
    margin-left:        10px;
}
like image 172
Joel Avatar answered Oct 04 '22 01:10

Joel


Just use position: absolute in the ::after {} pseudo-element's css:

span.my:after {
    content: " text that is supposed to come after the box";
    position: absolute;
    left: 100%;
}​

Remember, of course, to use position: relative; (or any other position value) on the parent span.my element.

JS Fiddle demo.

Also remember that as the ::after (and the ::before) pseudo-element inherits from the span to which it's 'attached' that it'll inherit the width, so that may need to be explicitly overridden in the CSS for that/those elements.

like image 36
David Thomas Avatar answered Oct 04 '22 02:10

David Thomas