Skip to content Skip to sidebar Skip to footer

CSS Not Working From Relative Path With Php 'includes'

folder structure / |--index.php +--includes |--header.html +--css |--style.css I have 2 subfolders in my main project folder. One is a folder called 'includes' and the ot

Solution 1:

You should try using

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

As index.php and the css folder lie at the same level.

With

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

you are asking your server to look after the style.css in upper level directory of index.php which does not exist.

You can also use / because, it points do the document root of the website.

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

Solution 2:

I don't think you understand how include works. Essentially the code in the referenced file will be copied into the main file, and then processed. So if you're including it into index.php, then you want to reference the CSS file accordingly.

The following should work:

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

You'll find it is the most easy to just use absolute paths when using HTML, that way the above like will still be valid, even if you copy it to a file that is in within a folder besides the root.


Solution 3:

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

(store this variable in config file so that you can use 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'/>

Solution 4:

If you are trying to add CSS in html file using PHP require:

<style><?php require("css/style.css");?></style>

Note: <style> tag is important otherwise it will echo plain text


Post a Comment for "CSS Not Working From Relative Path With Php 'includes'"