1

I've been searching about this for a while with no luck, I'm just curious if this is possible.

Is it possible to include CSS rules directly in a php file with no HTML code?

img {
  background: 0px 0px / 100% 100% no-repeat scroll rgb(12, 12, 12);
  height: 100%;
  width: 100%;
  position: fixed;
}

I just have this piece of code and I don't think would be necessary to create a .css file to include just this small piece of code, otherwise I will create the css file.

3
  • Unless is a one page site, i recommend to use a external file, but answering your question, you can either use echo, or close php tag (?>) write it inside style tag and open again (<?php) or put it on the style attribute inside the tag (style="blabla") Commented Jun 1, 2015 at 16:44
  • Please search for things like this before you post: stackoverflow.com/questions/3483213/… And: stackoverflow.com/questions/25336229/adding-css-to-php Commented Jun 1, 2015 at 17:01
  • @MatthewC Like I said in my post I didn't find it with luck. The ones you are pointing me to say that I can include a CSS file which is what I don't want to do. Commented Jun 1, 2015 at 17:04

3 Answers 3

1

You can put it into your PHP file using <style> tag:

<style>
    img {
        background: 0px 0px / 100% 100% no-repeat scroll rgb(12, 12, 12);
        height: 100%;
        width: 100%;
        position: fixed;
    }
</style>

Or directly to image into style attribute.

<img style="position: fixed; width: ...">
Sign up to request clarification or add additional context in comments.

1 Comment

It doesn't matter. <?php PHP here ?> <style> img {...} </style> <?php other PHP ?>. Or, of course, you can echo it, like echo '<style> img {...} </style>';. Many ways how to achieve that.
0

Yes, this is just an "inline CSS" policy

Comments

0

PHP runs on the server and css/html/javascript all run on a web browser, so you can't have the css in between and I'm afraid you'll need a little html, but just a little

it should look like this:

<?php <php code> ?>
<style>
    img {
        background: 0px 0px / 100% 100% no-repeat scroll rgb(12, 12, 12);
        height: 100%;
        width: 100%;
        position: fixed;
    }
</style>
<?php <more php code if you like> ?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.