Last Updated on August 5, 2021 by Roshan Parihar
In this tutorial, learn how to use CSS in PHP echo to add style to the text content. The short answer is: use the style
attribute and add CSS to it within single quotes (‘ ‘).
Let’s find out with the examples given below to include CSS in PHP echo.
How to Use CSS in PHP Echo with Style Attribute
You can use the <p>
tag inside the PHP echo
statement to add text content. In this tag, you have to add a style
attribute within which you can mention CSS as given below:
1 2 3 |
<?php echo "<p style='color:red;'>This is a text in PHP echo.</p>"; ?> |
Output
This is a text in PHP echo.
The above example shows the output that changes the appearance of the text content after adding the CSS. You can add as much CSS to it as you want to include.
Add CSS in a Class and Include the Class in PHP Echo
You can mention as many CSS as you want in a class. After that, add the CSS class to the text content with <p>
tag in PHP echo
statement as given below:
1 2 3 4 5 6 7 8 9 10 11 |
<style> .mycss{ color: green; border:1px solid #000; background: #ccc; padding: 10px; } </style> <?php echo "<p class='mycss'>This is a text in PHP echo.</p>"; ?> |
Output
This is a text in PHP echo.
You have to first add as many CSS as you want in a CSS class. The above example added 4 CSS properties in a class to style the text content.
Use Double Quotes and Escape Using Backslash
In addition to the above all methods, you can add CSS class in PHP echo
statement using the double quotes. After that, you have to escape the quotes using the slash (\
) symbol as given in the example below:
1 2 3 4 5 6 7 8 9 10 |
<style> .mycssquote{ color: blue; border:1px solid red; padding: 5px; } </style> <?php echo "<p class=\"mycssquote\">This is a text in PHP echo.</p>"; ?> |
Output
This is a text in PHP echo.
The above example uses the double quotes (” “) escaped using the backslash (\) symbol.
FAQS on How to Use CSS in PHP Echo to Add Style
Q1. Can You Style PHP?
Answer: No, you cannot style PHP as it is a server-side scripting language that cannot interact with CSS directly. However, you can place CSS in the HTML content inside the PHP script. It applies the CSS to the HTML content in the output.
Q2. How Do I Style an Echo Statement in PHP?
Answer: You can add HTML tags inside the echo statement to print HTML in the output. To style an echo statement content, you have to add style
attribute in the HTML content to apply CSS. The resulted output is the styled HTML content in the output.
Q3. How to Style PHP Echo Output?
Answer: PHP echo output is the HTML content that prints as a result. You can apply a style to that HTML content using the style
attribute within the HTML tag content inside the PHP echo statement. This applies CSS to the PHP echo output.
Q4. How to Add CSS in PHP?
Answer: To add CSS in PHP, you have to use the style
attribute within the echo statement of PHP. You can also add CSS in PHP by declaring the style within <style>
tag for the required class. After that, you have to add that class within the HTML tag inside the PHP echo statement.
You May Also Like to Read
great help thank you, I choose option 1
You’re welcome, Penny. Happy to help you.
thanks a lot. I found it finally from your website. third option!!!
Great! I am glad it helped.