Search code examples
phphtmlcssrequiresidebar

php require method wont display css styling


I created a index.php file that's uses <?php require("sidebar.html"); ?> to include a sidebar; the HTML element of the sidebar shows, however the css styling isn't showing. I've search Google and tried different method but it's not showing, any help would be highly appreciated.

The sidebar.html is located in HTML/ folder. And index.php is located in root/ folder

The css styling for the sidebar is being reference within the sidebar.html file

My css file is located in CSS/ folder

new to web development; Trying to make a sidebar that I can call on every page instead of hard-coding it to every page.


Solution

  • When you include an HTML file into a PHP script, path to all the related files (i.e. files that are referenced in the HTML document) must be relative to the PHP script in which you have included the HTML.

    Have a look at the file structure below:

    • Home
      • index.php
    • Includes
      • Assets
        • style.css
        • action.js
      • header.html

    The Assets directory contains CSS and JS files which are included in header.html. Now, if header.html has to be included in index.php that is inside the Home directory, the src/href attributes need to point to the path of css/js files relative to index.php.

    Something like this:

    <link rel="stylesheet" href="../Includes/Assets/style.css" />
    

    Happy coding :)