Słowik
Słowik

Reputation: 163

How to insert indents into html code tag?

I'm trying to put some python code in html document. I am using code tag. Example

<code>
    for iris, species in zip(irises, classification):
        if species == 0:
            print(f'Flower {iris} is Iris-setosa')
</code>

The problem is, page doesn't see new lines and indents. I can handle new lines with br tag but I didn't find anything to make indent. I tried pre tag, but I have to remove all indents in html document, and with several indents in it, it starts to look very ugly. Propably I could use &nbsp; but using 4,8 or 12 in one line doesn't seem to be good idea. Is there anything else I can do to format my code?

Upvotes: 1

Views: 321

Answers (1)

Mechanic
Mechanic

Reputation: 5380

The parser will ignore white space characters in the source code. you can may <pre> or <br/> or fake it with CSS. but the solution you proposed is also valid and works, but as you stated it is ugly. if you are going for that you can use &Tab; char and it will create a tab indent; it makes more sense to use it instead of 4 x &nbsp; but you still need to put it inside <pre> tag to avoid being ignored by the parser.

Upvotes: 1

Related Questions