如何在循环中的PHP代码中添加CSS样式?

时间:2017-10-09 04:30:36

标签: php html css

我正在尝试将css样式应用于php循环:

<h3 class='title'></h3>

然而,这似乎不适用于css样式:

.title{
    color : red;
}

我尝试在回显周围添加<?php ?>标记,然后应用css但仍然无效。

   $sql = "SELECT * FROM events LIMIT 10;";
                $result = mysqli_query($conn, $sql);
                if (mysqli_num_rows($result) > 0){
                    while ($row = mysqli_fetch_assoc($result)){
                        echo "<p></p> ";
                        echo "<h3 class='title'>Event name: </h3>";
                        echo " ";
                        echo $row['eventname'];
                        echo "<p></p> ";
                        echo "<h3 class='title'>Event Venue: </h3>";
                        echo " ";
                        echo $row['venue'];
                        echo " <p></p>";
                        echo "<h3 class='title'>Event Date: </h3>";
                        echo " ";
                        echo $row['date'];
                        echo " <p></p>";
                        echo "<b>Event Description: </b>";
                        echo " ";
                        echo $row['eventdescription'];
                }
            }


        ?>

3 个答案:

答案 0 :(得分:0)

我们需要使用冒号(:)而不是等号来分配CSS属性值:

.title {
    color: red;
}

答案 1 :(得分:0)

<?php echo "<style>h3.title{ color : red;}</style>"; ?>

使用此代码

答案 2 :(得分:0)

<?php $sql = "SELECT * FROM events LIMIT 10;";
                $result = mysqli_query($conn, $sql);
                if (mysqli_num_rows($result) > 0){
                    while ($row = mysqli_fetch_assoc($result)){
                        echo "<p></p> ";
                        echo "<h3 style='color:#ff0000' class='title'>Event name: </h3>";
                        echo " ";
                        echo $row['eventname'];
                        echo "<p></p> ";
                        echo "<h3 style='color:#ff0000' class='title'>Event Venue: </h3>";
                        echo " ";
                        echo $row['venue'];
                        echo " <p></p>";
                        echo "<h3 style='color:#ff0000' class='title'>Event Date: </h3>";
                        echo " ";
                        echo $row['date'];
                        echo " <p></p>";
                        echo "<b>Event Description: </b>";
                        echo " ";
                        echo $row['eventdescription'];
                }
            }
?>