简体   繁体   English

如何将CSS样式表包含到* included * PHP文件中?

[英]How to include a CSS stylesheet into an *included* PHP file?

I have an PHP/HTML main page, in which I include different other PHP files (I include them within the body of the main page). 我有一个PHP / HTML主页,其中包含其他不同的PHP文件(我将它们包含在主页正文中)。

What if I want to include a CSS stylesheet into only ONE of the included file? 如果我只想将CSS样式表仅包含在其中一个文件中怎么办?

What if that included file is a PHP-only file? 如果该包含的文件是仅PHP的文件怎么办? (that is, it doesn't have those <head> tags, where the stylesheet reference is supposed to be) (也就是说,它没有那些<head>标签,应该是样式表引用所在的标签)

What happens if I put "head" tags into a PHP file that is included in the body of the main page? 如果将“ head”标签放入主页正文中包含的PHP文件中,该怎么办? I am wondering if putting head tags in the middle of the HTML body tags could cause problems at all, or at least with certain browsers. 我想知道将head标签放在HTML body标签的中间是否会引起问题,或者至少对于某些浏览器会引起问题。

Think of it this way, after all of you includes you still get one html file at the end of it. 以这种方式考虑一下,在包括所有人之后,您仍然会在其末尾得到一个html文件。 If you are including files in the body of your document, it should only contain tags that fit within the body, not html, head, title, body, etc. Your css file should be loaded in the of the main doc. 如果您要在文档的正文中包含文件,则文档中应仅包含适合正文的标签,而不能包含html,head,title,body等。css文件应加载到主文档的中。 Something like this 像这样

    <html>
<head>
<title>Title</title>
<!--
css and js here
-->
</head>
<body>
<!-- php include here ---->
<div>content</div>
</body>
</html>

So after your includes the html should still be valid. 因此,在您包含html之后,该HTML应该仍然有效。 you can check that here http://validator.w3.org/ 您可以在这里检查http://validator.w3.org/

EDIT: You can included as many css files as you want anywhere in the document but the overall structure should still be as above. 编辑:您可以在文档中的任意位置包含任意多个CSS文件,但总体结构仍应如上所述。 But try to consolidate your css files as much as possible. 但是,请尝试尽可能整合您的CSS文件。 Every external file is one more request the browser has to make, and can slow the load time if you have a lot. 浏览器必须再提出一个外部文件请求,如果有很多文件,可能会减慢加载时间。

You can add style tags in the middle of your html, it's bad behaviour, but you can and it'll work. 您可以在html的中间添加样式标签,这是不好的行为,但是您可以并且会起作用。 So, you can only import css you need depending on what you include to your main php. 因此,您只能根据需要将主要的php导入css。 But, you don't have to put head tags for that. 但是,您不必为此放置标题标签。

It is ok if you will put you link your css file in any of your files, also if your file is php, you can put your css before or after the php tag. 可以将css文件链接到任何文件中也可以,如果文件是php,则可以将css放在php标记之前或之后。 But for me its better to include your css file on your main page as long as you have a unique design or style for a unique element or id on the file that you want to design. 但是对我而言,最好将css文件包括在主页上,只要您对要设计的文件具有唯一的设计或样式或元素或id的唯一样式。

<link href="style.css" type="text/css" rel="stylesheet">
<?php
 Content of your php file here
 ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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