This method really helped me
I recently discovered a helpful technique for aligning fractions based on the length of the numerator and denominator. By adjusting some code provided by Johnatan Lin, I was able to achieve the desired outcome. Essentially, when the numerator is longer, I applied the "numerator" class only to the numerator element and left the denominator without any classes. Conversely, when the denominator is longer, I used the "denominator" class only for the denominator element.
The css styling
.fraction {
display: inline-flex;
flex-direction: column;
padding: 1em;
align-items: center;
}
.numerator {
border-bottom: 2px solid grey;
}
.denominator {
border-top: 2px solid grey;
}
Example of html code
Revenues
Number of guests
.fraction {
display: inline-flex;
flex-direction: column;
padding: 1em;
align-items: center;
}
.numerator {
border-bottom: 2px solid grey;
}
.denominator {
border-top: 2px solid grey;
}
<h1>A way to show fractions (not only numers)</h1>
This is what I did after different solutions found on stackoverflow.
<h2>This is with longer numerator</h2>
<div class="fraction">
<div class='numerator'>Longer than the other one</div>
<div>Number of guests</div>
</div>
<h2>This is longer denominator</h2>
<div class="fraction">
<div>Revenues</div>
<div class="denominator">Number of guests</div>
</div>
<p>Hello from Giovanni</p>
A simplified approach
In order to streamline the process, I created a JavaScript function that can be inserted in the body tag within script tags or in a separate JavaScript file.
function numeratore(num, den){
document.write("<div class='fraction'><div class='numerator'>" + num + "</div><div>" + den + "</div></div><div id='div1'></div>")
}
function denominatore(num, den){
document.write("<div class='fraction'><div>" + num + "</div><div class=\"denominator\">" + den + "</div></div><div id='div1'></div>")
}
Include this in the html body
<script>
denominatore("Revenues", "number of guests")
numeratore("Production costs", "Number of menu items served")
</script>
denominatore: for longer denominators
numeratore: for longer numerators
CSS styling remains the same
This CSS should be added within the style tags or in an external stylesheet.
.fraction {
display: inline-flex;
flex-direction: column;
padding: 1em;
align-items: center;
}
.numerator {
border-bottom: 2px solid grey;
}
.denominator {
border-top: 2px solid grey;
}