php 如何在php中使用css样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13201451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:57:53  来源:igfitidea点击:

How to use css style in php

phpcssecho

提问by Li'

Im using php to display data from mysql. Here are my css statements:

我使用 php 显示来自 mysql 的数据。这是我的 css 语句:

<style type=”text/css”>
table {
    margin: 8px;
}

th {
    font-family: Arial, Helvetica, sans-serif;
    font-size: .7em;
    background: #666;
    color: #FFF;
    padding: 2px 6px;
    border-collapse: separate;
    border: 1px solid #000;
}

td {
    font-family: Arial, Helvetica, sans-serif;
    font-size: .7em;
    border: 1px solid #DDD;
}
</style>

They are used for displaying table, tableheader, tabledate. Im new to php css, so im just wondering how to use the above css style in php displaying codes:

它们用于显示表格、表格标题、表格日期。我是 php css 的新手,所以我只是想知道如何在 php 显示代码中使用上述 css 样式:

<?php>
echo "<table>";
echo "<tr><th>ID</th><th>hashtag</th></tr>";
while($row = mysql_fetch_row($result))
{
    echo "<tr onmouseover=\"hilite(this)\" onmouseout=\"lowlite(this)\"><td>$row[0]</td>                <td>$row[1]</td></tr>\n";
}
echo "</table>";
<?>

采纳答案by Shahrokhian

Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation semantics (the look and formatting) of a document written in a markup language. more info : http://en.wikipedia.org/wiki/Cascading_Style_SheetsCSS is not a programming language, and does not have the tools that come with a server side language like PHP. However, we can use Server-side languages to generate style sheets.

级联样式表 (CSS) 是一种样式表语言,用于描述以标记语言编写的文档的表示语义(外观和格式)。更多信息:http: //en.wikipedia.org/wiki/Cascading_Style_SheetsCSS 不是一种编程语言,并且没有像 PHP 这样的服务器端语言附带的工具。但是,我们可以使用服务器端语言来生成样式表。

<html>
<head>
<title>...</title>
<style type="text/css">
table {
margin: 8px;
}

th {
font-family: Arial, Helvetica, sans-serif;
font-size: .7em;
background: #666;
color: #FFF;
padding: 2px 6px;
border-collapse: separate;
border: 1px solid #000;
}

td {
font-family: Arial, Helvetica, sans-serif;
font-size: .7em;
border: 1px solid #DDD;
}
</style>
</head>
<body>
<?php>
echo "<table>";
echo "<tr><th>ID</th><th>hashtag</th></tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr onmouseover=\"hilite(this)\" onmouseout=\"lowlite(this)\"><td>$row[0]</td>                <td>$row[1]</td></tr>\n";
}
echo "</table>";
?>
</body>
</html>

回答by Omar

I guess you have your css code in a database & you want to render a php file as a CSS. If that is the case...

我猜你在数据库中有你的 css 代码,并且你想将一个 php 文件呈现为 CSS。如果是这样的话……

In your html page:

在您的html 页面中

<html>
<head>
   <!- head elements (Meta, title, etc) -->

   <!-- Link your php/css file -->
   <link rel="stylesheet" href="style.php" media="screen">
<head>

Then, within style.phpfile:

然后,在style.php文件中:

<?php
/*** set the content type header ***/
/*** Without this header, it wont work ***/
header("Content-type: text/css");


$font_family = 'Arial, Helvetica, sans-serif';
$font_size = '0.7em';
$border = '1px solid';
?>

table {
margin: 8px;
}

th {
font-family: <?=$font_family?>;
font-size: <?=$font_size?>;
background: #666;
color: #FFF;
padding: 2px 6px;
border-collapse: separate;
border: <?=$border?> #000;
}

td {
font-family: <?=$font_family?>;
font-size: <?=$font_size?>;
border: <?=$border?> #DDD;
}

Have fun!

玩得开心!

回答by rationalboss

Just put the CSS outside the PHP Tag. Here:

只需将 CSS 放在 PHP 标签之外。这里:

<html>
<head>
    <title>Title</title>
    <style type="text/css">
    table {
        margin: 8px;
    }
    </style>
</head>
<body>
    <table>
    <tr><th>ID</th><th>hashtag</th></tr>
    <?php
    while($row = mysql_fetch_row($result))
    {
        echo "<tr onmouseover=\"hilite(this)\" onmouseout=\"lowlite(this)\"><td>$row[0]</td>                <td>$row[1]</td></tr>\n";
    }
    ?>
    </table>
</body>
</html>

Take note that the PHP tags are <?phpand ?>.

请注意,PHP 标记是<?php?>

回答by Mr. Alien

I didn't understood this Im new to php cssbut as you've defined your CSS at element level, already your styles are applied to your PHP code

我不明白这一点,Im new to php css但是当您在元素级别定义 CSS 时,您的样式已经应用于您的 PHP 代码

Your PHP code is to be used with HTML like this

您的 PHP 代码将像这样与 HTML 一起使用

<!DOCTYPE html>
<html>
  <head>
    <style>
    /* Styles Go Here */
    </style>
  </head>
  <body>
  <?php
   echo 'Whatever'; 
  ?>
  </body>
</html>

Also remember, you did not need to echo HTML using php, simply separate them out like this

还请记住,您不需要使用 php 回显 HTML,只需像这样将它们分开

<table>
  <tr>
    <td><?php echo 'Blah'; ?></td>
  </tr>
</table>

回答by user3001093

Try putting your php into an html document:

尝试将您的 php 放入一个 html 文档中:

Note: your file is not saved as index.html but it is saved as index.php or your php wont work!

注意:您的文件没有保存为 index.html 而是保存为 index.php 否则您的 php 将无法工作!

 //dont inline your style

 <link rel="stylesheet" type="text/css" href="mystyle.css"> //<--this is the proper way!

 //save a separate style sheet (i.e. cascading style sheet aka: css)

回答by planetsarecool

css :hoverkinda is like js onmouseover

css:hover有点像 jsonmouseover

row1 {
    // your css
}
row1:hover {
    color: red;
}
row1:hover #a, .b, .c:nth-child[3] {
    border: 1px solid red;
}

not too sure how it works but css applies styles to echo'ed ids

不太确定它是如何工作的,但是 css 将样式应用于 echo'ed ids

回答by Panji

I don't know this is correct format or not. but it can solved my problem with removing type="text/css"when insert css code in html/tpl file with php.

我不知道这是正确的格式与否。但是它可以解决我在使用 php 在 html/tpl 文件中插入 css 代码时删除type="text/css" 的问题。

<style type="text/css"></style>

become

变得

<style></style>

Example enter image description here

例子 在此处输入图片说明

回答by elbrant

Absolute easiest way (with your current code) is to add a require_once("path/to/file")statement to your php code.

绝对最简单的方法(使用您当前的代码)是require_once("path/to/file")在您的 php 代码中添加一条语句。

<?php
require_once("../myCSSfile.css");
echo "<table>";
...

Also, as an aside: the opening <?phptag does not have a >on the end, and the closing ?>php tag does not start with <. Weird, but true.

另外,顺便说<?php一句:开始标签>的末尾没有 a ,结束?>php 标签不以<. 很奇怪,但确实如此。

回答by Isaac

You can also embed it in the php. E.g.

您也可以将其嵌入到 php.ini 文件中。例如

<?php
    echo "<p style='color:blue; border:2px red solid;'>CSS Styling in php</p>";
?>

hope this help for anyone in the future.

希望这对未来的任何人都有帮助。