woff
woff

Reputation: 107

HTML Link to class

I know in HTML you can link to an element by ID with # in the address bar, but is it possible to do the same with the classname of the element?

Upvotes: 1

Views: 458

Answers (3)

Nima Parsi
Nima Parsi

Reputation: 2110

It's possible if you don't mind using jQuery to get around this. The following code targets the class name and scrolls to it.

$("a").on("click",function() {
    $('html, body').animate({
       // add your class below
       scrollTop: $('.contact').offset().top
       // Scroll Speed
    }, 2000);
});

Upvotes: 2

Charbz
Charbz

Reputation: 536

It's not possible to use class names as anchor tags in the address bar. Theoretically classes are supposed to be re-used , and one element can have several classes, so it's not a unique identifier

Upvotes: 1

BoltClock
BoltClock

Reputation: 723729

No. Fragment identifiers can only point to either an a element with a name of that identifier, or an arbitrary element with an id of that identifier, assuming a conforming document where said element is unique.

A class name can apply to more than one element in a document, so it's not certain how a fragment identifier pointing to a class name should behave if there is more than one such element.

Upvotes: 0

Related Questions