Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use an anchor in an href to scroll to a location with classname and no id?

i'm using Javascript and I need an anchor to a classname cause we don't have id near the location. Can I use an anchor in an href tag to doing this?

The example is of an anchor with id's use:

<div id='anchorID'>
<a href='mysite.com#anchorID>

But can I do this if I have only classname? With anchor, not scrollInToView or something else:

<div class='myclass'>

Thank you!

like image 781
Spicchio Avatar asked Dec 22 '25 15:12

Spicchio


1 Answers

Is it necessary for the URL to display this anchor? If not read about scrollIntoView.

If you need the url alone to accomplish the navigation, then id or name attributes (on the element itself) are your only options.

BTW, a nice visual effect for smoothly scrolling to a functioning anchor can be accomplished with a CSS property called scroll-behavior:

html
{
        scroll-behavior: smooth;
}

This has nothing to do with your issue, but I thought you might like it.

like image 140
Lonnie Best Avatar answered Dec 24 '25 08:12

Lonnie Best



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!