html – 具有自己流程的CSS浮动注释

前端之家收集整理的这篇文章主要介绍了html – 具有自己流程的CSS浮动注释前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
更新

我举起了一个赏金,但是一个正确的答案从未达到.我已经实现了一个适用于我的JS解决方案,但是我不会将答案标记为正确.如果可以使用CSS / HTML,我仍然希望看到它.但普遍的共识是,目前还不可能.

目标

CodePen here,底部可运行的代码段.

我有一些HTML内容,我想用一个在其上方浮动的小消息进行注释,在最左边,就像< ruby​​>注释(但不完全).可以有许多内容,每个都有自己的注释.内容必须遵循正常的文本流.这是我当前的HTML:

  1. <div class='item' style='color: red'>
  2. <div class='annotation'>nope</div>
  3. <div class='content'>It's a bird,</div>
  4. </div>
  5. <div class='item' style='color: green'>
  6. <div class='annotation'>still no</div>
  7. <div class='content'>it's a plane,</div>
  8. </div>
  9. <div class='item' style='color: blue'>
  10. <div class='annotation'>yeah!</div>
  11. <div class='content'>it's Superman! </div>
  12. </div>
  13. <div class='item' style='color: orange'>
  14. <div class='annotation'>muahaha</div>
  15. <div class='content'>Go get the Kryptonite</div>
  16. </div>

工作实例

下面这句话是一只鸟,这是一架飞机,它是超人!去氪石有4个独立的部分(4个内容),每个部分用不同的颜色表示.每个内容都有自己的注释,以上面的斜体显示.

我通过使内容和注释float:left和赋予注释为负余量来进行工作.这是CodePen中的示例1.

破例1

当注释长于内容时,会出现该问题.在下面,注释仍然没有改变到更长的时间你可能是对的.两条内容行继续遵循正常的内联流程(根据需要),但是由于注释仍然排列到其内容的左边缘,所以它们重叠.

这是CodePen中的示例2.

破例2

一个提出的解决方案是使用一个具有可见性的表:collapse来进行对齐,这有助于防止重叠,但是在注释开始超过内容的左边缘的情况下,它会在注释之后产生额外的空间.

应该如何工作

我想要注释遵循自己的流程,但不会破坏内容的自然内联流.看下面的内容是如何仍然是一个完整的句子,但是啊!被转移到右边,让你可以拥有所有需要的房间很长时间.然而,这个muahaha修正了,因为它有空间直接坐在Go上去得到氪石.

我可以改变CSS和HTML来实现这一点,但只有一个CSS的解决方案是最佳的.谢谢.

  1. .item {
  2. margin-top: 20px;
  3. }
  4. .content,.annotation {
  5. float: left;
  6. white-space: pre;
  7. }
  8. .annotation {
  9. margin-top: -25px;
  10. font-style: italic;
  11. }
  12.  
  13.  
  14. h3 {
  15. clear: both;
  16. margin: 0;
  17. padding: 20px 0;
  18. }
  19.  
  20. td:first-child {
  21. color: red;
  22. }
  23. td:nth-child(2) {
  24. color: green
  25. }
  26. td:nth-child(3) {
  27. color: blue;
  28. }
  29. td:nth-child(4) {
  30. color: orange;
  31. }
  1. <h3>Working Example</h3>
  2. <div class='item' style='color: red'>
  3. <div class='annotation'>nope</div>
  4. <div class='content'>It's a bird,</div>
  5. </div>
  6. <div class='item' style='color: blue'>
  7. <div class='annotation'>yeah!</div>
  8. <div class='content'>it's Superman! </div>
  9. </div>
  10. <div class='item' style='color: orange'>
  11. <div class='annotation'>muahaha</div>
  12. <div class='content'>Go get the Kryptonite</div>
  13. </div>
  14.  
  15.  
  16. <h3>Broken Example 1 (note the overlap)</h3>
  17. <div class='item' style='color: red'>
  18. <div class='annotation'>nope</div>
  19. <div class='content'>It's a bird,</div>
  20. </div>
  21. <div class='item' style='color: green'>
  22. <div class='annotation'>you may be right</div>
  23. <div class='content'>it's a plane,</div>
  24. </div>
  25. <div class='item' style='color: blue'>
  26. <div class='annotation'>yeah!</div>
  27. <div class='content'>it's Superman! </div>
  28. </div>
  29. <div class='item' style='color: orange'>
  30. <div class='annotation'>muahaha</div>
  31. <div class='content'>Go get the Kryptonite</div>
  32. </div>
  33.  
  34. <h3>Broken Example 2 (note the overlap)</h3>
  35. <table>
  36. <tr style='font-style: italic'>
  37. <td>nope</td><td>you may be right</td><td>yeah!</td><td>muahaha</td>
  38. </tr>
  39. <tr style="visibility:collapse;"><td>It's a bird,</td><td>it's a plane,</td><td>it's Superman! </td><td>Go get the kryptonite</td></tr>
  40. </table>
  41. <table style="margin-top:-25px;"><tr><td>It's a bird,</td><td>it's Superman!</td><td>Go get the kryptonite</td></tr></table>
  42.  
  43. <h3>How it should look (cheating with positioning)</h3>
  44. <div class='item' style='color: red'>
  45. <div class='annotation'>nope</div>
  46. <div class='content'>It's a bird,</div>
  47. </div>
  48. <div class='item' style='color: blue'>
  49. <div class='annotation' style='margin-left: 35px'>yeah!</div>
  50. <div class='content'>it's Superman! </div>
  51. </div>
  52. <div class='item' style='color: orange'>
  53. <div class='annotation'>muahaha</div>
  54. <div class='content'>Go get the Kryptonite</div>
  55. </div>

解决方法

也许这将适合你:
  1. <table>
  2. <tr>
  3. <td>nope</td>
  4. <td>you may be right</td>
  5. <td>yeah it is!</td>
  6. </tr>
  7. <tr style="visibility:collapse;">
  8. <td>It's a bird,</td>
  9. <td>it's a plane,</td>
  10. <td>it's Superman!</td>
  11. </tr>
  12. </table>
  13. <table style="margin-top:-25px;">
  14. <tr>
  15. <td>It's a bird,</td>
  16. <td>it's Superman!</td>
  17. </tr>
  18. </table>

猜你在找的HTML相关文章