{"id":909,"date":"2017-03-20T10:05:53","date_gmt":"2017-03-20T04:35:53","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=909"},"modified":"2017-03-20T10:05:53","modified_gmt":"2017-03-20T04:35:53","slug":"php-code-to-include-css-in-a-php-file","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/php-code-to-include-css-in-a-php-file\/","title":{"rendered":"PHP &#8211; Code to include css in a .php file"},"content":{"rendered":"<p><label class=\"label label-Warning\">PROBLEM<\/label><\/p>\n<p>To include a css file to .php file.<\/p>\n<p>Inside the .php file, We have;<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?php<br\/>\tLoads of code..<br\/>\t?&gt;<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p>We want it like this..<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?php<br\/>\t&lt;link href=&quot;bootstrap\/css\/bootstrap-responsive.css&quot; rel=&quot;stylesheet&quot;&gt;<br\/>\tLoads of code..<br\/>\t?&gt;<\/code><\/pre> <\/div>\n<p>But obviously that doesn&#8217;t work. Is there any way to do this?<\/p>\n<p><strong>There are Four ways to solve this :<\/strong><\/p>\n<p>Solution 1 : echo a snippet.<\/p>\n<p>Solution 1 : include\/require<\/p>\n<p>Solution 2: same as you normally do in a HTML file.<\/p>\n<p>Solution 3:Consider your CSS file as a PHP file.<\/p>\n<p>PHP is a server-side language,CSS is client-side and is used by the browser.<\/p>\n<p><label class=\"label label-info\">SOLUTION 1<\/label><\/p>\n<p><strong>use echo :<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?php<br\/>\techo &#039;&lt;link href=&quot;bootstrap\/css\/bootstrap-responsive.css&quot; rel=&quot;stylesheet&quot;&gt;&#039;;<br\/>\t?&gt;<\/code><\/pre> <\/div>\n<p><label class=\"label label-info\">SOLUTION 2<\/label><\/p>\n<p><strong>Use include\/require :<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;style&gt;&lt;? include_once \u201dPATH\/your_style.css&quot; media =&quot;&quot;  ?&gt;&lt;\/style&gt;<br\/>\/\/<br\/>&lt;style&gt;&lt;? require_once \u201dPATH\/your_style.css&quot; media =&quot;&quot; ?&gt;&lt;\/style&gt;<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><label class=\"label label-info\">SOLUTION 3<\/label><\/p>\n<p><strong>same as you normally do in a HTML file.<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?<br\/>&lt;head&gt;<br\/>&lt;link href=\u201dPATH\/your_style.css\u201d &gt;<br\/>&lt;\/head&gt;<br\/>?&gt;<\/code><\/pre> <\/div>\n<p><label class=\"label label-info\">SOLUTION 4<\/label><\/p>\n<p><strong>Consider your CSS file as a PHP file.<\/strong><\/p>\n<p>( instead of your_style.css, work with your_style.php )<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?<br\/> header(&quot;Content-type: text\/css; charset: UTF-8&quot;);<br\/> <br\/>$style_tipo = &quot;Helvatica, Arial, sans-serif&quot;;<br\/>$style_colour= rgba(255, 255, 255, 30);<br\/>....<br\/> <br\/>a { color: &lt;? echo tyle_colour ?&gt; }<br\/>....<br\/>?&gt;<\/code><\/pre> <\/div>\n<p>Css file linking can be categorized in two ways :<\/p>\n<p>&nbsp;<\/p>\n<p><label class=\"label label-info\">SOLUTION 5<\/label><\/p>\n<p><strong>By Html Code :<\/strong><\/p>\n<p>This is the traditional method for including CSS in your document.<br \/>\nIt can be done by using link tag in head section of the document.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;link rel=&quot;stylesheet&quot; href=&quot;main.css&quot; type=&quot;text\/css&quot;&gt;<\/code><\/pre> <\/div>\n<p><label class=\"label label-info\">SOLUTION 6<\/label><\/p>\n<p><strong>By Php Code :<\/strong><\/p>\n<p>This is by far new and powerful way to import css in a document.<\/p>\n<p>But the only condition is you have to surround the php code with style element.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?php include &#039;header.php&#039;; ?&gt;&lt;style&gt;&lt;?php include &#039;CSS\/main.css&#039;; ?&gt;&lt;\/style&gt;<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p>CSS files aren&#8217;t linked to PHP, they are linked to HTML.<\/p>\n<p><strong>PHP can generate a line of HTML that references a CSS file like this:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;link rel=&quot;stylesheet&quot; type=&quot;text\/css&quot; href=&quot;my-style.css&quot;&gt;<\/code><\/pre> <\/div>\n<p><label class=\"label label-info\">SOLUTION 7<\/label><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?php<br\/><br\/>echo &#039;&lt;head&gt;&#039;;<br\/>    echo &#039; &lt;link rel=&quot;stylesheet&quot; href=&quot;\/styles.css&quot; type=&quot;text\/css&quot;&gt;&#039;;<br\/>echo &#039;&lt;\/head&gt;&#039;;<br\/><br\/>require( &quot;config.php&quot; );<br\/><br\/>$Quote = new CQuote; <br\/><br\/>if( $Quote-&gt;Get( $strQuote ) == 0 )<br\/>\techo $strQuote;<br\/><br\/>?&gt;<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>PROBLEM To include a css file to .php file. Inside the .php file, We have; [ad type=&#8221;banner&#8221;] We want it like this.. But obviously that doesn&#8217;t work. Is there any way to do this? There are Four ways to solve this : Solution 1 : echo a snippet. Solution 1 : include\/require Solution 2: same [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[1731,1732,1724,1720,1719,1726,1725,1722,1717,1723,1718,1721,1729,1730,1727],"class_list":["post-909","post","type-post","status-publish","format-standard","hentry","category-php","tag-css-in-php-tutorial","tag-css-not-working-in-php-file","tag-how-do-i-correctly-link-my-css-file-to-both-my-index-php-and-my-include-php-files","tag-how-do-we-link-css-files-to-a-php-file","tag-how-to-add-css-to-php-file","tag-how-to-include-css-in-php-echo","tag-how-to-include-external-css-file-in-php","tag-how-to-include-style-sheet-as-php-file","tag-including-css-in-php-file","tag-including-external-javascript-and-css-files-in-php","tag-linking-a-css-file-to-a-php-script","tag-php-5-include-and-require","tag-php-css-examples","tag-php-include-css-not-working","tag-php-include-css-path"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/909","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=909"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/909\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=909"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}