1

I am trying to apply css styles to a php loop:

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

However this does not seem to be applying the css style:

.title{
    color : red;
}

I have tried putting <?php ?> tags around the echo and then applying the css but that still doesnt work.

   $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'];
                }
            }


        ?>
16
  • Hello, Have you tried below code. <?php echo "<style>.title{ color : 'red';}</style>" ?> Commented Oct 9, 2017 at 4:36
  • d joe so what problem you are facing?Seems to be working fine Commented Oct 9, 2017 at 4:39
  • is <h3 class='title'>Event name: </h3> is showing in your out? Commented Oct 9, 2017 at 4:40
  • @javedrathod yup doesnt work :( Commented Oct 9, 2017 at 4:40
  • 1
    so you are applying css on the same page or in a css file which is included to this code page? Commented Oct 9, 2017 at 4:44

3 Answers 3

0

We need to use a colon (:) instead of an equal sign to assign a CSS property value:

.title {
    color: red;
}
Sign up to request clarification or add additional context in comments.

1 Comment

@djoe This doesn't seem like an issue related to PHP. Try inspecting a .title element using the developer tools of your browser. Check to see if something else overrides the color property in the element styles.
0
<?php echo "<style>h3.title{ color : red;}</style>"; ?>

Use This Code

2 Comments

the text just disappears when i do that ? not sure why
Which text disappear? Please remove single quotes from red and try !important property after that.
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'];
                }
            }
?>

2 Comments

The suggestion here is to add style='color:#ff0000'. This would make sense if there's another .title rule with higher priority.
Explain your answer too else its hard to other to understand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.