tags:

views:

26

answers:

3

Hello there
I am using the following code for display the requested content.It is not the main page and here in this time only I need a css.But this CSS is not working.I dont know what is the reason.Please help me on this.

echo '
<html>
<head>      
    <title>My site</title>

<link href="css/main.css"/>
</head>
<body>
<div class="header">
        <div class="headercontent">


<div style="float: right; padding: 5px 15px 0pt 0pt;">Site</div>

<br>

        </div>
    </div>
    '

But the CSS is not working here.

+1  A: 

One thing I can see is that the link is missing the necessary rel="stylesheet". If that's not it, you will need to elaborate on what "not working" means exactly.

Pekka
A: 

what CSS is not working? Are you getting any PHP errors? PHP and CSS are totally unrelated in this context. If your HTML is being outputted then your issue is with your CSS (likely the lack of a rel="stylesheet") not your PHP. Only PHP issue i can see is your echo is missing a closing semi-colon

seengee
+1  A: 

Your <link> needs the properties that make the browser know it's CSS, like this:

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

Unless your server is already transferring it with a "text/css" MIME type, the browser's a bit confused on what it should do with this file, unaware it's a stylesheet.

It's important to remember <link> elements have many other purposes so you need to tell it (specifically with rel) what this one does, since they can also be used for:

  • Shortcut Icons
  • iPhone/iPad Icons
  • Search specifications (e.g. opensearch)
  • Canonical links
  • etc...
Nick Craver
+1 Nice detailed answer. However, if you don't send a `text/css` content type along with the style sheet, Firefox will ignore it even if you specify a `type` attribute.
Pekka
@Pekka - definitely good to know, the last time I tested without a correct MIME type in the headers was by accident and way back in Firefox 2 and it worked then, when did that behavior change?
Nick Craver
@Nick I don't know when it started, I think somewhere around V2. It's still there: http://stackoverflow.com/questions/2883695/firefox-doesnt-show-my-css/ I'm not complaining though, I think it has won me three or four (promised) beers already :)
Pekka