下标和上标为同一元素

时间:2021-10-08 20:11:16

Is there any way to add both subscript and and superscript to the same element? If I do

有没有办法将下标和上标添加到同一个元素?如果我做

Sample Text<sub>Sub</sub><sup>Sup</sup>

the superscript appears after the subscript. One I idea I had is to do something like:

上标出现在下标之后。我有一个想法是做一些像:

<table>
    <tr>
        <td rowspan='2' valign='center'>Sample Text</td>
        <td>Sup</td>
    </tr>
    <tr>
        <td>Sub</td>
    </tr>
</table>

It seems to do the job but is quite ugly. Any better ideas?

它似乎做了这项工作,但非常难看。还有更好的想法?

Thanks!

4 个解决方案

#1


6  

I'm no CSS guru but you could try something along the lines of http://jsfiddle.net/TKxv8/1/

我不是CSS大师,但你可以尝试一下http://jsfiddle.net/TKxv8/1/

There are a lot of hardcoded values and the effects on other elements around may only be found afterwards but it's a good place to start.

有许多硬编码值,并且可能只会在之后找到对周围其他元素的影响,但这是一个很好的起点。

Sample Text 
<span class='supsub'>
    <sup class='superscript'>Sup</sup>
    <sub class='subscript'>Sub</sub>
</span>

.supsub {position: absolute}
.subscript {color: green; display:block; position:relative; left:2px; top: -5px}
.superscript {color: red; display:block; position:relative; left:2px; top: -5px}

#2


4  

This one is similar to CyberDudes approach, additionally it indents the following text depending on the width of sub and sup:

这个类似于Cyber​​Dudes方法,另外它根据sub和sup的宽度缩进以下文本:

http://jsfiddle.net/jwFec/1/

Some text <span class="supsub"><sup>sup</sup><sub>sub</sub></span> followed by other text.<br />

<style>
.supsub {
    display: inline-block;
}

.supsub sup,
.supsub sub {
    position: relative;
    display: block;
    font-size: .5em;
    line-height: 1.2;
}

.supsub sub {
    top: .3em;
}
</style>

#3


3  

Well, you can specify position of sup relative to Sample Text's right border.

那么,您可以指定相对于Sample Text右边框的sup位置。

http://jsfiddle.net/a754h/

#4


2  

Here's a clean solution. Create two CSS classes:

这是一个干净的解决方案。创建两个CSS类:

.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:

您可能已经将“nobr”类作为 替换。现在来表达硫酸盐的分子式,使用“supsub”类如下:

<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.

也就是说,将你的上标/下标括在“supsub”类中,并在它们之间加上一个
。如果你喜欢你的上标/下标有点大或小,那么调整字体大小,然后修改vertical-align和line-height。边距设置中的-9em是为了防止上标/下标添加到包含它们的行的高度;任何大的价值都可以。

#1


6  

I'm no CSS guru but you could try something along the lines of http://jsfiddle.net/TKxv8/1/

我不是CSS大师,但你可以尝试一下http://jsfiddle.net/TKxv8/1/

There are a lot of hardcoded values and the effects on other elements around may only be found afterwards but it's a good place to start.

有许多硬编码值,并且可能只会在之后找到对周围其他元素的影响,但这是一个很好的起点。

Sample Text 
<span class='supsub'>
    <sup class='superscript'>Sup</sup>
    <sub class='subscript'>Sub</sub>
</span>

.supsub {position: absolute}
.subscript {color: green; display:block; position:relative; left:2px; top: -5px}
.superscript {color: red; display:block; position:relative; left:2px; top: -5px}

#2


4  

This one is similar to CyberDudes approach, additionally it indents the following text depending on the width of sub and sup:

这个类似于Cyber​​Dudes方法,另外它根据sub和sup的宽度缩进以下文本:

http://jsfiddle.net/jwFec/1/

Some text <span class="supsub"><sup>sup</sup><sub>sub</sub></span> followed by other text.<br />

<style>
.supsub {
    display: inline-block;
}

.supsub sup,
.supsub sub {
    position: relative;
    display: block;
    font-size: .5em;
    line-height: 1.2;
}

.supsub sub {
    top: .3em;
}
</style>

#3


3  

Well, you can specify position of sup relative to Sample Text's right border.

那么,您可以指定相对于Sample Text右边框的sup位置。

http://jsfiddle.net/a754h/

#4


2  

Here's a clean solution. Create two CSS classes:

这是一个干净的解决方案。创建两个CSS类:

.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:

您可能已经将“nobr”类作为 替换。现在来表达硫酸盐的分子式,使用“supsub”类如下:

<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.

也就是说,将你的上标/下标括在“supsub”类中,并在它们之间加上一个
。如果你喜欢你的上标/下标有点大或小,那么调整字体大小,然后修改vertical-align和line-height。边距设置中的-9em是为了防止上标/下标添加到包含它们的行的高度;任何大的价值都可以。