Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put title/alt attributes into CSS :after { content: image }?

I’ve got the following CSS to add a PDF icon to any link that links to a PDF:

a.pdf-link:after { padding-left: 2px; content: url(../images/icon-pdf-link.gif);}

Is it possible to put some title and alt attributes on this image? I would like it so that the user is able to hover over the icon and get some text like “This links to a .pdf file.” Which I’ve typically done just by putting title attributes to it, but can’t figure out if I can do that through this method.

like image 859
SventoryMang Avatar asked May 09 '11 22:05

SventoryMang


People also ask

How do I add alt attributes to an image?

How to add alt text to images. Simply add an alt attribute to the <img> tag in the HTML code. If you're using a modern CMS, it should be possible to add alt text without having to dig into the HTML code.

Can you put alt in CSS?

Answer. CSS background images should not have alternative text if the image is truly a background image. Decorative (i.e removing it from the page causes no impact to the page's meaning or function) CSS background images do not need additional markup.

How do you add alt text in CSS?

Summary. For ambient images that are CSS, it is a courtesy to provide alternate text. When doing so, place image in its own empty <span> with an aria-label and role="img. This is also true, in a situation where CSS must be used for information content.

How do I insert alt text into an image?

To add alt text to a picture, shape, chart, or SmartArt graphic, right-click on the object and choose Format Picture. In the Format Picture panel, choose the Layout and Properties icon. Then choose Alt Text.


2 Answers

You can this, these days, using CSS3.

According to https://www.w3.org/TR/css-content-3/#alt:

1.2. Alternative Text for Speech

Content intended for visual media sometimes needs alternative text for speech output. The content property thus accepts alternative text to be specified after a slash (/) after the last . If such alternative text is provided, it must be used for speech output instead.

This allows, for example, purely decorative text to be elided in speech output (by providing the empty string as alternative text), and allows authors to provide more readable alternatives to images, icons, or text-encoded symbols. Here the content property is an image, so the alt value is required to provide alternative text.

.new::before {
  content: url(./img/star.png) / "New!";
    /* or a localized attribute from the DOM: attr("data-alt") */
}
like image 174
basiphobe Avatar answered Oct 10 '22 11:10

basiphobe


No, content only accepts raw text and image data, not HTML.

You need to use JavaScript to dynamically add tooltips to your existing HTML elements.

As for the icon, you could use a background image and some padding:

a.pdf-link {
    padding-left: 2px;
    padding-right: 20px;
    background: url(../images/icon-pdf-link.gif) right center no-repeat;
}

If you need to specifically have a tooltip only on the icon, though, you need to do everything in JavaScript as the comments say.

like image 44
BoltClock Avatar answered Oct 10 '22 10:10

BoltClock