code block example

Hey, i know code blocks are usually written using the pre tag, but since that's a nightmare to read and navigate through in the html due to the fact you have to un-indent everything inside of it, i tried to come up with a more manageable way of creating one. Is it an acceptible method? Am i allowed to put div tags in code tags and span tags inside of eachother etc? does it cause any accessibility or semantic problems etc? i'd appreciate any opinions, thanks in advance. https://codepen.io/deerCabin/pen/GRbpzML
34 Replies
ἔρως
ἔρως5mo ago
the typical thing for syntax highlight is to use <pre> with <code> and then <span> for all the tokens with <br> for newlines
ἔρως
ἔρως5mo ago
yes, which you should use instead
Jochem
Jochem5mo ago
everyone uses highlight.js for displaying code, even Discord and Stack Overflow
ἔρως
ἔρως5mo ago
and does exactly what i described it also has language detection, so, more often than not, you don't need to specify the language
snxxwyy
snxxwyyOP5mo ago
ah okay i see, if you don't want to use a library though (i'm not saying i won't, i'll for sure look into it), would this sort of thing be acceptable?
ἔρως
ἔρως5mo ago
you would be basically doing what highlight.js do, to make it work you can't put <div> inside <code> because <code> is inline and <div> is a block
Jochem
Jochem5mo ago
MDN lists the acceptable contents of a code element as Phrasing content
ἔρως
ἔρως5mo ago
THAT'S WHAT I WAS MISSING! phrasing content span, b, br, a, strong, .... stuff like that no p, div, pre and stuff can go inside the <code> you can put it, and the browser will probably do something, but good luck
snxxwyy
snxxwyyOP5mo ago
Oh I didn’t know phrasing content existed, that’s super useful. In terms of br for line breaks, if everything is in a pre tag, couldn’t you enter a new line? Imagining it, everything would have to essentially be in one long line to prevent excessive space which would be a nightmare
ἔρως
ἔρως5mo ago
trust me on this: you don't want to deal with fiddly indentation and newlines and all that stuff it is inside the <pre>, yes, but it is super fiddly you can do it, but good luck
Jochem
Jochem5mo ago
It's fine to figure this stuff out for understanding HTML, but displaying highlighted code is one of those cases where it's just much better to use the library. like, by all means build it for fun and educational purposes, but just use highlight.js if you need to do it in production code
snxxwyy
snxxwyyOP5mo ago
Ah okay, I appreciate the help, I’ll give it a go with what y’all have suggested and come back if I’ve got any other questions, pre is quite confusing haha
ἔρως
ἔρως5mo ago
<pre> is pretty good for code and other related things, like console output and stuff like that
snxxwyy
snxxwyyOP5mo ago
i'll bear that in mind, thanks so i assume this is the way you meant? https://codepen.io/deerCabin/pen/PorPVvw, but do you see how clumped everything is together in the html? i feel that would get hard to manage when there's a lot of "code".
ἔρως
ἔρως5mo ago
everything in a single line it makes whitespace easier to handle you can use &#9; to indent
snxxwyy
snxxwyyOP5mo ago
ohh i see, i was just about to ask about indenting, and i assume i can use tab-size to control that. i updated the codepen, i'm not sure if i did it correctly but the br tags don't work when it's all in one line
ἔρως
ἔρως5mo ago
it is working
No description
snxxwyy
snxxwyyOP5mo ago
Oh I got confused with line breaks and spaces sorry. If I wanted to add a space between two spans I assume I’d add two br tags?
ἔρως
ἔρως5mo ago
hell no! yikes! that's 90's formatting 🤣 use line-height
snxxwyy
snxxwyyOP5mo ago
Oh right 😅, but wouldn’t line height place space between where the red lines are? I meant space where the red arrow is.
No description
snxxwyy
snxxwyyOP5mo ago
Apologies for the poor diagram, I’m on my phone atm
ἔρως
ἔρως5mo ago
No description
ἔρως
ἔρως5mo ago
this is what you want, right?
snxxwyy
snxxwyyOP5mo ago
not quite, the shaded red is the space created by the line height, which does space the text out, however i mean like space space between it, like margin space, the shaded blue.
No description
ἔρως
ἔρως5mo ago
yes, that's an empty line, so, another br
snxxwyy
snxxwyyOP5mo ago
ah okay, thank you for the help.
ἔρως
ἔρως5mo ago
you're welcome
snxxwyy
snxxwyyOP5mo ago
oh one more question, how about adding spaces between characters, i assume it'd be &#160;?
ἔρως
ἔρως5mo ago
no, just space
snxxwyy
snxxwyyOP5mo ago
oh okay, cool
ἔρως
ἔρως5mo ago
the only annoying thing is the tabs for indenting, and the newlines
snxxwyy
snxxwyyOP5mo ago
ah okay, thanks
ἔρως
ἔρως5mo ago
you're welcome
Want results from more Discord servers?
Add your server