r/HTML • • Nov 05 '26

Question Menu code repetition

Should I look for a way to call an external html page with the code for my menu? I feel like adding the code to every page might clutter the code a bit. It’s been a while and I don’t remember wether or not we discussed this in school lol

7 Upvotes

17 comments sorted by

3

u/heesell Nov 05 '26

Perhaps you could expand your question.

In frameworks, we use components for this but I found this code pen that does it in the same kind of way using Javascript: https://codepen.io/grace_2241/pen/XWXxWgr

2

u/Dauntle55Duck Nov 05 '26

I think this may be exactly what I’m looking for thank you!!

1

u/mondayquestions Nov 05 '26

If you are building something simpler and more static, static site generators like Hugo or Astro might be a better choice.

1

u/Dauntle55Duck 28d ago

I’ve looked into these a bit, seems like a great next step for my site!

2

u/glandix Nov 05 '26

DRY: don’t repeat yourself … in other words, reduce duplicated code so when you need to update it, you change it in one place and you’re done.

2

u/Dauntle55Duck 28d ago

I know to do it but I usually struggle with the how. Everyone here has been super helpful with how to do this in html :)))

7

u/azangru Nov 05 '26

Should I look for a way to call an external html page with the code for my menu?

Yes. This means either running a server that generates the full html of a given page out of reusable pieces, or building the full html pages with a static site generator.

3

u/[deleted] Nov 05 '26

Server-side includes have been around since the 90s for just this reason.

2

u/5eeso Nov 05 '26

The problem you’re describing is one of the reasons static site generators exist. You can use something like Eleventy with Nunjucks to create a shared layout that each page can use.

2

u/Haasva Nov 05 '26

Load with JavaScript using templates (very simple code) or use iframe as someone recommended. Two good options.

2

u/ThatBlindSwiftDevGuy Nov 06 '26

Using an inline frame can be an accessibility nightmare, so I wouldn't immediately jump to using that for reusable components like a navigation

2

u/cshaiku 27d ago

PHP is exceptionally good at generating html templates.

1

u/GirlCanGame 24d ago

Agreed, as long as the server running the website has php enabled, you can mix and match any bits of html with simple "include"

2

u/Weekly_Ferret_meal Nov 05 '26

OR if the option of servers or JS isn't there. you can go old school and use iframe.

the index.html contains the main page with the menu that targets the a content iframe to load the pages like home.html, about, contact, etc...

https://www.w3schools.com/html/tryit.asp?filename=tryhtml_iframe_target

That's how I built my first website.

It's old school, but it's still valid.

3

u/ThatBlindSwiftDevGuy Nov 06 '26

An inline frame for this sort of thing can be an absolute nightmare for accessibility, so I wouldn't immediately jump to that

2

u/Weekly_Ferret_meal Nov 06 '26

Right I wasn’t aware of that, 

Personally I’d use a html preprocessor like pug and call it a day, if static html was the only option 

That would allow to create partials to insert easily in every page