Search code examples
htmlcsslayouthtmlelements

How to include code as text in an HTML page


I'm creating a webpage and I want to include code in the webpage. The problem I'm having is that I can't figure out a way to indent lines of code without having lots of different tags.

For example if I wanted to display the code:

for (int i = 0; i < 6; i++) {
    for (int j = 0; j < 5; j++) {
        for (int k = 0; k < 5; k++) {
            //Do something
         }
    }
}

At the moment I'm displaying it using <p class="tab1">,<p class="tab2>, etc. and in the CSS file using .tab { margin-left: 40px; }, .tab { margin-left: 80px; }, etc. But surely there is a simpler way to indent code on the webpage than having to make a separate CSS class for each indent? Does anyone have any ideas? It would be much appreciated, Thanks.


Solution

  • Use the pre-tag :

    <pre>
    for (int i = 0; i < 6; i++) {
        for (int j = 0; j < 5; j++) {
            for (int k = 0; k < 5; k++) {
                //Do something
             }
        }
    }
    </pre>