简体   繁体   English

PHP包含HTML CSS

[英]php include with html css

I have a php file that i include in other php files. 我有一个包含在其他php文件中的php文件。 This php file is the menu. 这个php文件是菜单。 It links to my stylesheet with <link href="styles/main.css" rel="stylesheet/index" type="text/css" /> . 它通过<link href="styles/main.css" rel="stylesheet/index" type="text/css" />链接到我的样式表。 But this only works if the php file that includes it is correct according to the path. 但这仅在包含该文件的php文件根据路径正确时有效。 How can I use absolute paths? 如何使用绝对路径?

Say if I include the menu.php from another folder how can you automatically update the path to the css file? 说如果我从另一个文件夹包含menu.php,如何自动更新css文件的路径?

只需使用绝对路径, href="/style/main.css"

An absolute URI is like this: 绝对URI如下所示:

href="http://example.com/absolute/path/to/file.css"

an URI relative to the current directory is like this: 相对于当前目录的URI如下所示:

href="relative/url/to/file.css"

an URI relative to your site's root (http://example.com/) starts with a /: 相对于您网站根目录(http://example.com/)的URI以/开头:

href="/relative/path/from/yoursite/to/file.css"

Inside your css file all URLS are relative to it's own position so it's behaviour doesn't change when the path of the file which includes it changes. 在css文件中,所有URL都是相对于其自身位置的,因此当包含它的文件路径更改时,其行为不会更改。
In example if your menu.css file is located into 例如,如果您的menu.css文件位于

http://example.com/styles/menu.css http://example.com/styles/menu.css

just use 只是使用

<link href="/styles/menu.css" rel="stylesheet/index" type="text/css" />

And browsers will always look for menu.css in http://yoursite.com /styles/menu.css 浏览器将始终在http://yoursite.com /styles/menu.css中查找menu.css。

<link href="/styles/main.css" rel="stylesheet/index" type="text/css" />

它表示从网站根目录开始的styles/main.css

Use the full path relative to your document root instead of the relative path. 使用相对于文档根目录的完整路径,而不是相对路径。 Eg 例如

<link href="/styles/main.css" rel="stylesheet/index" type="text/css" />

as you suggested, just you absolute paths. 如您所建议,只有绝对路径。 So when you link to your stylesheet, just use the the full path like http://www.mysite.com/css/myCssFile.css 因此,当您链接到样式表时,只需使用完整路径,例如http://www.mysite.com/css/myCssFile.css

顺便说始终确保你把一个基础标签上的HTML,这样的:

<base href="http://www.mysite.com/" />

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

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