douduan3203
2016-04-06 00:43
浏览 185
已采纳

php如何包含访问css文件

I recently asked a question about my php includes and received the answer. Now that the include accesses the correct file, my html/css/javascript web pages show some hope. The only issue is that the php includes of the pages have this look:

PHP include page (not accessing css)

Instead of this:

What the page should look like (this page is not using php includes)

Is there a way for the php includes to access the css files? My current code for one page that contains the includes is:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css" type="text/css" />
<script type="text/javascript" src="main.js"></script>
<title> Water Polo, The Best Sport</title>
</head>
<body>
<div class="wrapper">
<?php
include'../includes/header.php';
?>
<?php
include'../includes/navbar.php';
?>
<div class= "content">
</div>
<?php
include'../includes/footer.php';
?>
</div>
</body>
</html>

Header.php

<?php echo'<div class ="header">
<h1>The Best Sport</h1>
<h1 class="sitetitle">AllWaterPolo</h1>
<img src ="img/51wmckj8p1l__sx300__1.png" class="wpball" alt="Water Polo Ball" />
<h2 class="homeScreenLink"> <a href ="index.html">Water Polo!</a></h2></div>';>

To develop the website I am currently using MAMP and running the code which is in a folder by putting it in htdocs.

I took the header out of the php include and made the css file work with the document, but one thing remains the same. The code that I included in the document via php does not take on the effects of the css document, but the header, which now is out of the include and is written write in the document works. Is there a way to allow the code which has been included via php to access the working css file? If it would facilitate the answering process, I'll post any necessary pictures. Just comment below.

图片转代码服务由CSDN问答提供 功能建议

我最近问了一个关于我的php包含的问题,并收到了答案。 现在include包访问了正确的文件,我的html / css / javascript网页显示出一些希望。 唯一的问题是php包含的页面有这样的外观:

而不是:

< 有没有办法让php包含访问css文件? 我对包含包含的一个页面的当前代码是:

 &lt;!DOCTYPE html&gt; 
&lt; html&gt; 
&lt; head&gt; 
&lt; link rel =“stylesheet  “href =”styles.css“type =”text / css“/&gt; 
&lt; script type =”text / javascript“src =”main.js“&gt;&lt; / script&gt; 
&lt; title&gt;  Water Polo,The Best Sport&lt; / title&gt; 
&lt; / head&gt; 
&lt; body&gt; 
&lt; div class =“wrapper”&gt; 
&lt;?php 
include'../ includes / header.php';  
?&gt; 
&lt;?php 
include'../ includes / navbar.php'; 
?&gt; 
&lt; div class =“content”&gt; 
&lt; / div&gt; 
&lt;?php  
include '../包括/ footer.php'; 
&GT;吗?
&LT; / DIV&GT; 
&LT; / BODY&GT; 
&LT; / HTML&GT; 
   
 
  

Header.php

 &lt;?php echo'&lt; div class =“header”&gt; 
&lt; h1&gt; The Best  Sport&lt; / h1&gt; 
&lt; h1 class =“sitetitle”&gt; AllWaterPolo&lt; / h1&gt; 
&lt; img src =“img / 51wmckj8p1l__sx300__1.png”class =“wpball”alt =“Water Polo Ball”/&gt; \  n&lt; h2 class =“homeScreenLink”&gt;  &lt; a href =“index.html”&gt; Water Polo!&lt; / a&gt;&lt; / h2&gt;&lt; / div&gt;';&gt; 
   
 
 

开发网站我正在使用MAMP并通过将其放入htdocs来运行文件夹中的代码。

我从php include中取出了标题并制作了css文件 使用该文档,但有一点是不变的。 我通过php包含在文档中的代码没有承担css文档的效果,但是标题,现在已经不在include中了,并且写在文档中写入了。 有没有办法允许通过php包含的代码访问工作的css文件? 如果它有助于回答过程,我会发布任何必要的图片。 请在下面评论。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongwen3410 2016-04-06 00:55
    已采纳

    You have correctly identified the problem, that the HTML cannot find the CSS. That is directly because of this tag:

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

    Specifically, this part of the tag:

    href="styles.css"
    

    You have told your code to look for the styles.css file in the same directory as the index.php file. Is that correct?

    Usually, the web site structure looks like this:

    public_html
        - css
        - js
        - includes
        - img
    

    Your website seems to be structured like this:

    includes
    public_html
    

    Imagine yourself inside the index.php file. All files that are INCLUDEd become part of index.php, as if they were there originally. So, you are expecting to find the CSS file here (as per your <link rel="stylesheet" tag):

    public_html/style.css
    

    If they are really inside a CSS folder, then perhaps this will fix it:

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

    Update:

    No need to echo out your HTML in PHP, you can literally just do this:

    header.php

    <div class ="header">
        <h1>The Best Sport</h1>
        <h1 class="sitetitle">AllWaterPolo</h1>
        <img src ="img/51wmckj8p1l__sx300__1.png" class="wpball" alt="Water Polo Ball" />
        <h2 class="homeScreenLink"> <a href ="index.html">Water Polo!</a></h2>
    </div>
    

    If the other include files are similar, then it appears your rendered file will all be HTML. Therefore, the next step is: where is your style sheet? Try this. In the address bar of your browser, type:

    localhost/styles.css
    

    And see if your stylesheet appears. If not, try this:

    localhost/css/styles.css
    

    If the second one works, then change this:

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

    to this:

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

    Note: I might not have your path structure correct. Amend as required.

    打赏 评论
  • dscqrkvr9562034621 2016-04-06 00:56

    Short answer = no.

    You may want to look into using SASS. Besides minifying the css files, you can also set it up so the main (in your case styles.css) pull from several SASS files. For example you could have the following SASS files: main.scss, header.scss, navbar.scss, & footer.scss. If your IDE supports SASS, when you save any one of these files it can automatically compile all four into your styles.css file. Then all you need is to reference that one file and you are set.

    http://thesassway.com/beginner/how-to-structure-a-sass-project

    打赏 评论

相关推荐 更多相似问题