9

Here is my HTML structure:

div{
  direction: rtl;
}

span{
  direction: ltr;
}
<div>
  <span>امروز -2</span>
  </div>

This is expected result:

enter image description here

As you see, - sign should come in the beginning of the number. How can I do that?

Note: The direction of div should be rtl.


ٍEDIT: I generate that number like this:

$sums_value = sprintf("%+d",$sums_value);

/* 
sums_value = -2 //=> -2
sums_value = 2  //=> +2

So the number has right format, but I don't know why it will be broken in the output:

enter image description here

Martin AJ
  • 6,261
  • 8
  • 53
  • 111
  • It's probably treating `-2` as one item, so it's not reversing it. Just write the number normally in the markup. – Carcigenicate Aug 12 '16 at 16:08
  • I know this is probably painfully obvious, but is there no reason you can simply write it in the ltr format? (Is it dynamic content?) – DBS Aug 12 '16 at 16:16
  • @Carcigenicate The number is written normally in the markup. As you see it is `-5` in the inspect element box. But I don't know why it will be reverse in the website like `5-` ... – Martin AJ Aug 12 '16 at 16:27
  • @DBS Yeah it is a dynamic content. – Martin AJ Aug 12 '16 at 16:27

3 Answers3

13

Since your screenshot has the "-2" in a different span element you could is the unicode-bidi option on that specific span:

div{
  direction: rtl;
}

span{
  direction: ltr;
  unicode-bidi: bidi-override;
}
<div>
  امروز 
  <span>-2</span>
</div>

The general idea of unicode-bidi is to have the ability to change the default behavior of directionality of the text where you have multiple languages on the same page.

Since you are using an RTL language, and you want the -2 to appear in LTR, the unicode-bidi: bidi-override is very handy.

Dekel
  • 60,707
  • 10
  • 101
  • 129
8

The following worked for me:

span {
  unicode-bidi: plaintext;
}

More info: https://developer.mozilla.org/en-US/docs/Web/CSS/unicode-bidi

By the way, 'bidi' is short for bidirectional.

Marcus
  • 3,459
  • 1
  • 26
  • 25
3

You can use the before pseudo element to add a hyphen.

q::before { 
  content: "-";
  color: blue;
}


<q>Some quotes</q>, he said.

Will render as

-Some quotes, he said.
randominstanceOfLivingThing
  • 16,873
  • 13
  • 49
  • 72