How can I include CSS in php?

后端 未结 7 470
北恋
北恋 2020-12-10 09:01

i want to include my css/stylesheet via php so that...


so th

相关标签:
7条回答
  • 2020-12-10 09:46

    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'>";
    
    0 讨论(0)
  • In style.php:

    echo file_get_contents('style.css');
    

    This will just output the contents of style.css.

    0 讨论(0)
  • 2020-12-10 09:55

    You can directly include CSS into a HTML file:

    <style type="text/css">
    <?php include 'stylesheet.php'; ?>
    </style>
    
    0 讨论(0)
  • 2020-12-10 09:57

    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"  />';
         }
     ?>
    
    0 讨论(0)
  • 2020-12-10 10:02

    Another variation of dynamically changing the page style:

    <link rel="stylesheet" href="css/<?php echo $user['style']; ?>.css">
    
    0 讨论(0)
  • 2020-12-10 10:03

    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.

    0 讨论(0)
提交回复
热议问题