Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums CSS Link with "#" throws to top of page

  • This topic is empty.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #199040
    Romchick
    Participant

    Having this issue often. Sometimes not. Can’t understand why it happens. Here is the code: http://codepen.io/NeedHate/pen/PwVMMb
    If you click on “Click me” it goes to top. How to prevent it?

    #199041
    Paulie_D
    Member

    You should (almost) never have href=”#”. It is a link to an undefined anchor (which will be the top of the page). People who use it normally do so because they want to dangle JavaScript off it.

    If you don’t want a link to actually go somewhere there’s not much point in using a link.

    Stack Overflow reference

    #199042
    Romchick
    Participant

    Well, I’m using :target to open block:
    href=”#gotoblock”- link opens that target with id=gotoblock. Sometimes it works fine. And I don’t get it why. 8(

    #199045
    Romchick
    Participant

    I guess it’s because of html/body “height: 100%;”.

    #199047
    Senff
    Participant

    A link should point somewhere, and it should go to another location when you click on it. That’s simply by design.

    What you want, is something to happen (not somewhere to go), so perhaps it would make more sense from a semantic point of view to use <button> instead.

    #199048
    Paulie_D
    Member
    #199049
    Romchick
    Participant

    Yeah, I understand that. But its some kind of css trick, because of “:target”. The link opens something that was invisible. I managed that already. If you click “Click me” it will open sub menus, like a checkbox. That is the goal. But when you put body/html height to 100% it throws :target to the top.

    #199056
    Senff
    Participant

    Well yea, but that’s kinda normal behavior. Links pointing to “#” will try to go to a point where an (empty) ID is defined, unless that behavior is overriden.

    Don’t know what to tell you to be honest. If you use tricks that mess with a link’s normal behavior, you can expect abnormal/surprising results.

Viewing 8 posts - 1 through 8 (of 8 total)
  • The forum ‘CSS’ is closed to new topics and replies.