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

前端 未结 10 2243
旧巷少年郎
旧巷少年郎 2020-12-03 13:37

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

10条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-03 14:20

    This does not automatically convert '1/2' to a fraction form. But if you have more control over the templating, you can do the following. Since no one has suggested using a table yet, here goes:

    HTML:

    Top of Fraction
    Bottom of Fraction

    CSS:

    table.fraction {
      display: inline-block;
      text-align: center;
      margin-left: 1em;
    }
    
    table.fraction td {
      line-height: 2em;
    }
    
    table.fraction td.top {
      border-bottom: 1px solid darkgray;
    }
    

    Result:

    Fraction using a table

提交回复
热议问题