Follow Me

Simple Pre Code with Selection

Simple Pre Code with Selection
For those of you who often share blogger tutorials, of course you are familiar with the application of the code, either HTML, CSS, JavaScript or jQuery in the posts. And as a place to apply these codes to posts, you need to wrap each code with the pre tag.

Examples of HTML code declarations wrapped in pre tags

<pre><code><button>Press Me!</button></code></pre>

The above example is the HTML code that was previously parsed and a simple example of the application of the pre tag. The appearance of the pre tag can actually be developed by your friend so that the appearance is different from the usual one. As in the post I have shared by adding a plug-in / script to the Syntax Highlighter Prism on Blogger.

Well here I will try to share the concept of a pre-simpler tag without the need for an external script that you can apply to the blog.

Ok just go ahead, please follow the simple steps below:

Open Blogger> Template> Click Edit HTML> Save the CSS code below before ]]> </b:skin> or </style>


/* CSS Simple Pre Code */
pre {
background: #fff;
white-space: pre;
word-wrap: break-word;
overflow: auto;
}

pre.code {
margin: 20px 25px;
border: 1px solid #d9d9d9;
border-radius: 2px;
position: relative;
box-shadow: 0 1px 1px rgba(0,0,0,.08);
}

pre.code label {
font-family: sans-serif;
font-weight: normal;
font-size: 13px;
color: #444;
position: absolute;
left: 1px;
top: 16px;
text-align: center;
width: 60px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
pointer-events: none;
}

pre.code code {
font-family: "Inconsolata","Monaco","Consolas","Andale Mono","Bitstream Vera Sans Mono","Courier New",Courier,monospace;
display: block;
margin: 0 0 0 60px;
padding: 15px 16px 14px;
border-left: 1px solid #d9d9d9;
overflow-x: auto;
font-size: 13px;
line-height: 19px;
color: #444;
}

pre::after {
content: "double click to selection";
padding: 0;
width: auto;
height: auto;
position: absolute;
right: 18px;
top: 14px;
font-size: 12px;
color: #aaa;
line-height: 20px;
overflow: hidden;
-webkit-backface-visibility: hidden;
transition: all 0.3s ease;
}

pre:hover::after {
opacity: 0;
visibility: visible;
}

pre.code-css code {
color: #0288d1;
}

pre.code-html code {
color: #558b2f;
}

pre.code-javascript code {
color: #f57c00;
}

pre.code-jquery code {
color: #78909c;
}

Next save the script below before the closing tag </body>

<script type='text/javascript'>
//<![CDATA[
//Pre Auto Selection
$('i[rel="pre"]').replaceWith(function() {
return $('<pre><code>' + $(this).html() + '</code></pre>');
});
var pres = document.querySelectorAll('pre,kbd,blockquote');
for (var i = 0; i < pres.length; i++) {
pres[i].addEventListener("dblclick", function () {
var selection = getSelection();
var range = document.createRange();
range.selectNodeContents(this);
selection.removeAllRanges();
selection.addRange(range);
}, false);
}
//]]>
</script>

The script above is used to automatically select each friend double-click on the area wrapped in the pre, kbd, and blockquote tags.

After that save the template.

For application to posts, please add the code below on the HTML post tab

<pre class='code code-html'><label>HTML</label><code>... kode HTML (yang telah diparse) di sini ...</code></pre>

<pre class='code code-css'><label>CSS</label><code>... kode CSS di sini ...</code></pre>

<pre class='code code-javascript'><label>JS</label><code>... kode JavaScript di sini ...</code></pre>

<pre class='code code-jquery'><label>Jquery</label><code>... kode jQuery di sini ...</code></pre>


If you want to display pre tags with dark colors, please use this code

/* CSS Simple Pre Code */
pre {
background: #333;
white-space: pre;
word-wrap: break-word;
overflow: auto;
}

pre.code {
margin: 20px 25px;
border-radius: 4px;
border: 1px solid #292929;
position: relative;
}

pre.code label {
font-family: sans-serif;
font-weight: bold;
font-size: 13px;
color: #ddd;
position: absolute;
left: 1px;
top: 15px;
text-align: center;
width: 60px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
pointer-events: none;
}

pre.code code {
font-family: "Inconsolata","Monaco","Consolas","Andale Mono","Bitstream Vera Sans Mono","Courier New",Courier,monospace;
display: block;
margin: 0 0 0 60px;
padding: 15px 16px 14px;
border-left: 1px solid #555;
overflow-x: auto;
font-size: 13px;
line-height: 19px;
color: #ddd;
}

pre::after {
content: "double click to selection";
padding: 0;
width: auto;
height: auto;
position: absolute;
right: 18px;
top: 14px;
font-size: 12px;
color: #ddd;
line-height: 20px;
overflow: hidden;
-webkit-backface-visibility: hidden;
transition: all 0.3s ease;
}

pre:hover::after {
opacity: 0;
visibility: visible;
}

pre.code-css code {
color: #91a7ff;
}

pre.code-html code {
color: #aed581;
}

pre.code-javascript code {
color: #ffa726;
}

pre.code-jquery code {
color: #4dd0e1;
}



So regarding the implementation of Simple Pre Code with Selection, hopefully useful.
Next Post Previous Post
No Comment
Add Comment
comment url