Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change direction of negative number with combination of LTR and RTL content

Tags:

html

css

bidi

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

like image 284
Martin AJ Avatar asked Aug 12 '16 16:08

Martin AJ


People also ask

What is LTR and rtl?

For example, the en-US locale (for US English) specifies left-to-right. Most Western languages, as well as many others around the world, are written LTR. The opposite of LTR, RTL (Right To Left) is used in other common languages, including Arabic ( ar ) and Hebrew ( he ).

How do you change the direction of HTML text?

Add dir="rtl" to the html tag any time the overall document direction is right-to-left (RTL). This sets the default base direction for the whole document.

What is LTR direction?

Use rtl for languages written from right to left (like Hebrew or Arabic), and ltr for those written from left to right (like English and most other languages).

How do you implement rtl?

Implementing RTL is very simple. You just have to add dir attribute to you top element of the web page. This will still work as is, showing component on left. Therefore, your CSS should change according to the direction and make it right: 10px; .


2 Answers

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.

like image 82
Dekel Avatar answered Oct 12 '22 04:10

Dekel


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.
like image 40
randominstanceOfLivingThing Avatar answered Oct 12 '22 05:10

randominstanceOfLivingThing