- How to display code snippets in HTML?
- Why do you need to display code snippets in HTML?
- Encode Reserved tags into HTML entities
- Syntax highlighting of code blocks
- Using PrismJS to style your code block in 3 easy steps
- Conclusion:
- FAQs:
- What is a code snippet?
- Is it good approach to use tag inside tag?
- How to organize code snippets efficiently?
- Is there a way to embed code snippets into the website without javascript?
- Do you recommend to use 3rd party libraries for Syntax Highlighting?
- Write Code Blocks in HTML
- Wrap the Tag and Its Contents Inside the Tag to Write Code Block in HTML
- Use the Tag and the CSS white-space Property to Write Code Block in HTML
- Related Article - HTML Tag
- Code Block
- Code Block Settings
- Code Snippets
- Shortcode
- Customization
- How to style background for the code?
- Adding CSS/Code for a single page
- Troubleshoot
- Code in code block section does not display.
- Resource
- Resource
- Contents
- Cookie and Privacy Settings
How to display code snippets in HTML?
Indie maker with a passion for SEO working on web projects. Ex-mobile dev-agency owner. Now, helping early stage founders turn their side projects into businesses.
You want your code snippets to look ‘pretty,’ but do you know how to display them properly in HTML and make them awesome? Do you want to display formatted code on your custom or WordPress blogs but don’t know the best resources, plugins, or libraries to do it? In this tutorial, I will share with you the possible easiest ways to display code snippets in HTML, along with examples.
Why do you need to display code snippets in HTML?
Showcasing code in HTML can be beneficial if you want to share your programming knowledge with your audience in the shape of code snippets. Let’s say you have a blog and you’re writing a technical guide with some code examples. You want to demonstrate parts of this code in your article with the proper format and styling. In that case, you will need a way to present them.
Normally, when you try to indent the text inside HTML tags. It ignores the line breaks and renders single lines. But, in
(preformatted) tag, spaces and line breaks aren't ignored. Just look into the simple text example below: You can easily print the code with the same approach. Just place it insidetag, and that's it. There is another tag combined withtag, which gives more semantic meaning to search engines. The HTML code tag. tag is used for the piece of computer code. Usually, you include this tag inside of thetag to tell a browser, and Google that’s a block of code you want to display, not to render. Here is an example:Encode Reserved tags into HTML entities
What if you want to display the HTML code block using the
tag? Looking at the example below, you will see that the entered HTML code has been rendered but not displayed because of reserved characters. To fix this issue, you will need to use HTML entities. Like this: Now, don't get yourself confused. There are tools available to convert HTML tags into HTML entities to make your life easier. For example, you can use Text HTML entities Convertor or HTML Encode.Syntax highlighting of code blocks
And if you're using WordPress, you can use the plugin Prismatic by Jeff Starr.
Using PrismJS to style your code block in 3 easy steps
- Download the JavaScript and CSS files of Prismjs or use CDN
- Now, link the CSS files inside the head section and the JavaScript file at the bottom
- Use the pre-defined class names in tag
You can use PrismJS for different languages like CSS, JavaScript, Go, PHP, etc.
Colors schemes vary on the style or theme you choose, like:
There are endless customizations that you can do to highlight your code blocks.
Some useful List of plugins are:
- Line Numbers
- Line Highlight
- Show Language
- Command Line
- Highlight Keywords
- and much more
By default, plugins are not included in downloaded files.
Before downloading the files, you must enable or select your desired plugins and languages.
Adding line numbers to code block:
Or copy to clipboard option:
Conclusion:
So far, you've learned about sharing code snippets on a website by displaying them through preformatted and computer code tags.
You have also learned about JS libraries and the WordPress plugin to integrate into your blog or website.
But now, what if you want to share useful code snippets with the team or friends in a more modern and smarter way?
Here comes the new code snippets manager by Codiga. It allows you to add, manage, and share code snippets within your chosen IDE. We provide plugins for VS Code, IntelliJ/Jetbrains and Google Chrome.
FAQs:
What is a code snippet?
In simple words, a code snippet is a block of code you can utilize in various programming projects. Code snippets are useful for reducing the time it takes to create software by allowing developers to reuse code.
Is it good approach to use tag inside tag?
Yes, if you want to display the computer/programming code, then using inside tag is recommended.
How to organize code snippets efficiently?
You can use smart code snippets to organize your code blocks; the best part is you can instantly share them with your team or the world!
Is there a way to embed code snippets into the website without javascript?
Yes, there is a way to display code snippets on the website without using the JavaScript library. You can use the and tags to insert preformatted text or source code and style them accordingly.
Do you recommend to use 3rd party libraries for Syntax Highlighting?
Yes, using a 3rd party solution for syntax highlighting on your websites is beneficial. I prefer Prism, a feature-rich Syntax Highlighting JS library.
Are you interested in Datadog Static Analysis?
Write Code Blocks in HTML
This article will introduce a few methods of writing blocks of code snippets in HTML.
Wrap the Tag and Its Contents Inside the Tag to Write Code Block in HTML
The tag defines a piece of computer code. The content inside is displayed in the browser’s default monospace font.
The tag is used to represent code blocks in HTML. The tag is an inline tag.
It means that if we write code snippets inside the tag, the contents inside of the tag are displayed in a line. It does not preserve line breaks or spaces.
The tag defines preformatted text. Any text in a element is displayed in a fixed-width font, and the text preserves both spaces and line breaks.
The text or contents inside the tag will be displayed as written exactly in the HTML source code. The tag is a block-level element.
If we wrap the tag and its contents inside the tag, the contents inside the tag will behave as a block-level element. The contents will have the monospace font, and the line breaks and spaces of the code snippets will also be preserved.
For example, write some JavaScript code and wrap the code inside the tag. Next, wrap the element with a tag.
p>Code snippet for addition of two numbers in javascriptp> pre> code> function add (a,b) sum = a + b; return sum; > code> pre>
The above example will display the JavaScript code block.
Use the Tag and the CSS white-space Property to Write Code Block in HTML
In this method, we will seek a replacement for the tag using some CSS styles. Previously, we knew the element was an inline element.
We used the tag to make it a block-level element. But, we can also make the tag a block-level element by setting its display property to block .
And we can preserve the line breaks and spaces of the code tag by setting its white-space property to pre-wrap . As a result, it will display code snippets as written in HTML source code.
Thus, we will be able to create code blocks in HTML.
For example, create a tag and write some JavaScript code snippet inside it. Give the tag a class name of the sum .
In CSS, select the sum class and set the display property to block and the white-space property to pre-wrap .
code class="sum"> function (a, b) sum = a + b; return sum; > code>
.sum display: block; white-space: pre-wrap; >
The code examples above will produce a JavaScript code block.
Related Article - HTML Tag
Copyright © 2023. All right reserved
Code Block
The code block element can be used to embed custom code as readable text or execute the code (CSS, HTML, JS, PHP, Shortcode, Tracking code etc) on specific pages of your Enfold website.
Code Block Settings
In the Code Block option, you can add the custom code in the Code Block Content area and select the required options to suit your purpose.
- Code Wrapper Element
- Escape HTML Code
- Disable Shortcode Processing
- Deactivate schema.org markup
Code Snippets
How to use the snippets?
- Add the CSS Snippets to Enfold child theme styles.
- Add the JS and PHP scripts to your child theme functions.php file.
- Shortcodes can be used in a text area in pages, posts, sliders and widget areas. Enable debug mode to view the page shortcode.
- Enable custom CSS class name support to add your class names to the theme elements.
- Disable script merging and clear the cache to view the changes on the frontend.
NOTE: If the code snippets produce a different result on your site, it may be because the settings on your site are different or due to any customization already added to achieve a similar result. Please try removing your customization if any and feel free to change the values in the example codes to suit your design.
Shortcode
Shortcode for a code block element:
[av_codeblock wrapper_element='' wrapper_element_attributes='' escape_html='' deactivate_shortcode='' deactivate_wrapper='' av-desktop-hide='' av-medium-hide='' av-small-hide='' av-mini-hide='' av_uid='av-6sv14']
Customization
How to style background for the code?
By default, the code wrapped in the pre tag will have the alternate grey and white background color. To custom style the pre tag and change the background style of your code please use the following CSS:
/*---------------------------------------- // CSS - Code block custom style //--------------------------------------*/ pre
Adding CSS/Code for a single page
The code block element makes it easy to add custom code can be added to a single page or specific pages. Lets take a look at adding custom CSS code to a single page or post:
- Drag and drop the code block element on the page or post to which you like to add custom code.
- Add your CSS code using the style tag (as shown below) in the code block content area ( do not wrap the code in pre tag or select Escape HTML Code).
Troubleshoot
Code in code block section does not display.
After inserting your code especially if it is a script for a referral ad and it does not show up it is most likely due to the Adblock plugin installed on your browser.
Resource
Useful plugins for advanced code highlighting
Resource
Contributed video:
Contents
This site uses cookies. By continuing to browse the site, you are agreeing to our use of cookies.
Cookie and Privacy Settings
We may request cookies to be set on your device. We use cookies to let us know when you visit our websites, how you interact with us, to enrich your user experience, and to customize your relationship with our website.
Click on the different category headings to find out more. You can also change some of your preferences. Note that blocking some types of cookies may impact your experience on our websites and the services we are able to offer.
These cookies are strictly necessary to provide you with services available through our website and to use some of its features.
Because these cookies are strictly necessary to deliver the website, refusing them will have impact how our site functions. You always can block or delete cookies by changing your browser settings and force blocking all cookies on this website. But this will always prompt you to accept/refuse cookies when revisiting our site.
We fully respect if you want to refuse cookies but to avoid asking you again and again kindly allow us to store a cookie for that. You are free to opt out any time or opt in for other cookies to get a better experience. If you refuse cookies we will remove all set cookies in our domain.
We provide you with a list of stored cookies on your computer in our domain so you can check what we stored. Due to security reasons we are not able to show or modify cookies from other domains. You can check these in your browser security settings.
Check to enable permanent hiding of message bar and refuse all cookies if you do not opt in. We need 2 cookies to store this setting. Otherwise you will be prompted again when opening a new browser window or new a tab.
These cookies collect information that is used either in aggregate form to help us understand how our website is being used or how effective our marketing campaigns are, or to help us customize our website and application for you in order to enhance your experience.
If you do not want that we track your visit to our site you can disable tracking in your browser here:
We also use different external services like Google Webfonts, Google Maps, and external Video providers. Since these providers may collect personal data like your IP address we allow you to block them here. Please be aware that this might heavily reduce the functionality and appearance of our site. Changes will take effect once you reload the page.
Google reCaptcha Settings:
Vimeo and Youtube video embeds:
The following cookies are also needed - You can choose if you want to allow them:
You can read about our cookies and privacy settings in detail on our Privacy Policy Page.