Search code examples
htmlcssclasstagshref

How to link "class" name in href of same html file?


I want to link the project class into the nav item. How can I link the class name into the href?

code of nav item

<li class="nav-item">
     <a class="nav-link" href="#">Projects</a>
</li>

code of projects

<div class="projects">
      <h4>--Projects--</h4>
       .....
</div>

both of the codes are in the same HTML file.


Solution

  • In order to obtain this behavior, you're going to have to use IDs instead of classes. IDs can only be used once per page. Now from what I understand you want the nav item to jump to a particular part of the page. In this example, you would give your <a href="#projects" to where you want your page to jump. So in this example, once you click the nav item "Projects" you will jump to the div id="projects" portion of the page.

    <li class="nav-item">
         <a href="#projects" class="nav-link">Projects</a>
    </li>
    
    I want to link the project class into the nav item. How can I link the class name into the href?
    
    code of nav item
    <li class="nav-item">
         <a class="nav-link" href="#">Projects</a>
    </li>
    code of projects
    <div id="projects">
          <h4>--Projects--</h4>
           .....
    </div>