i was wondering if i could use this command:
<?php
include_once "example.css"
?>
inside a PHP file so for example:
<!DOCTYPE CSS>
<?php
include_once "example1.css"
?>
<?php
include_once "example2.css"
?>
<?php
include_once "example3.css"
?>
I want to use this because the website I am trying to make is about 8000 lines of CSS long and I want to break it up into multiple CSS files for example about.css, footer.css etc. but the problem is when I try to link 10 different CSS files to one of my pages it glitches out because I have to many linked so can I do the above example with CSS and PHP?
One way to include a bunch of css files would be like so:
<?php
$css = [
'example1.css',
'example2.css',
'example3.css'
]
?>
<!DOCTYPE html>
<html>
<head>
<?php foreach($css as $file): ?>
<link type="text/css" rel="stylesheet" href="<?php echo($file) ?>">
<?php endforeach; ?>
</head>
<body>
<!-- Your content -->
</body>
</html>
BTW, there is no "CSS" doctype - a doctype always denotes some kind of HTML/XML document:
https://developer.mozilla.org/en-US/docs/Glossary/Doctype
Also, you might want to read about the basic structure of an HTML document:
https://developer.mozilla.org/en-US/docs/Learn/HTML/Introduction_to_HTML/Document_and_website_structure
Regarding the foreach syntax used above, see:
https://www.php.net/manual/en/control-structures.alternative-syntax.php
Yes, you can do this.
You can include any type of file, and it will be treated as if it were in inserted into the PHP script outside a <?php ?> section. So if the file doesn't contain any <?php, it will just be output literally.
So lately I've realized I want to make my sites as simple as possible in certain ways, one of them is having as few files as possible. I've looked into linking for CSS-PHP files using query strings, similar to how XenForo does it.
<link rel="stylesheet" href="css.php?css=xenforo,form,public">
Now linking for a file is easy, but how would I add the CSS inside a PHP file without using the style tag? Can I just do something like the block below?
<?php
if(isset($_GET['form'])) {
?>
CSS HERE
<?php
}
?>
Add
<?php
header(Content-type: text/css");
?>
to the top of your php stylesheet.
eg:
<?php
header("Content-type: text/css");
?>
h1 {
color: blue;
}
<html>
<head>
<title></title>
<link rel="stylesheet" href="test.php">
</head>
<body>
<h1>test</h1>
</body>
</html>
I have css stylesheet for all e-mail templates which is placed in the
/webroot/css/email-layout.css
I trying to display css inline in templates using the:
<style type="text/css">
<?php echo $this->Html->css(array('email-layout.css')); ?>
</style>
But after receiving the email is still displayed like link, not inline.
How can i solve it please?
Many thanks for advice.
It's because your style is an internet link (external file)? You need to output this css file as internal file when generating e-mail. For example:
<style>
<?php echo file_get_contents($_SERVER['DOCUMENT_ROOT'].'/css/email-layout.css'); ?>
</style>
Does it help?
<?php echo $this->Html->css(array('email-layout.css')); ?>
will output
<link rel="stylesheet" href="/css/email-layout.css" />
But if you want to get the file content, use file_get_contents function
<style type="text/css">
<?php echo file_get_contents(WWW_ROOT . 'css' . DS . 'email-layout.css'); ?>
</style>
WWW_ROOT : Full path to the webroot.
I have googled a lot but it seems that I am doing something wrong.
I want to do this:
<?php
include 'header.php';
include'CSS/main.css';
...
?>
However, my page prints the CSS code.
Note: I want to use PHP to include the CSS file, and not use
I also do you want to rename my CSS file to a PHP file as some website mentioned.
Any clues?
Many thanks.
You have to surround the CSS with a <style> tag:
<?php include 'header.php'; ?>
<style>
<?php include 'CSS/main.css'; ?>
</style>
...
PHP include works fine with .css ending too. In this way you can even use PHP in your CSS file. That can be really helpful to organize e.g. colors as variables.
You are including the CSS code as text in your PHP page. Why not just link it in the traditional fashion?
<link rel="stylesheet" href="CSS/main.css" type="text/css">
you can use:
<?php
$css = file_get_contents('CSS/main.css');
echo $css;
?>
and assuming that css file doesn't have it already, wrap the above in:
<style type="text/css">
...
</style>
To use "include" to include CSS, you have to tell PHP you're using CSS code. Add this to your header of your CSS file and make it main.php (or styles.css, or whatever):
header("Content-type: text/css; charset: UTF-8");
This might help with some user's connections, but it theoretically (read: I haven't tested it) adds processor overhead to your server and according to Steve Souder, because your computer can download multiple files at once, using include could be slower. If you have your CSS split into a dozen files, maybe it would be faster?
Steve's blog post: http://www.stevesouders.com/blog/2009/04/09/dont-use-import/
Source: http://css-tricks.com/css-variables-with-php/
<?php
define('CSSPATH', 'template/css/'); //define css path
$cssItem = 'style.css'; //css item to display
?>
<html>
<head>
<title>Including css</title>
<link rel="stylesheet" href="<?php echo (CSSPATH . "$cssItem"); ?>" type="text/css">
</head>
<body>
...
...
</body>
</html>
YOUR CSS ITEM IS INCLUDED
This is an older post, however as the info is still relevant today an additional option may help others.
Define a constant for the file path per Stefan's answer. The
definition can be placed at the top of the PHP page itself, or within
an included/required external file such as config.php.
(http://php.net/manual/en/function.include.php)
Echo the constant in PHP tags, then add the filename directly after.
That's it!
Works for other linked files such as JavaScript as well.
<?php
define('CSS_PATH', 'template/css/'); //define CSS path
define('JS_PATH', 'template/js/'); //define JavaScript path
?>
<!-- Doctype should be declared, even in PHP file -->
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="<?php echo CSS_PATH; ?>main.css">
<script type="text/javascript" src="<?php echo JS_PATH; ?>main.js"></script>
</head>
<body>
</body>
</html>
If you want to import a CSS file like that, just give the file itself a .php extension and import it anyway. It will work just fine :)
You can also do the following:
Create a php file in includes folder, name it bootstrap_css.php for example
paste the css code files to file created above
<?php
$minCss=' <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">';
$business = '<link href="bootstrap/css/modern-business.css" rel="stylesheet">';
echo $minCss;
echo $business;
?>
in the html header, include the css files as follows
<?php include_once 'includes/bootstrap_css.php'; ?>
You could do this
<?php include("Includes/styles.inc"); ?>
And then in this include file, have a link to the your css file(s).
I don't know why you would need this but to do this, you could edit your css file:-
<style type="text/css">
body{
...;
...;
}
</style>
You have just added here and saved it as main.php. You can continue with main.css but it is better as .php since it does not remain a css file after you do that edit
Then edit your HTML file like this. NOTE: Make the include statement inside the tag
<html>
<head>
<title>Sample</title>
<?php inculde('css/main.css');>
</head>
<body>
...
...
</body>
</html>
I solved a similar problem by enveloping all css instructions in a php echo and then saving it as a php file (ofcourse starting and ending the file with the php tags), and then included the php file.
This was a necessity as a redirect followed (header ("somefilename.php")) and no html code is allowed before a redirect.
Just put
echo "<link rel='stylesheet' type='text/css' href='CSS/main.css'>";
inside the php code, then your style is incuded. Worked for me, I tried.
This is the format of what I have which works:
<head>
<title>Site Title</title>
<?php include 'header.php'; ?>
</head>
Inside my header.php I have:
<!doctype html>
<html class="no-js" lang="en">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" type="image/png" href="assets/images/icon/favicon.ico">
<link rel="stylesheet" href="assets/css/bootstrap.min.css">
The file name must be something other than a .CSS index. Write the following:
<link rel="stylesheet" href="style.css" type="text/css" />
The best way to do it is:
Step 1:
Rename your main.css to main.php
Step 2: in your main.php add
<style> ... </style>
Step 3: include it as usual
<?php include 'main.php'; ?>
That is how i did it, and it works smoothly..
_trace its directory, I guess
echo css('lib/datatables_rqs/jquery.dataTables.css');
i want to include my css/stylesheet via php so that...
<link rel="stylesheet" href="http://www.mydomain.com/css/style.php">
so that i can than dynamicly change different stylesheets.... how can i do that
As long as you set your MIME type in style.php to CSS, you should be in business. Add this to the very top:
<?php Header ("Content-type: text/css; charset=utf-8");?>
Another option if you're running on an Apache server is to tell it to check .css files for PHP. Add this to your .htaccess file to do this:
AddType application/x-httpd-php .css
Then you could simply include a regular .css file:
<link rel="stylesheet" href="http://www.mydomain.com/css/style.css">
You can add this php code in your html head section but file should be .php.
For example: index.php
<html>
<head>
<?php
$cssFile = "style.css";
echo "<link rel='stylesheet' href='" . $cssFile . "'>";
?>
</head>
<body>
...
...
</body>
</html>
You can store any css file path in $cssFile variable using different conditions.
In style.php:
echo file_get_contents('style.css');
This will just output the contents of style.css.
Another variation of dynamically changing the page style:
<link rel="stylesheet" href="css/<?php echo $user['style']; ?>.css">
Add multiple CSS files dynamicly
The answers seem to be different that they question...
If you want to add all CSS files in the CSS map and not have to worry about changing the code whenever a css file name changes or another one is added, use:
<?php
foreach(glob("CSS/*.css") as $css_file)
{
echo '<link rel="stylesheet" href="'.$css_file.'" type="text/css" medial="all" />';
}
?>
why don't you do it the other way around and just include a different css file in your php?
print "<link rel='stylesheet' href='$path_to_css_file'>";
You can directly include CSS into a HTML file:
<style type="text/css">
<?php include 'stylesheet.php'; ?>
</style>