5

I have an application with an angular frontend and C# backend. I'm receiving this rich text from the backend of my application:

<b>Hello <span style="font-weight:normal"> world </span></b>

What gets displayed is " Hello world "
What I want to be displayed is " Hello world "

The desired behaviour is that the styling of the span doesn't get overwritten. This codepen example shows the desired behaviour, but it is frontend ONLY: https://codepen.io/david-lo/pen/rNVOEbY

What am I missing?

5
  • Can you please try with this code <b>Hello <span style="font-weight:normal"> world </span></b> ? Commented Feb 13, 2020 at 9:38
  • @David Lo try like this <p>"Hello <b> world</b>"</p> Commented Feb 13, 2020 at 9:41
  • @AmanGojariya edited the question, sorry for the misunderstanding. Commented Feb 13, 2020 at 10:04
  • @DavidLo Your code is working on your codepen example. Commented Feb 13, 2020 at 10:13
  • @AmanGojariya Yes, the codepen shows how I want the outcome to be when i'm receiving the rich text from my backend (which does not work as intented). Commented Feb 13, 2020 at 10:52

3 Answers 3

2

I solved my issue by using SafeHtml and DomSanitizer :

Before:

  public txt: string; //txt is rendered in my html file. 
  @Input() public set header(_txt: string) {
    this.txt = _txt;
  }

The string input _txt has value <b>Hello <span style="font-weight:normal"> world </span></b>

The problem was that my span styling was ignored so the output would be:

Hello world

After:

  public txt: SafeHtml;
  @Input() public set header(_txt: string) {
    this.txt= this.domSanitizer.bypassSecurityTrustHtml(_txt);
  }

By using DomSanitizer the way shown above, my span styling was respected in the frontend and I achieved the desired output:

Hello world

Sign up to request clarification or add additional context in comments.

Comments

1

Please Add below css

p {margin: auto; display: inline-block;}

1 Comment

Thanks, this makes my codepen example output appear on one line like: <b>Hello<\b> world. but this is not my main issue.
1

If you are trying to render that code with the backslash symbols, this is clearly incorrect HTML.
The correct line should look like this:

<b>Hello <span style="font-weight:normal"> world </span></b>

2 Comments

The thing is that i'm sending it in kvotation marks from the backend, but it's received in the frontend the way that you're displaying it. I understand that that the question can be deceiving so i'll edit it.
Then I would suggest you inspect that specific element in the browser dev tools. Is the font-weight of the span overriden by a rule with "!important" flag? If no, check the font you are using - does it support different font-weights?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.