简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM