0

I have a css file called main.css in my styles folder on the root of my project:

#header
{
    background-color: aqua;
    height: 120px;
}

In another php file header.php I have something like this:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="/styles/main.css"/>
</head>
<body>
<header id="header"></header>

I also have a footer.php:

</body>
</html>

When combining the in my index.php like this:

<?php
    require('includes/header.php');
?>
<?php
    require('includes/footer.php');
?>

the result is that my css rules don't get applied. How can I fix this?

4
  • 1
    Is the styles folder within the includes folder? If not, if they both have the same parent directory, change the CSS path to ../styles/main.css Commented Mar 21, 2015 at 7:38
  • I did, and the result was the same. It only loads in header.php, not in index.php. Commented Mar 21, 2015 at 7:44
  • How do you access your project in the browser? Is it located in a subfolder like http://localhost/myproject/index.php? Commented Mar 21, 2015 at 9:05
  • Yes, it is exactly like that. Commented Mar 21, 2015 at 9:36

3 Answers 3

1

problem may be in your css path if php file and style folder both are in root use like this href="styles/main.css"

Sign up to request clarification or add additional context in comments.

Comments

1

Link your css file using base

try

href="../styles/main.css"

2 Comments

But the path I use is absolute.
This only works when accessing header.php. When loading index.php, it fails.
1

it seems like your css file path cause this problem.

if your directory structure like this below:

  • includes
  • |__header.php
  • |__footer.php
  • styles
  • |__main.css

replace the css path in the header.php like:

href="../styles/main.css"

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.