Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWeb Content Preparation

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Making Fractions

Creating fractions with HTML can have surprising results for the unwary. For websites with various fractions, consistency requires they all be constructed with the same method.

There are at least 4 ways to make fractions:

  1. Unformatted fractions — These fractions are typed straight across, with the numerator and the denominator having the same baseline. Examples are 1/2 and 64/256.

  2. Using HTML entities — These would be simple single-digit fractions like ½ and ¾. They are limited to pre-defined HTML entities. Here is a list.

    ½ = ½ ⅓ = ⅓ ⅔ = ⅔ ¼ = ¼ ¾ = ¾ ⅕ = ⅕ ⅖ = ⅖ ⅗ = ⅗ ⅘ = ⅘
    ⅙ = ⅙ ⅚ = ⅚ ⅛ = ⅛ ⅜ = ⅜ ⅝ = ⅝ ⅞ = ⅞
  3. Using HTML sup and sub codes — Fractions with larger numerators and denominators can be made, like 128256 and 64256. They tend to open the line spacing to accommodate the fraction number character positioning.

  4. Using CSS classes — CSS classes can be defined to create fractions, like 64256 and 128256 (with the ⁄ HTML entity fraction‑slash) or 64/256 and 128/256 (with the keyboard slash). They don't open the line spacing to accommodate the fractions.

The last method, using CSS classes, is addressed in this article.

Either the HTML entity ⁄ fraction‑slash character or the keyboard slash character may be used to publish fractions. In my opinion, ⁄ creates a better-formed fraction. Both methods are addressed here.

The difference between the two is the HTML entity ⁄ fraction‑slash character is slightly more slanted than the keyboard slash character. In some fonts, the ⁄ fraction‑slash might also be thinner.

Here is an example of both in a large font, first the ⁄ fraction‑slash, then the keyboard slash:

⁄ /

CSS class names are used for the fractions. The style is dynamic with the font size so the fractions will look good at any size.

Here are three fractions at various sizes. These are composed with the ⁄ fraction‑slash character.

38 64256 128256
38 64256 128256
38 64256 128256

Here are the fractions composed with the keyboard slash character.

3/8 64/256 128/256
3/8 64/256 128/256
3/8 64/256 128/256

As you can see, the two different kinds of slashes publish the fractions with a slightly different format.

Here are the CSS styles. The first two are for the fraction numerator and denominator numbers. The third is used only for the ⁄ fraction‑slash character.

<style type="text/css">
.numerator {
   font-size:.7em;
   line-height:.7em;
   vertical-align:.3em;
}
.denominator {
   font-size:.7em;
   line-height:.7em;
   vertical-align:-.2em;
}
.slash-entity {
   font-size:.8em;
   line-height:.8em;
}
</style>

That style should be good for most font families. If needed, tweak the styles to make the fractions look good at your website.

Construct a fraction in three steps: The numerator, the slash, and the denominator.

Here is the code for each part of the 64256 fraction. Following that is the code for the entire fraction.

  1. The Numerator.

    <span class="numerator">64</span>
    
  2. The Slash.

    If the &frasl; fraction‑slash character is used:

    <span class="slash-entity">&frasl;</span>
    

    If the keyboard slash character is used:

    /
    
  3. The Denominator.

    <span class="denominator">256</span>
    

The code for the entire fraction.

If the &frasl; fraction‑slash character is used:

<span class="numerator">64</span><span class="slash-entity">⁄</span><span class="denominator">256</span> 

If the keyboard slash character is used:

<span class="numerator">64</span>/<span class="denominator">256</span> 

When style is applied consistently, and this applies to how fractions are formed, there tends to be less feeling of raggedness or irregularity.

(This article first appeared with an issue of the Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC