Linking Photos Correctly

Learn how to add photos to your GitHub repository and link them in your HTML so they show up on your website.

0 of 12 steps done
All 12 steps done. Your photos are linked correctly!

What you'll have at the end

All your photos will be in one folder called img. Your HTML will point to each photo, and each photo will be a button that opens another page. Check off each step when you finish it. This page remembers your checks.

my-repo/
├── index.html
├── golf.html
├── styles.css
└── img/
    ├── home.png
    └── golf.png
  1. 1

    Save each photo as a .png or .jpg file. If the photos will sit next to each other, like in a menu, make them all the same size so they line up.

    Keep files small. Big photos from a phone make your page slow.
  2. 2

    Rename your files before you upload them. Capital letters matter online: Home.png and home.png are not the same file.

    Good names
    • home.png
    • golf-course.jpg
    • tennis_2.png
    Bad names
    • Home.png.png (ending is there twice)
    • My Photo.PNG (space and capitals)
    • golf(1).jpg (symbols)

    The rules: all lowercase, no spaces (use - or _ instead), and only one ending like .png.

    See .png.png? Your computer was hiding the ending, so you typed it a second time. Turn on file endings so you can see them. On Windows: View → File name extensions. On a Mac: Finder → Settings → Advanced → Show all filename extensions.
  3. 3

    Go to your repository on GitHub. At the top left of the file list, check that it says main. That's the version your website uses.

  4. 4

    If you already have an img folder, click it. If you don't, make one:

    • Click Add file → Create new file.
    • Type img/ in the name box. The slash makes it a folder.
    • Type .gitkeep as the file name and click Commit changes.
    GitHub won't save an empty folder, so the .gitkeep file is just there to hold it.
  5. 5

    Make sure you're inside img. The top of the page should say your-repo / img. Then click Add file → Upload files, drag your photos in, and click Commit changes.

    Common mistake: uploading from the main page of the repo. Then the photos end up outside the img folder and your links can't find them.
  6. 6

    Open the img folder and read each name carefully. Your HTML must use the exact same name, with the same capital letters and the same ending (.png, .jpg or .jpeg).

    If a name is wrong, open the file, click the pencil, fix the name, and commit.

  7. 7

    Open your HTML file on GitHub and click the pencil to edit it. Type this where you want the photo:

    <img src="img/golf.png" alt="Golf">
    • src tells the page where the photo is: the folder, a slash, then the file name.
    • alt is a short description. It shows up if the photo doesn't load, and it helps people who use screen readers.
    Do this
    • img/golf.png
    Don't do this
    • C:\Users\me\Desktop\golf.png
    • /Users/me/Pictures/golf.png
    A path to a file on your own computer only works for you. Nobody else can see your computer, so the photo will be broken online.
  8. 8

    To make the photo clickable, put it inside an <a> tag. href is the page the click opens:

    <a href="golf.html">
        <img src="img/golf.png" alt="Golf">
    </a>

    For a menu, do this once for each page and put them all inside <nav>:

    <nav>
        <a href="index.html"><img src="img/home.png" alt="Home"></a>
        <a href="golf.html"><img src="img/golf.png" alt="Golf"></a>
    </nav>
    Linking to a different website? Use the full address, starting with https://, like <a href="https://example.com">.
  9. 9

    Before you save, look at each src and href and compare it to the file names from step 6:

    • Capital letters match
    • Ending matches (.png is not .PNG or .jpg)
    • Folder name is there (img/)
    • Every quote mark is closed, and </a> is there
  10. 10

    Click Commit changes. Write a short note about what you did, like Add menu photos. Make sure Commit directly to the main branch is picked, then click commit.

    Using the same menu on other pages? Do steps 7 to 10 on each page.

  11. 11

    Open your website. It's usually https://your-username.github.io/your-repo/. Wait about a minute after saving for it to update.

    • Does every photo show up?
    • Does every photo open the right page when you click it?
    • Still seeing the old page? Refresh it. On a computer, press Ctrl+Shift+R (Windows) or Cmd+Shift+R (Mac).
    Photo broken? Right-click it and choose Open image in new tab. The address bar shows where the page looked, so you can spot the typo.
  12. 12

    Delete any photos in the wrong place or with a bad name, like a golf.png.png outside the img folder. Open the file, click … → Delete file, and commit.

    Then check your website one more time to make sure everything still works.

Photo not working? Find the problem

What you seeWhyHow to fix it
A broken photo icon, or just wordsThe name or folder in your code is wrongDo steps 6 and 9 again
Works on your computer but not onlineA capital letter is different, or the path points to your computerUse lowercase names and img/…
Photo shows, but clicking does nothingThe photo isn't inside <a>, or href is missingDo step 8 again
Clicking opens a "404" pageThe page name in href is spelled wrongMake it match the page's file name
Nothing changed at allNot saved to main, or the old page is still loadedCheck you're on main, wait a minute, refresh