I have this _head.html file:
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weboldal modulokból</title>
<link rel="stylesheet" href="styles.css">
How can I insert the content of _head.html file into < head > tag of my index.html?
This was my solution:
<head><link rel="import" href="_head.html"></head>
but it was a wrong result.
2
Answers
1. Make sure you have both the index.html and _head.html files in the same directory.
2. In your index.html, add an empty
<head>
section where you want to include the content from _head.html. You can add an id attribute to make it easier to select this element using JavaScript.3. Add a tag just before the closing tag in your index.html to include the JavaScript code to load and insert the content from _head.html.
This JavaScript code uses the fetch API to retrieve the content of _head.html as text and then inserts it into the
<head>
section of your index.html.With this approach, you won’t need to use
<link rel="import">
, and it’s more in line with modern web development practices.HTML Imports are a feature that works natively only in Google Chrome. Support for HTML Imports in Google Chrome has been available since version 31, but it requires manual enabling. For further details, you can refer to the official documentation on MDN Web Docs.
So using the Link tag should not work anymore. Example Below :
Instead Please try to do it in php. Here is a simple example
Make sure your index file is named Index.php, you can check the screenshot.