tags:

views:

185

answers:

3

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?

+1  A: 

Dont use the tags. Use CSS.

Check out this link

Shankar Ramachandran
couldn't you style <sup> and <sub> with these style attributes described in this article?
Dave
@Dave: You can, but you'd need to reset the default styles to iron out the differences between browsers.
Chetan Sastry
While useful, this doesn't answer his question.
Daniel Schaffer
This link is interesting, but I couldn't find there a word about placing subscript below superscript.
netimen
+1  A: 

Well, you can't do that with plain vanilla HTML. Like it's been mentioned, use CSS. But you will want some positioning aswell!

CodeMonkey
How do I do it?
netimen
+4  A: 

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.

Zack Mulgrew
But here the offset to the left depends on the size of the superscript and subscript. But what if nominator is 31234 and denominator is 56547? Is there a unified decision which works not depending on the size of superscript and subscript?
netimen
@netimen This is a good point and I thought of it after I whipped up the example above. In short: no, there is no magic value. This is one occasion where the CSS3 Ruby module might come in handy.
Zack Mulgrew
@netimen It would be possible to calculate the correct offset using JavaScript and then apply it to each instance of super/subscript. What is it that you're trying to represent? Fractions? Chemical compounds?
Zack Mulgrew
I see. May be I could use tables for this task?@Zack I'm trying to represent connections between words in a sentence)
netimen
While it goes against my "web developer instincts" I think that using a table might be the best thing you could do here, especially if you want the numbers "centered" with each other.
Zack Mulgrew
Thank you! but how do I inline a table into a text row?
netimen