Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display "Classic" built-up fractions with an horizontal line in CSS / JavaScript?

I have a fraction and I want to display it neatly and nicely.

For example

4/5 

would be

 4
 —
 5

I have looked at this and while this solution is decent the problem lies in having the 4 and the 5 in the same line with a straight line separating them as in traditional fractions.

Any hack or solution would be acceptable. Not necessarily CSS, Javascript or any other language is acceptable.

like image 388
cjds Avatar asked Dec 14 '12 12:12

cjds


People also ask

How do you display fractions in JavaScript?

Fractions don't exist in JavaScript, but you can rewrite them as division problems using the division operator. Note that the resulting number is always converted to decimals — just like with a calculator. Improper fractions use the division operator in the same way.

How do you display something horizontally in CSS?

If you want to make this navigational unordered list horizontal, you have basically two options: Make the list items inline instead of the default block. .li { display: inline; } This will not force breaks after the list items and they will line up horizontally as far as they are able.

Can you use fractions in CSS?

Yes, you can specify fractional pixels. As this has been part of CSS since the very first version, it should be well supported by any browser that supports CSS at all.

How do you show fractions in HTML?

The HTML5 MathML <mfrac> tag is an inbuilt element in HTML5. It is use to add fraction symbol between two digits or equations.


2 Answers

Use this

<sup>6</sup>/<sub>7</sub>​ 

DEMO


For straight line

HTML

<div class="top">2</div><div class="bottom">6</div>​ 

CSS

.top{border-bottom:solid black 1px; display:inline-block; float:left} .bottom{ display:inline-block; clear:left; float:left} 

​DEMO 2

like image 186
Sowmya Avatar answered Sep 23 '22 02:09

Sowmya


.fraction {
  display: inline-block;
  position: relative;
  vertical-align: middle; 
  letter-spacing: 0.001em;
  text-align: center;
  font-size: 12px;
  }
.fraction > span { 
  display: block; 
  padding: 0.1em; 
  }
.fraction span.fdn {border-top: thin solid black;}
.fraction span.bar {display: none;}
Foobar
  <div class="fraction">
    <span class="fup">4</span>
    <span class="bar">/</span>
    <span class="fdn">5</span>
  </div>
Foobar

Change .fraction font-size to get it to a size you want

like image 35
Oskari Kantoniemi Avatar answered Sep 23 '22 02:09

Oskari Kantoniemi