skip to Main Content

How can I hyperlink a specific word in multiple pages of single website at once.

Detail
For example I have a website named word.com and there are 100 pages on this website. Now I have the word Dog 100 times on these 100 pages. And I want to hyperlink the word Dog with a detailed Wikipedia page.

How can I do it in HTML and CSS?

I don’t know much about coding.

2

Answers


  1. To add a hyperlink specific word "DOG" you will need to have 100 different files if you have 100 pages of your website as it is not possible to do it in a single file.

    HTML

    <a href="image link">Dog</a>
    

    CSS

    a {
        color: blue; 
    }
    
    a:hover {
        color: red; 
    }
    

    You will need to repeat this for all pages.

    Login or Signup to reply.
  2. You can’t do it with only HTML and CSS. You need to write a script which reads the text containing the word "dog" and then puts an anchor <a> tag around it with the correct href. I recommend using JavaScript for this, since it is relatively easy to read from HTML elements with JavaScript.

    The following code turns every occurence of dog into a link, even if its in another word.

    const dogContainer = document.getElementById("here-be-dogs")
    dogContainer.innerHTML = dogContainer.innerHTML.replaceAll(/Dog/gi, "<a href="test.com">$&</a>")
    <div id="here-be-dogs">
      <p>The dog says bork!</p>
      <p>Hello, doggy!</p>
      <p>Dog dog dog dog</p>
      <p>This big dog eats hotdogs</p>
      <p>The quick brown fox jumps over the lazy dog.</p>
    </div>

    The regex /Dog/gi matches every occurence of "dog", case-insensitively.

    The $& inside the replacement string is the matched text. I use this to preserve the casing.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search