Hacker News new | ask | show | jobs
by Chris_Newton 3747 days ago
Notes on web pages are tricky because there are so many edge cases, and they tend to expose the limitations of our current HTML+CSS model.

For example, we can achieve an effect similar to the one shown here using just CSS (no JS) if we’re able to use position:relative on the containing paragraph without breaking anything else in the styling:

HTML:

    <p class="note-container">This is some text with a <span class="note" desc="Style me!">marginal note</span>. Lorem ipsum dolor sit amet...</p>
CSS:

    .note-container {
      position: relative;
      width: 400px;
    }
    
    .note {
      text-decoration: underline;
    }
    
    .note:hover::after {
      content: attr(desc);
      position: absolute;
      top: 0;
      left: 440px;
      width: 150px;
      height: 100%;
      border-left: 1px solid black;
      padding-left: 9px;
    }
I put a quick demo of this one here:

https://jsfiddle.net/2ejk2who/

However, what happens if we need to work with touch-only devices, where hover effects aren’t available? Two options would be to show the notes all the time, or to embed a hidden checkbox and make the anchor text its label. In the latter case, tapping on the text could then toggle whether the marginal note appears if we used something based on :checked instead of :hover.

But now what if more than one note is set to visible at once? We can’t combine the position:absolute technique to get neat alignment at the top of the paragraph with using floats so multiple notes automatically fall under one another. I don’t have a good pure-CSS answer for this one.

In any case, on a smaller screen we might not want to show marginal notes alongside the main text anyway. It would be helpful if there were a way to have the notes drop underneath the paragraph and stack up, but again, we can’t combine the kind of absolute positioning that would place a note there with something that extends the paragraph’s box so later content moves down after any visible notes.

Perhaps one day we’ll have more flexible options for generated content and CSS positioning that will let us do these things, but for now the only ways I know to achieve some of these effects still rely on JS.