A shared link can appear as a card with a title, description, and image. Open Graph tags in the HTML give sharing services those details and identify the page they describe.
The tags belong in the document head so they reach a crawler in the HTML response. When several pages use the same template, each page needs values that describe its own content.
A local web server is enough to check the tags and open the image. For a live preview, the sharing service needs to fetch both the public page and the image without signing in.
Steps to add Open Graph tags in HTML:
- Open the HTML file your web server serves in your text editor.
The sample uses garden.html and an existing garden.jpg in the same directory on a local web server.
- Add the Open Graph prefix to the opening HTML tag.
<html lang="en" prefix="og: https://ogp.me/ns#">The lang="en" declaration is specific to the sample. If the opening tag already has a prefix attribute, include og: https://ogp.me/ns# in that attribute's value once.
- Insert the Open Graph tags before </head>, using your own page and image details in place of the sample values.
<meta property="og:title" content="Garden Notes"> <meta property="og:type" content="website"> <meta property="og:url" content="http://localhost:8000/garden.html"> <meta property="og:description" content="Ideas for a small garden."> <meta property="og:image" content="http://localhost:8000/garden.jpg"> <meta property="og:image:alt" content="Green leaves on a cream background">
The Open Graph protocol requires og:title, og:type, og:url, and og:image. The website type fits a general web page. The description summarizes the page, and the image alt text describes the picture. For a public page, og:url is the full canonical HTTPS address you want people to share, and og:image is the full HTTPS address of the preview image. If a property already has a tag, update its value to avoid conflicting entries.
- Save the HTML file.
- Open the edited page at its web server URL in Chrome.
The local sample is at http://localhost:8000/garden.html. If the page is already open, reload it to pick up the saved HTML.
- Open the loaded page's source with Ctrl+U in Chrome.
On macOS the shortcut is Command+Option+U.
- Verify that the Open Graph tags in the served <head> contain the values you saved.
Chrome opens a view-source: tab. The sample shows Garden Notes, website, both full URLs, the page description, and the image alt text.
- Open the exact address in the og:image tag in Chrome.
The sample image address is http://localhost:8000/garden.jpg.
- Verify that Chrome displays the intended preview image at that address.
The sample shows green leaves on a cream background. A missing file, sign-in page, or different image means the declared address needs fixing.