[SalesForce] Apex string with html tags in Visualforce

Is there a way in visualforce to interpret html tags in string fields from apex class?

What I am looking for is, say I have

String str1="Dormammu! <div style='color:#313131;'> I have come to bargain!</div>"; in apex class.

And if I use {!str1} in my visualforce page, how to make it such that style defined in the string is applied to I have come to bargain!

From what I tried, the text displayed in Visualforce page is

Dormammu! <div style='color:#313131;'> I have come to bargain!</div>

Best Answer

Yes you can:

Class Property

String str1="Dormammu! <div style='color:#313131;'> I have come to bargain!</div>";

VF Page

<apex:outputText value="{!str1}" escape="false"/>

When you add the escape="false" to the vf component it will output the string unescaped to the DOM which would cause your HTML to be displayed. Keep in mind though that this is generally not a good idea as it is a potential point of vulnerability and is look upon with scrutiny by security review if your are developing a managed package.