This document describes how to use the code-blocks.css
and copyToClipboard.js
files to style code blocks and add a
“Copy to Clipboard” button to them.
The provided CSS and JavaScript files allow you to style
<pre>
and <code>
elements and add
a button to copy the code to the clipboard. This is useful for
displaying code snippets on a webpage with a consistent style and
enhanced functionality.
code-blocks.css
<pre>
and
<code>
elements and the copy button.
copyToClipboard.js
<pre>
block containing a <code>
element.
The code-blocks.css
file uses CSS variables to define
the styling properties. These variables can be easily overridden to
customize the appearance of the code blocks and the copy button.
--code-font-family
monospace
.
--code-border-color
#ccc
.
--code-background-color
#f5f5f5
.
--copy-button-bg-color
#fff
.
--copy-button-border-color
#ccc
.
--copy-button-text-color
#000
.
--copy-button-hover-bg-color
#f0f0f0
.
Link the code-blocks.css
file in the
<head>
section of your HTML document:
<link rel="stylesheet" href="path/to/code-blocks.css">
Include the copyToClipboard.js
module in your HTML
document. This script should be included at the end of the
<body>
section or use the
DOMContentLoaded
event to ensure the DOM is fully loaded
before executing the script.
<script type="module" src="path/to/copyToClipboard.js"></script>
Add <pre>
and <code>
elements
to your HTML document. The JavaScript module will automatically add a
“Copy to Clipboard” button to each <pre>
block
containing a <code>
element.
<pre><code>public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
}</code></pre>
}
<pre><code>public class AnotherExample {
public static void main(String[] args) {
System.out.println("Another example!");
}</code></pre> }
Here is an example of a complete HTML document that includes the CSS and JavaScript files:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Code Blocks Example</title>
<link rel="stylesheet" href="path/to/code-blocks.css">
</head>
<body>
<h1>Code Examples</h1>
<pre><code>public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
}</code></pre>
}
<pre><code>public class AnotherExample {
public static void main(String[] args) {
System.out.println("Another example!");
}</code></pre>
}
<script type="module" src="path/to/copyToClipboard.js"></script>
</body>
</html>
You can override the CSS variables to customize the appearance of the
code blocks and the copy button. Add a <style>
block
to your HTML document and redefine the variables as needed.
<style>
:root {
--code-font-family: 'Courier New', monospace;
--code-border-color: #ff5733;
--copy-button-bg-color: #e7e7e7;
--copy-button-border-color: #ff5733;
}</style>
The copyToClipboard.js
module performs the following
actions:
DOMContentLoaded
event to ensure the DOM
is fully loaded.<pre>
elements in the document.<pre>
element, checks if it contains a
<code>
element.<pre>
element.<code>
element to the clipboard when clicked.By following these steps, you can style code blocks and add a “Copy to Clipboard” button to them using the provided CSS and JavaScript files. This enhances the user experience by making it easy to copy code snippets from your webpage.
Here is an example of a complete HTML document with customized styling:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Customized Code Blocks Example</title>
<link rel="stylesheet" href="path/to/code-blocks.css">
<style>
:root {
--code-font-family: 'Courier New', monospace;
--code-border-color: #ff5733;
--copy-button-bg-color: #e7e7e7;
--copy-button-border-color: #ff5733;
}</style>
</head>
<body>
<h1>Customized Code Examples</h1>
<pre><code>public class CustomHelloWorld {
public static void main(String[] args) {
System.out.println("Custom Hello, world!");
}</code></pre>
}
<pre><code>public class CustomExample {
public static void main(String[] args) {
System.out.println("Another customized example!");
}</code></pre>
}
<script type="module" src="path/to/copyToClipboard.js"></script>
</body>
</html>
This setup allows you to maintain a clean and organized stylesheet with reusable variables and provides the flexibility to override these variables as needed.