Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I accomplish this with CSS?

If I've got elements like this:

<a href="something">A</a>
<a href="something_else">B</a>
<a href="something">A</a>
<a href="...">C</a>

I know I can use something like

body
{
    counter-reset:section;
}

a:before
{
    counter-increment:section;
    content:counter(section)". ";
}

to get

1. A
2. B
3. A
4. C

but is there a way to get the following?

1. A
2. B
1. A
3. C

ie. uniquely identify all links on a page by prefixing the text with the same number.

Note: hardcoding specific URLs isn't an option, I'm potentially dealing with hundreds of links and don't know the URLs ahead of time.

I realize this would be easy/possible with javascript, I am only interested in CSS-based solutions or an explanation of why this isn't possible with CSS.

like image 585
Emory Avatar asked Jul 17 '12 08:07

Emory


People also ask

Can you do anything with CSS?

CSS (Cascading Style Sheets) is used to style and layout web pages — for example, to alter the font, color, size, and spacing of your content, split it into multiple columns, or add animations and other decorative features.

Is CSS a valuable skill?

CSS is a rule-based syntax in programming important to web development. Web developers who know CSS are in high demand for businesses. In fact, according to the Bureau of Labor Statistics, web development is a profession expected to grow much faster than the average occupation over the next decade.

What are the benefits of learning CSS?

CSS is important for web designers Cascading Style Sheets are an important way to control how your web pages look. CSS controls the fonts, text, colors, backgrounds, margins, and layout.


2 Answers

Ok, I got what you mean with your question. Just with plain CSS it's not possible (at least not cross-platform..)

If you can use javascript, you have several possibilities.

My preference would be to use a data-attribute to hold the value, for this example I chose data-counter. If you do like this, the CSS becomes trivial:

CSS

a:before
{
   content:attr(data-counter)". ";
}​

And the Javascript would look like this if you have jQuery:

JS with jQuery

var linkcounter = {};
var counter = 0;
$("a").each(function() {
    if (!linkcounter.hasOwnProperty($(this).attr("href"))) {
        counter++;
        linkcounter[$(this).attr("href")] = counter;
    }
    $(this).attr("data-counter", linkcounter[$(this).attr("href")]);
});

​ or like this without jQuery:

vanilla JS

var linkcounter = {};
var counter = 0;
var anchors = document.getElementsByTagName('a');
for(var i = 0; i < anchors.length; i++) {
    if (!linkcounter.hasOwnProperty(anchors[i].getAttribute("href"))) {
        counter++;
        linkcounter[anchors[i].getAttribute("href")] = counter;
    }
    anchors[i].setAttribute("data-counter", linkcounter[anchors[i].getAttribute("href")]);
}

You can view the version without jQUery here: http://jsfiddle.net/ramsesoriginal/CVW7Y/5

And the version with jQuery here: http://jsfiddle.net/ramsesoriginal/CVW7Y/4

Sadly there is no CSS only way to do this (yet). I hope this helps. ​

like image 95
ramsesoriginal Avatar answered Sep 18 '22 12:09

ramsesoriginal


I don't think you can get this behaviour with pure CSS, and you need Javascript. And there are always cases like this:

http://google.com/
http://google.com
google.com
google.com/
www.google.com

You get the point.


In jQuery this is quite trivial, so I'd suggest you use that.

like image 41
orlp Avatar answered Sep 22 '22 12:09

orlp