Superscript in CSS only?

后端 未结 15 1375
花落未央
花落未央 2020-11-29 15:20

How can I get superscript done, only in CSS?

I have a stylesheet where I mark the external links with a superscript character, but I\'m having a hard time getting th

相关标签:
15条回答
  • 2020-11-29 15:45

    I was working on a page with the aim of having clearly legible text, with superscript elements NOT changing the line's top and bottom margins - with the following observations:

    If for your main text you have line-height: 1.5em for example, you should reduce the line-height of your superscript text for it to appear correctly. I used line-height: 0.5em.

    Also, vertical-align: super works well in most browsers but in IE8 when you have a superscript element present, the rest of that line is pushed down. So instead I used vertical-align: baseline together with a negative top and position: relative to achieve the same effect, which seems to work better across browsers.

    So, to add to the "homegrown implementations":

    .superscript {
        font-size: .83em;
        line-height: 0.5em;
        vertical-align: baseline;
        position: relative;
        top: -0.4em;
    }
    
    0 讨论(0)
  • 2020-11-29 15:47

    http://htmldog.com/articles/superscript/ Essentially:

    position: relative;
    bottom: 0.5em;
    font-size: 0.8em;
    

    Works well in practice, as far as I can tell.

    0 讨论(0)
  • 2020-11-29 15:49

    The CSS property font-variant-position is under consideration and may eventually be the answer to this question. As of early 2017, only Firefox supports it, though.

    .super {
        font-variant-position: super;
    }
    

    See MDN.

    0 讨论(0)
  • 2020-11-29 15:51

    Honestly I don't see the point in doing superscript/subscript in CSS only. There's no handy CSS attribute for it, just a bunch of homegrown implementations including:

    .superscript { position: relative; top: -0.5em; font-size: 80%; }
    

    or using vertical-align or I'm sure other ways. Thing is, it starts to get complicated:

    • CSS superscript spacing on line height;
    • Beware CSS for Superscript/Subcript on why you arguably shouldn't style superscript/subscript with CSS at all;

    The second point is worth emphasizing. Typically superscript/subscript is not actually a styling issue but is indicative of meaning.

    Side note: It's worth mentioning this list of entities for common mathematical superscript and subscript expressions even though this question doesn't relate to that.

    The sub/sup tags are in HTML and XHTML. I would just use those.

    As for the rest of your CSS, the :after pseudo-element and content attributes are not widely supported. If you really don't want to put this manually in the HTML I think a Javascript-based solution is your next best bet. With jQuery this is as simple as:

    $(function() {
      $("a.external").append("<sup>+</sup>");
    };
    
    0 讨论(0)
  • 2020-11-29 15:54

    If you are changing the font size, you might want to stop shrinking sizes with this rule:

    sup sub, sub sup, sup sup, sub sub{font-size:1em !important;}
    
    0 讨论(0)
  • 2020-11-29 15:54

    Check out: http://www.cssdesignpatterns.com/Chapter%2012%20-%20ALIGNING%20CONTENT/Vertical-aligned%20Content/example.html

    if looks like you want "vertical-align:text-top"

    0 讨论(0)
提交回复
热议问题