Syntax highlighting with HTML and CSS

If you have a blog or a portfolio on which you want to present code, whether HTML / JavaScript or CSS code extracts, you have undoubtedly come up against a problem: how do you activate syntax highlighting to make it look as if you had put the code in a code editor? In this article, we'll look at how to do it.
•
Updated on August 14, 2026
•
2 min read
Believemy logo
In this article

If you have a blog or a portfolio where you want to showcase code, whether it's HTML/JavaScript snippets or CSS, you have very likely encountered a problem: how to enable syntax highlighting to make it look as if the code is in a code editor? Let's see in this article how to do it together.

Image

 

Step 1

The HTML code.

HTML
<pre>
  <code class="css">
    .search-input {
      box-shadow: 0px 1px 4px rgba(0,0,0,.15);
      /* This is a comment */
      font-size: 1rem;
      width: 100%;
    }
  </code>
</pre>

When we click on the button, the notification will appear like magic!

 

Step 2

Let's style the button.

CSS
button {
  font-size: 1.2rem;
  color: #fff;
  background: #ff4754;
  padding: 1.2rem 2.4rem;
  border-radius: 0.2rem;
  transition: all 0.2s;
}

button:hover {
  cursor: pointer;
  opacity: 0.8;
}

Let's finish styling the class created by highlight.js.

CSS
.hljs {
  padding: 0.6rem 2rem;
  background: black;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 5px;
  transition: all 0.2s;
}

.hljs:hover {
  border-color: #ff4754;
}

In this step, we style the hljs class even though we did not create this class.

This is actually created automatically by Highlight.js when it detects a <code></code> tag.
We are merely taking advantage of this automatically generated class to style the entire block in which we place our code to color.

Category: Developers

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.

Believemy logo
Comments (0)

You need to be logged in to comment on this article: log in or sign up.

Read next

Creating a markdown previewer with React

Creating a real markdown previewer with React, and above all, doing it correctly, requires real concentration! Fortunately, with a little help anyone can do it. Here are a few tips for creating your own markdown previewer!

December 9, 2024

How can you use CSS variables to make everything more dynamic?

Perhaps you've already had that moment when you wanted to change the main colour of one of your projects? Or perhaps you've already experienced those moments when you go back to retrieve the colours of your site's theme? It's all very time-consuming and impractical. Thanks to CSS variables, this will be a thing of the past! Let's find out how.

December 9, 2024

Creating a random colour generator with JavaScript

Want to practise your JavaScript skills? Try building a random colour generator! In this article, we'll look at how to do it.

December 9, 2024

Add a "click to copy" button with JavaScript

Would you like to make it quick and easy for your users to copy text onto your project? That famous link to share your article? That affiliate link that helps you generate extra sales? Then this article is for you! Let's take a look at the two main methods for adding a "click to copy" button with the help of JavaScript.

December 9, 2024

Link CSS to HTML: 3 Methods Explained Simply

Learn the 3 methods to link CSS to HTML: external stylesheet, internal style, and inline. Code examples, best practices, and common mistakes to avoid.

March 22, 2026

Create a menu that spans the entire page using CSS

From time to time we have to ask our visitors which page they want to go to, by displaying the main sections of our website. This menu is often not very pleasant to use, so let's find out together in this article how to make it much more modern, with beautiful animations that will make your passers-by want to visit your site!

December 9, 2024

Create an animated gradient background on hover avec CSS

Whether it's to highlight a part of your website or a quote as in this example, generating and adding a gradient to the background can give your website a very modern touch. Find out in this article how to do it easily!

December 9, 2024

Create a clean, modern timeline using html and css

Whether you want to show a purchasing process, the highlights of your career on your portfolio, or anything else, learn how to create a sleek, modern timeline to show it off!

December 9, 2024

Become the solopreneur you dream of being.

First project, career change, business launch: access our courses, practice on real-world cases, and move forward with clear guidance.