I want to post code snippets on my site. I’m sure you’ve all seem demos and tutorials where the author shows the code used. My questions is, do people copy and paste the code from the file into the markup or is there some way they are pulling the appropriate code into the page so said snippet is only maintained once?
btw, I posted this in the js forum thinking the solution might use an ajax request.
So, say you’re displaying a snippet of css code. When you show code examples are you maintaining the code both in the .css file and in your markup ( or pulling it from a database)?
I’m trying to find out if that’s what people do – maintain code in 2 separate locations. Or if there is some elegant way to pull a snippet of ( for example ) css code from the .css file so developers than can maintain code once.
Why is that a good idea? Is it just for presentation purposes so that a different background color doesn’t result in a ragged background on the right margin to the code?
I should take back the comment. I used to use the <code> element inside a <pre> and had troubles with it, but recently read about the semantics of keeping the <code> element, and have been meaning to go over some old code and put it back in (I stopped using it at some point, as I found it was simpler just to use <pre> on its own.) From memory, it made a difference to set <<code> to display: block. But just testing it now, it didn’t make much difference at all, apart from the background issue you mentioned. So I guess I’ve just thrown in a red herring. Sorry about that. (Either I’ve remembered incorrectly, or things have changed … but I suspect the former.)
It might also be worth mentioning that you can save your code in a GitHub Gist and then embed that on your site.
This is quite handy for longer code snippets.
I use this technique on my blog - here’s an example.
The longer code snippets are in Gists, the shorter snippets in <pre> and <code> tags.
So it’s shown on your site and a link to Gist? That’s cool.
Ya 10 years ago I tried to use code tag but found the margins/padding it produced (or something) too inconsistent cross browser. So that’s why I only use pre.