Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I change the height of an image in CSS :before/:after pseudo-elements?

Suppose I want to decorate links to certain file types using an image. I could declare my links as

<a href='foo.pdf' class='pdflink'>A File!</a> 

then have CSS like

.pdflink:after { content: url('/images/pdf.png') } 

Now, this works great, except if pdf.png isn't the right size for my link text.

I'd like to be able to tell the browser to scale the :after image, but I can't for the life of me find the right syntax. Or is this like background images, where resizing just isn't possible?

ETA: I'm leaning towards either a) resizing the source image to be the "right" size, server-side and/or b) changing the markup to simply supply an IMG tag inline. I was trying to avoid both those things but they sound like they'll be more compatible than trying to do stuff purely with CSS. The answer to my original question seems to be "you can sort of do it, sometimes".

like image 765
Coderer Avatar asked Jan 23 '12 20:01

Coderer


People also ask

How do you change the size of an image without distorting it in CSS?

Try removing width="100%", or set the width manually based on the aspect ratio.

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.

What is :: before and :: after in CSS?

The ::before selector inserts something before the content of each selected element(s). Use the content property to specify the content to insert. Use the ::after selector to insert something after the content.


1 Answers

Adjusting the background-size is permitted. You still need to specify width and height of the block, however.

.pdflink:after {     background-image: url('/images/pdf.png');     background-size: 10px 20px;     display: inline-block;     width: 10px;      height: 20px;     content:""; } 

See the full Compatibility Table at the MDN.

like image 146
Ansel Santosa Avatar answered Sep 30 '22 05:09

Ansel Santosa