Adjust the vertical positioning of ruby text

后端 未结 2 2057
遇见更好的自我
遇见更好的自我 2021-01-13 01:15

I\'d like to use HTML to mark up Japanese text with its pronunciation. However, I\'ve found that at large font sizes, the baseline of the <

2条回答
  •  Happy的楠姐
    2021-01-13 01:47

    I think I've found a solution which allows for styling elements while preserving their semantics. Here's a demonstration, and then a few key insights:

    body {
      font-size: 72pt;
    }
    
    ruby {
      display: inline-flex;
      flex-direction: column-reverse;
    }
    
    rb, rt {
      display: inline;
      line-height: 1;
    }
    おそ

    Set the element to be display: inline.

    Chrome seems to have some special behavior when is display: block, which prevents styling it as a normal block-level element. That special behavior appears to be disabled when using display: inline.

    Use a separate element for each annotation.

    While there are several valid ways to mark up ruby text, using a new element for each annotation gives us a nice container element and makes styling much easier. This is semantically equivalent to using a single ruby element with multiple and elements.

    Use flexbox to position ruby text.

    Flexbox is by far the simplest and most powerful way to position ruby text. If your browser doesn't yet support flexbox, you can try using inline-table, but I didn't have much success in styling content using it. Users of older browsers might also want to see Paul Fioravanti's answer.

    I used display: inline-flex to treat the ruby elements as if they were normal text while still having flex contents, and then flex-direction: column reverse to position the text vertically with the on top.

    Set line-height: 1 in the element.

    A lot of the extra space between the text and its ruby markup can be attributed to the fact that the line height is not the same as the text height by default. Setting line-height: 1 will ensure they are the same.

    If the spacing between elements is still not the desired spacing, setting margin-bottom on the element can adjust it. It can be set to a positive or negative value.

提交回复
热议问题