【发布时间】:2020-12-20 23:12:09
【问题描述】:
我有一个 MySQL 数据库,它使用 PHP 在我的网站上打印实时活动。现在我喜欢设置这些文本输出的样式(颜色、位置、大小),但我不知道怎么做。你有什么想法吗?
<?php
$pdo = new PDO('mysql:host=x.x.x.x.x;dbname=x.x.x.x', 'x.x.x.x', 'x.x.x.x');
$sql = "SELECT * FROM test ORDER BY id DESC LIMIT 1";
foreach ($pdo->query($sql) as $row) {
echo '<span class="temperature">' . $row['temperatur'] . '</span>';
echo '<span class="unit">°C</span>';
}
?>
<style>
.temperature {
color: red;
}
.unit {
color: blue;
margin-top: 2em;
position: absolute;
}
</style>
第二个问题:我喜欢在 img 上加边距,但它不起作用。高度和宽度效果很好,但 margin-top 没有效果。有人有想法吗?
foreach ($pdo->query($sql) as $row)
if ($row['status'] == '1') {
$image = "alarm";}
else {
$image = "noalarm";}
echo '<img src="img/' . $image . '.png" margin-top="3.5em" height="40%" width="35%" title="Fire" alt="Fire" />';
?>
【问题讨论】:
-
用一个类将它们包装在一个跨度中,并像正常一样设置该类的样式 - 例如
echo "<span class=\"temp\">".$row['temperatur']." °C</span>"; -
你在
echo之前用CSS注入HTML和样式,看看w3school,他们已经有例子了。 -
到目前为止您尝试过什么?像其他用例一样编写 CSS 样式有什么问题吗?
-
您将如何在任何静态 HTMNL 文档中设置 文本内容“Foo”的样式...?回答这个问题,也就回答了这个问题。