code tag in html with examples





  1. The HTML <code> tag is used for indicating a piece of code.
  2. The <code> tag surrounds the code being marked up.
  3. The <code> is rendered in a fixed width font.
  4. Similar tags are kbd for specifying keyboard entry text, samp for specifying sample output from a computer program, pre for defining preformatted text, and var for defining a variable.
  5. The <code> tag is supported in all major browsers.


Example :

     <html>
     <head>
     <title>Code</title>
     </head>
     <body>
     <p>This is text without cite effect.</p>
     <code>This is text with cite effect.</code><br/>
     <code>var myArray=new Array(3);</code>
     </body>
     </html>
     

Demo :





In the above example, we have rendered code tag. The text inside the code renders like a piece of code.



Code And Style :



<html>
<head>
<title>CodeStyle</title>
<style type="text/css">
code
{
color:blue;
font-size:20px;
}
</style>
</head>
<body>
<p>This is text without code effect.</p>
<code>This is text with code effect.</code>
</body>
</html>

Demo :




In the above example, we have used CSS with the code tag.



Code and Javascript :



<html>
<head><title>CodeJavascript</title>
<script type="text/javascript">
function code()
{
var element=document.createElement("code");
var text=document.createTextNode("This is code effect.");
element.appendChild(text);
document.getElementById("code").appendChild(element);
}
</script>
</head>
<body>
<div id="code">
</div>
<input type="button" id="b1" value="Press" onclick="code()">
</body>
</html>
     

Demo :





In the above example, we have used javascript to create a code tag and render it on UI.

0 comments:

Post a Comment