简体   繁体   中英

CSS doesn't apply to an included PHP file

I want to separate my code to make it clearer. To do so, instead of having one big php file I spare them and keep one "main" file where I include the others when I need. The problem is that CSS only apply to the "main" file, but not to included files.

I'm pretty sure this is not a path issue, for I'm working with every files in the same folder (I know it's not the best way to work, but so far I only have few files so it's not a big deal). I have double checked eventual writing mistakes.

Here is my "main" php file :

<!DOCTYPE html>
<html>
<head>
    <title>A very nice title</title>
    <link rel="stylesheet" type="text/css"   href="style1.css">
    <meta charset="utf-8" />
</head> 

<body>
    <?php include("file2.php"); ?>
</body>

and here is my "file2.php" file :

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css"   href="style1.css">
    <meta charset="utf-8" />
</head> 

<div class="beer">
    <img src="simple_beer.png" alt="beer picture"/>
    <div class="description>
        <p> a simple text </p>
        <form id="add" action="[nothing interesting]">
            <input type="number>
            <input type="submit" value="add to card">
        </form>
    </div>
</div>

So i'm using the same CSS files for both php files. But it's not working on the file2.php when I have a look at it (I'm working on a local host). I've tried to delete cache from my browser (firefox). If you're wondering why I'm working on php files instead of HTML, it's simply because I intend to add php code later.

I have been looking for a solution for almost two hours, nothing seems to work. Thanks in advance !

Include everything header-related inside main.php file, remove head section from file2.php and then include file2.php in the body section of main.php. When you include that file, it will get everything that is above. That means whatever you have in main.php before include() function, will be accessible in called file (thanks to that you can link everything once and have it available in all called files).

Include() brings full code structure from the called file which you do not need (talking about head tag) if main file will hold everything that you need inside tag.

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