I've made a sample code as below code to dynamically create pages using php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<style type="text/css">
div {
width: 200px;
height: 200px;
margin: 0 auto;
background-color: black;
color: white;
}
</style>
</head>
<body>
<?php
echo '<div>' . '<p>' . 'generated by php' . '</div>';
?>
</body>
</html>
Then I decided to use an external css file and refer to it by using the LINK tag in the head tag And no styles were applied to the DIV generated. What I did at last was putting the LINK tag anywhere after the PHP code and voila... it worked perfectly. Is there any restrictions about where to put a link tag or not?
The link tag can actually be placed any where on your file, either in the <head>
section of your HTML or in the PHP part. This is because the actual formatting using the rules specified in your css file are performed by the browser — the server (and hence PHP) code has nothing to do with how css files are applied. I would check the HTML output generated by your PHP code and the initial output when the <link>
was in the <head>
section (assuming it was correctly placed in the <head>
of the HTML and compare the 2 (from the client browser's perspective, that is!)