简体   繁体   中英

How do I include a css file into php in the head section of the page and a js file before the end of body tag?

I want to include a css file into the head section of a php file ? Right now I am including css in this way:

<?php include('./includes.header.html'); ?>
<style><?php include('./test.css'); ?> </style>

What it does is it includes the css in the body section not in the head section. I have no idea to add js file before the end of body tag. Please tell me a way to include the css file in the head section and js file before the end of the body tag. Thanks in advance.

php include should only be used to include PHP files, not css. Edit the header HTML and add the css normally ie

<head>
  <link rel="stylesheet" type="text/css" href="test.css">
</head>

You can programatically add it using PHP anywhere ie

<?php print_r('<link rel="stylesheet" type="text/css" href="test.css">'); ?>

According to default structure of a web page add css and js files like this

HTML

<html>
  <head>
    <link rel="stylesheet" type="text/css" href="test.css">
  </head>
  <body>
    //your other code here
    <script src="test.js">
  </body>
</html>

php include can be used to add php files but for css you should use this default method.

In response to your comment about adding the styles to a specific page header: you could use a conditional with isset and a variable to set section-specific styles to your header like so:

<?php
    if (isset($section_specific_styles))
    {
        echo "<link rel=\"stylesheet\" href=\"css/{$section_specific_styles}.css\">";
    }
?> 

You would then set the variable $section_specific_styles in the relevant page to only add the stylesheet in that page's header (thus, other pages without the variable set would not add the additional stylesheet.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM