dongshandun4363 2014-05-09 10:16
浏览 235
已采纳

在PHP页面中添加CSS代码(不是文件)

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.

  • 写回答

2条回答 默认 最新

  • douzhuang6321 2014-05-09 10:21
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条