Highlight.js: Highlight.JS Inline Code Syntax Highlightning

Created on 27 Sep 2015  路  3Comments  路  Source: highlightjs/highlight.js

Hello!

Highlight.js is a great highlightning tool. But now i want to know if it is also possible to make inline highlighting, so not like a block (like at http://tekkkz.com/posts/articles/arch_installation.html) but inside a text paragraph?

If it is possible, how?

Most helpful comment

Problem is:

.hljs {
    display: block;
}

The code won't be displayed as inline.

Solution is to add the following to your css code:

pre code.hljs {
  display: block;
}

code.hljs {
  display: inline;
}

All 3 comments

Yes, you can use highlight.js not only with <pre><code> blocks, but with any other, see README#custom-initialization.

Problem is:

.hljs {
    display: block;
}

The code won't be displayed as inline.

Solution is to add the following to your css code:

pre code.hljs {
  display: block;
}

code.hljs {
  display: inline;
}

Just pointing out, the ideal solution is to have pre > code.hljs on the library for display: block; and padding rules, but I solved this in my case with two lines, though I will expand the JS in here to make it clearer.

Intialize like this (Without jQuery):

for (i in document.getElementsByTagName("code")) {
  console.log(document.getElementsByTagName("code")[i]);
};

Or like this (With jQuery):

$('pre code').each(function(i, block) {
  hljs.highlightBlock(block);
});

And add this CSS: p code.hljs, li code.hljs { display: inline; padding: .2em }

The padding change is necessary for Solarized Dark, with .5em it overlaps for more convoluted examples, at least on my blog, and feels utterly ugly.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Rarst picture Rarst  路  8Comments

Lestoroer picture Lestoroer  路  7Comments

starikovs picture starikovs  路  5Comments

goloroden picture goloroden  路  3Comments

dlinx picture dlinx  路  5Comments