5

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 other one is called 'css'. I have my -index.php file in the main folder -header.html in my 'main/includes' folder -style.css in my 'main/css' folder

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

My header.html file links the 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 have tried using the base tag, although I'm not sure I'm using it right. <base href="http://localhost/main" /> (this is NOT working)

0

5 Answers 5

3

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" />
Sign up to request clarification or add additional context in comments.

3 Comments

<link href="/css/style.css" type="text/css" rel="stylesheet" /> does not work.
<link href='css/style.css' type='text/css' rel='stylesheet'/> THIS WORKS!! thanks!
I was struggling with this exact same problem and when I read your answer I literally had to facepalm. It's so incredibly obvious, but I was thinking of the header.html as being nested in a folder. Thanks!
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.

Comments

2

$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'/>

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.