Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

PHP include not working?

Hey,

I can't seem to get my CSS working properly when I use PHP include for my header.

At the moment, my header file (header.php) contains my CSS file, which contains styles for all my pages:

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

and a logo.

In my index.php file I have 3 divs underneath the logo, which have background images and are my main navigation to the other pages on my site.

basically, if I include my header.php file at the top of my index.php file :

<?php include("inc/header.php"); ?>

when I load the site, my background texture and logo are styled correctly, but the rest of the page is blank. Which makes me think that the CSS is only being applied the the elements directly within the header.php file, and not to index.php. Have I just made a mistake somewhere?

oh and, my site directory looks like this:

  • index.php
  • css/style.css
  • inc/header.php
  • img/img_1.png img_2.png img_3.png

and in my CSS all my background images are referenced as:

background-image: url("../img/img_[number].png");

hope this makes sense! and thanks :)

Hi Grace,

When you say "the rest of the page is blank" do you mean that the rest of the page is unstyled or you don't even see the html from the rest of your index file? If you're seeing the content but it's unstyled then you may just have a problem with your css selectors not matching up with your markup.

You may need to post the html from header.php and the top portion of you index file showing the include and a little of the html that comes after.

2 Answers

I had the same problem due to my use of "inc" in my relative file path. Try dropping the root folder in your code and just use the target file name alone.

ie: <?php include ("header.php"); ?>

It is not a 'must' to use root-relative includes and links, but it will make your life easier in many ways.