0

I'm having a problem with included files accessing the style sheet in my header.php file. Screen shot of file structure

I've created a very basic outline, with just a nav element outline, to see if I could get my inclusions working properly. My index.html has two links:

  • one to a page at the root level, test.php
  • a second that is within the includes folder, test2.php.

For whatever reason, my test2.php, although including the header.php element, does not bring in any of the styles. Below is my code from test.php and test2.php, what exactly am I missing?

Test.php

<?php
    include 'includes/header.php';
?>

Test2.php

<?php
    include 'header.php';
?>

Style.css

nav {
    border: 1px solid;
    width: 95%;
    height: 50px;
}

Header.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="style/style.css" />
</head>
<body>
    <nav></nav>
Theodore Steiner
  • 1,553
  • 2
  • 23
  • 34

2 Answers2

2

I think you should use ../ to read it's previous containing directory.

It should be like this:

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

OR You can set the base_url of your website, so it will be more flexible even you uploaded it on a hosting.

 <link rel="stylesheet" href="<?=base_url().'style/style.css';?>" />

You can read more about creating base_url

Hope this helps!

Roshan
  • 786
  • 1
  • 9
  • 20
0

what is the meaning of :

My index.html has two links:

one to a page at the root level, test.php
a second that is within the includes folder, test2.php.

if that means your index.php contain :

include 'test.php';
include 'includes/test2.php';

you must set your header.php stylesheet link base on your index.php position/directory, like this :

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

because your index.php that called header.php through test.php and test2.php.

Raynaldo
  • 21
  • 5
  • by @Roshan 's answer, using base_url() it's good solution to include that header.php from anywhere. – Raynaldo Feb 27 '19 at 03:33