CSS not working relative path with php 'include'

Folder structure

 /
 |--index.php

 +--includes
 |--header.html

 +--css
 |--style.css

      

I have 2 sub-folders in my main project folder. One is a folder called "includes" and the other is called "css". I have mine - a index.php

file in the main folder - header.html

in my folder 'main/includes'

- style.css

in a folder'main/css'

My index.php includes header.html like this: include_once('includes/header.html');

(it works!)

My header.html file links css like this: <link href='../css/style.css' type='text/css' rel='stylesheet'/>

(this does NOT work!)

I don't understand why the css file is not loaded.
I tried using the base tag, although I'm not sure if I'm using it correctly. <base href="http://localhost/main" />

(this does not work)

+3


source to share


4 answers


You should try using

<link href='css/style.css' type='text/css' rel='stylesheet'/>

      

Since the index.php

folder is css

on the same level.

FROM



<link href='../css/style.css' type='text/css' rel='stylesheet'/>

,

you're asking your server to keep track of a style.css

top-level directory index.php

that doesn't exist.

You can also use /

because it points to the document root on the website.

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

      

+1


source


I don't think you understand how it works include

. Essentially, the code in the referenced file will be copied into the main file and then processed. So if you include it in index.php

, you want to reference the CSS file appropriately.

The following should work:



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

      

You will find that it is easiest to use absolute paths when using HTML, so it will still be valid even if you copy it to a file inside a folder other than the root.

+1


source


$siteurl ="http://localhost/project";

(save this variable in config file so you can use it globally)

<link href='../css/style.css' type='text/css' rel='stylesheet'/>

      

will be changed to

<link href='<?php echo $siteurl;?>/css/style.css' type='text/css' rel='stylesheet'/>

      

+1


source


just change from <link href='../css/style.css' type='text/css' rel='stylesheet'/>

to<link href='css/style.css' type='text/css' rel='stylesheet'/>

0


source







All Articles