0

I can't seem to find the right answer for this. I was wondering how to include a CSS code inside my PHP page.

My PHP page has scripts in it then I use echo to display HTML elements when the page is loaded. 3 of those elements are forms with submit buttons. I was trying to get those submit buttons have the same width and height. I researched and it seems the only solution is adding CSS which is usually inserted inside the tag but I have a PHP page, not HTML. If I try to add and tags the PHP file in file manager view changes into HTML file meaning if I add the tag the server identifies my PHP file as HTML instead so I don't want that additional problem.

I just wanted to have the form buttons have the same width. below is currently what I have, I added some spaces on the value of each submit button to try and extend their width but it's still noticeably not matching.

http://s12.postimg.org/iqp7okq55/Capture2.png

EDIT:

Thanks guys, you helped me greatly. I added this code before any other echo for HTML elements:

echo "<html><head><style>input.panelbutt {width:120px;}</style></head>";

Of course I added a closing HTML tag echo at the end and made sure the submit button has "Class='panelbutt'" attribute.

1
  • Also, this is quite basic concept of working with PHP/HTML/CSS/... I would suggest you take a look at sites like codecademy.com to get more comfortable with the code. Commented May 9, 2014 at 10:32

2 Answers 2

2

There is no reason your PHP script cannot output CSS in the same way as it does HTML.

I personally like using HEREDOC syntax instead of multiple echo statements as I think it looks neater (with less \ characters) and it's easier to embed PHP variables into, but that's up to you...

echo <<<EOF
<style>
/* put your CSS here */
</style>
EOF;

or put it in inline by closing the PHP tags

<?php
// some php here...
?>
<html>
  <head>
     <title>PHP, CSS and HTML mix page</title>
     <!-- more head tags here.... -->
     <style> 
     /* css here */ 
     </style>
  </head>
  <body>
  <?php // more PHP
  ?>
  <!-- html here -->
  </body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Vogomatix, I added the final script which I came up with because of your answer
0

Create your CSS file as a static resource then when generating your HTML to return to the browser simply include the following line in the <HEAD> area:

echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"pathtofile.css\">";

Example css:

button.classABC {
    width: 100px;
}

1 Comment

Pretty sure he said no file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.