php - 在php中,如何使用css样式

我使用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>



它们用于 table, tableheader, tabledate。


<?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>";


<?>



时间:

可以使用服务器端语言来生成样式表。


<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>



在html页面中:


<html>


<head>


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



 <!-- Link your php/css file -->


 <link rel="stylesheet" href="style.php" media="screen">


<head>



然后,在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;


}



只需将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>



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

尝试将php放入html文档:

注意:你的文件不能保存为index.html,必须被保存为index.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)



css :hover kinda类似于js onmouseover


row1 {


// ur css


}


row1:hover {


color: red;


}


row1:hover #a, .b, .c:nth-child[3] {


border: 1px solid red;


}



也可以将它嵌入到php。比如,


<?php


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


?>



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

成为

<style></style>

...