How to import/include a CSS file using PHP code and not HTML code?

后端 未结 14 1809
無奈伤痛
無奈伤痛 2020-11-28 11:11

I have googled a lot but it seems that I am doing something wrong.

I want to do this:



        
相关标签:
14条回答
  • 2020-11-28 11:37

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

    0 讨论(0)
  • 2020-11-28 11:47

    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.

    0 讨论(0)
  • 2020-11-28 11:49

    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.

    0 讨论(0)
  • 2020-11-28 11:51
    <?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

    0 讨论(0)
  • 2020-11-28 11:52

    You can also do the following:

    1. Create a php file in includes folder, name it bootstrap_css.php for example
    2. 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;
      ?>
      
    3. in the html header, include the css files as follows

       <?php include_once 'includes/bootstrap_css.php'; ?>
      
    0 讨论(0)
  • 2020-11-28 11:53

    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>
    
    0 讨论(0)
提交回复
热议问题