Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using :after pseudo element after anchor element - browser differences

I have an <a> element, after which I want to show a > sign, using the :after pseudo element.

The <a> element's content is dynamic, so its width changes, and sometimes the content event spans a few rows (since this <a> element is inside a <div> who's width is fixed).

I would like the >'s horizontal position to start at the end of the longest row. That is, when I give it that right:0; rule, it should be at the right most edge of the element (the vertical position doesn't matter right now):

enter image description here

That's the way it behaves in FF, but in Chrome and IE, the > appears at the end of the shortest row:

enter image description here

I'd like to understand what causes the difference between the browsers, but more importantly, I'd like all browsers to behave like FF - placing the :after at the end of the longest row. Is that possible?

I put the above code on dabblet

like image 664
Lea Cohen Avatar asked Oct 29 '13 09:10

Lea Cohen


People also ask

Can you have multiple after pseudo-elements?

You can't add two ::after pseudo-elements to one DOM element. You can however add an ::before additionally. Depending on what you are trying to accomplish, this may work.

When would you use the :: before or :: after pseudo-element in your CSS?

One simple example of using ::before pseudo-elements is to provide quotation marks. Here we use both ::before and ::after to insert quotation characters.

Can I have multiple before pseudo-elements for the same element?

1, an element can only have at most one of any kind of pseudo-element at any time. (This means an element can have both a :before and an :after pseudo-element — it just cannot have more than one of each kind.)

What is the purpose of 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.


1 Answers

That's because your a element is set to display inline by default, and Firefox handles positioning within inline elements slightly differently to Chrome and IE.

To fix this in both Chrome and IE (whilst retaining the look in Firefox), simply give your a element an inline-block display:

a {
    position:relative;
    display:inline-block;
}

Modified Dabblet demo.

like image 182
James Donnelly Avatar answered Sep 28 '22 07:09

James Donnelly