How to Use CSS in PHP Echo to Add Style (3 Easy Ways)

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:

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:

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:

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

Join the Conversation

10 Comments

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.