I have a word, which has both superscript and subscript. Now I render it like this word<sub>1</sub><sup>2</sup>
And get the following:
word12.
How can I put the subscript exactly under the superscript?
The <sup> tag defines superscript text. Superscript text appears half a character above the normal line, and is sometimes rendered in a smaller font. Superscript text can be used for footnotes, like WWW. Tip: Use the <sub> tag to define subscript text.
Select the text you want to format as either a superscript or subscript. 2. To convert it to a superscript, press Ctrl + Shift + + (that's the Ctrl, Shift, and Plus sign keys). To make a subscript, press Ctrl + = (that's Ctrl and the equal sign).
The HTML <sup> element is found within the <body> tag. The <sup> tag is used to comply with typographical standards or conventions. The <sup> tag is generally used to specify exponents such as m2 and ordinal numbers such as 1st, 2nd or 3rd.
Here's a clean solution. Create two CSS classes:
.nobr {
white-space: nowrap;
}
.supsub {
display: inline-block;
margin: -9em 0;
vertical-align: -0.55em;
line-height: 1.35em;
font-size: 70%;
text-align: left;
}
You might already have the "nobr" class as a <nobr> replacement. Now to express the molecular formula for sulfate, use the "supsub" class as follows:
<span class="nobr">SO<span class="supsub">2-<br />4</span></span>
That is, enclose your superscript/subscript within the "supsub" class, and put a <br /> between them. If you like your superscripts/subscripts a bit larger or smaller, then adjust the font size and then tinker with the vertical-align and line-height. The -9em in the margin setting is to keep the superscripts/subscripts from adding to the height of the line containing them; any big value will do.
There are many ways you can do this with CSS, and each has their pros and cons. One way would be to use relative positioning. A quick example might work like this:
<span class="fraction">
<span class="numerator">3</span>
<span class="denominator">4</span>
</span>
And the CSS to go along with this:
span.fraction { }
/* Or child selector (>) if you don't care about IE6 */
span.fraction span.numerator {
position:relative;
top:-0.5em;
}
span.fraction span.denominator {
position:relative;
top:0.5em;
left:-0.5em; /* This will vary with font... */
}
This particular example would work better if you use a monospaced font.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With