Geek Slack

Introduction to HTML
    About Lesson



    HTML Computer Code Elements


    HTML Computer Code Elements

    HTML provides several elements to display computer code in a web page. These elements help to represent code snippets and emphasize the difference between code and other text.

    1. The <code> Element

    The <code> element is used to define a piece of computer code.

    Example:

    <p>Here is some <code>inline code</code> in a paragraph.</p>

    2. The <pre> Element

    The <pre> element represents preformatted text. Text within this element is displayed in a fixed-width font, and whitespace is preserved.

    Example:

    <pre>
    function helloWorld() {
        console.log("Hello, World!");
    }
    </pre>

    3. The <samp> Element

    The <samp> element is used to represent sample output from a computer program.

    Example:

    <p>The program output is: <samp>Error: File not found</samp></p>

    4. The <kbd> Element

    The <kbd> element is used to define keyboard input.

    Example:

    <p>To save the file, press <kbd>Ctrl</kbd> + <kbd>S</kbd>.</p>

    5. The <var> Element

    The <var> element is used to define a variable in programming or mathematical expressions.

    Example:

    <p>The value of <var>x</var> is 10.</p>

    6. Combining Code Elements

    These elements can be combined to create detailed and structured representations of code and its components.

    Example:

    <pre>
    <code>
    function calculateArea(<var>width</var>, <var>height</var>) {
        return width * height; <samp>// returns the area</samp>
    }
    console.log(calculateArea(5, 10)); <samp>// 50</samp>
    </code>
    </pre>

    Conclusion

    Using HTML computer code elements, you can effectively display code snippets, program output, keyboard input, and variables. These elements enhance the readability and structure of your content when presenting code-related information on web pages.