I’m currently building a website for an assignment. I’ve created a header.php and header.css, but the header behaves inconsistently—it looks different on some pages. I’ve already managed to create a footer that works fine across pages. I could also be using it wrong in my other php files…
Could someone please help me figure out what I’m doing wrong? I’ve tried asking ChatGPT, but the suggestions haven’t helped.
Relative paths are off
Your CSS and image paths use /../, which can behave unpredictably depending on the file structure and where the page is located. On some pages, it may resolve correctly, and on others, it may not.Example:
If your file is at root/pages/profile.php, this might try to go to /css/header.css, which works. But if you’re already in the root, /../ becomes /, and things may break or double up.
2. Missing or mismatched <body> / <html> tags
Your header.php includes the opening HTML and <body> tags. But if you’re including header.php inside other pages (not as a full standalone file), then those tags may get duplicated or clash with existing ones in the main file.
3. Not including header properly in other PHP files
If you’re just copying/pasting the header code or using include/require without understanding how full pages should be structured, inconsistencies can happen.
How to Fix It
Fix your paths
Use absolute paths relative to your root, not /../. If your files are in /css/, /img/, etc., then just use:
Split header.php properly
If you’re using header.php across multiple pages, don’t include the full <html>, <head>, and <body> tags in it. Instead, just include the <header> part.Better structure:
<!-- index.php -->
<?php include 'includes/header.php'; ?>
<main>
<!-- Page content here -->
</main>
<?php include 'includes/footer.php'; ?>
Then in header.php, only include the <header> part — not the full page setup: