What I want
Sidenotes. The foremost concern is, of course, placing them at a height relative to that of the text referencing them. I can do it with JavaScript, looking at the offsetTop
property of the note reference, but then I would have to handle viewport resizes and zooming as well, whereas a CSS solution would leave that to the browser. A CSS solution is thus preferable.
Background and motivation
This is part of the responsive design of a blog. Notes shall be displayed as sidenotes if there is enough room for them in the margins; otherwise, they shall appear as footnotes. This post explains it in more detail. Keep in mind that, past a certain width, we'll have margins anyway, since text lines cannot exceed a certain length if they are to remain readable, so we could as well employ that otherwise unused space. Tufte is a well-known proponent of this solution.
What I have tried
So far I have tried two approaches. Both of them use relative positioning on the marker (a <sup>
) so as to be able to position the note (a <span>
) with a top offset relative to that of the referencing text.
My first try employs absolute positioning:
article {
width: 50%;
margin-left: auto;
margin-right: auto;
}
sup {
/* Needed to position sidenotes */
position: relative;
}
.sidenote {
width: 20%;
position: absolute;
/* BTW: This should be proportional to the note's height,
so as to make its vertical center align with the text
referencing it */
top: -100%;
}
/* How could I set the notes' horizontal position relative to the margin or to an element other than the enclosing <sup>? */
.sidenote.l {
right: 0;
}
.sidenote.r {
left: 15vw;
}
<article>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat<sup>1<span class="sidenote r">Several variations of this sentence are known.</span></sup>. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</article>
The problem here is horizontal positioning. Since our reference is the <sup>
, it becomes tricky to set the sidenote near the margin. It might be doable with JavaScript, but then I would have to update the offset every time the viewport was resized. Not looking forward to it.
This post suggests a similar approach, but doesn't show which parent should be set as reference with relative positioning. If it's the <p>
, the problem arises that we can only have one note on each side per paragraph, which is a limitation I would rather avoid.
My second try revolves around CSS grid:
article {
display: grid;
grid-template-columns: 15% auto 15%;
}
p {
grid-column: 2;
}
sup {
position: relative;
}
.sidenote.l {
grid-column: 1;
}
.sidenote.r {
grid-column: 3;
}
<article>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat<sup>1<span class="sidenote r">Several variations of this sentence are known.</span></sup>. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</article>
However, it seems as though it weren't possible to put a child (the <span>
) in a different grid column than the one its parent (the <sup>
) belongs to.
null
. The need of them to function on any device with ease and intuitively, while still looking attractive, on the other hand, is vital for the success of whatever you're coding. Look for simple solutions that make sense. When needing to take this type of decisions I usually talk to non-technical people asking what would make sense to them. – Araucanian<sup>1</sup><sup>2</sup>
. Or maybe at a few words distance. Is the answer you are looking for supposed to cover this case? – Araucanian