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!
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

Ivan Tomicic
3,873 PointsStylesheets inside include files?
Hello, is there a way to put all stylesheets and scripts inside one include file and call them on each page?
Seems like regular way of copying style and script links into include file does not work?
2 Answers

Lucas Krause
19,924 PointsI think this might help you:
index.php:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Welcome to my website</title>
<?php include("include.php"); ?>
</head>
<body>
<h1>Welcome to my website</h1>
<p>My website's content.</p>
</body>
</html>
include.php
<link rel="stylesheet" href="normalize.css">
<link rel="stylesheet" href="main.css">
<script src="main.js"></script>
You can even do something like this inside include.php:
<link rel="stylesheet" href="normalize.css">
<?php
$my_stylesheet="main.css";
echo '<link rel="stylesheet" href="'.$my_stylesheet.'">';
?>
<script src="main.js"></script>

Travis Roemer
1,847 PointsCan you show us what you have done so far?
Lorenzo Pieri
19,772 PointsLorenzo Pieri
19,772 PointsThe only question is: is it really worth it? You would be doubling the HTTP requests for a file that is already included by include\require command!
include('inc/header.php') and meanwhile the header.php has an include that goes like include('inc/resources.php')
Still a great way to stay clean, of course, but putting everything in the header include file is "faster" and still as clean as it can be.
My say! :)
Lucas Krause
19,924 PointsLucas Krause
19,924 PointsWhere the heck comes
header.php
into play? :D I can't see the relation to my post (?).Lorenzo Pieri
19,772 PointsLorenzo Pieri
19,772 PointsSo you are not going to create an include('inc/header.php') once you are done with the basic markup? Mine was more of a future consideration and not only relevant to your post. I commented under your post because it was a consideration RELATED to a possible and futile doubled HTTP REQUEST that would probably happen since he\she will then include a file by the name of header.php, no matter your post.
Lucas Krause
19,924 PointsLucas Krause
19,924 PointsOkay. Now I can see the relation. I didn't look far enough. Sorry for my
short-sightedness.
Anyway, your objection isn't fully correct since there would be only 1 HTTP request, the one requesting your index.php (for example).
The index.php
include()
s header.php which in turninclude()
s resources.php. But none of theinclude()
calls invoke a HTTP request unless you are usinginclude()
with a "http://" URL wrapper. They just read and include a file from the server's local filesystem which performs really fast in these days.Hope I get it right (?).