views:

165

answers:

5

I have found that my HTML is, to be honest, very clunky. Small, simple pages are OK. But there comes a point when between indenting and the kinds of tags I have, it's impossible to keep lines short. Is there a W3C (or otherwise "official" or well accepted) formatting guide for clean, maintainable HTML? If not, what suggestions can the community provide?

+2  A: 

My advice is to not even worry about it. HTML and XML, unlike most languages, are almost trivial to re-indent to any style the programmer wants. In HTML I use width-2 tabs, and regularly have sections that run off the edge of the editor window. There's not really any way to avoid that unless you just skip indenting sections of the document.

John Millikin
I was afraid of that. Poorly formatted code really bothers me. I guess I'll learn to live with it.
Thomas Owens
It's not poorly formatted -- deep indentation in C-style languages is a symptom of poor factoring / design, but HTML *can't* be refactored, so deep indentation is an unavoidable result of a large document structure.
John Millikin
Ah, that is a good point.
Thomas Owens
When deep indentation become cumbersome to work with the file, I start breaking the file into smaller, more manageable chunks and use server-side includes to piece it back together. You can always paste it back in later if you need to.
Diodeus
Indentation is pretty trivial to deal with. I always focus more on trying to minimize the amount of markup I'm adding. If I can get the desired effect with CSS, I often go for that rather than the mess of tables and spacers that you often see, It makes a big difference in readability.
Toji
Disagreed. You should worry about formatting and style. However, I must admit that HTML is a difficult nut to crack...
Yarik
+1  A: 

I just indent properly and don't worry about line length.

If you are using a templating system then you don't have any content in the view anyway, so the lines will only have variables and tags.

Basically keep as much out of the HTML as possible and it's less of a problem.

Rich Bradshaw
+2  A: 

I typically put every "block" type element on a new line, and indent every time with it. Even when there is a short amount of code, it helps to where elements are changing. The only items I keep on the same line are bold and italic tags and the occasional div and span tag when I'm formatting a small chunk of code, so you'd see something like:

<p>
   This is my <b>fancy</b> code block of text here. This will
   typically wrap forever and <div class="SarcasticStyle">EVER</div> until
   I run out of things to say.
   <br/>
   Yeah, I think that's it.
</p>
Dillie-O
A: 

I used to be very strict about whitespace in my HTML (2 spaces for each level of indention, no tabs, group sections with a space in between, etc). After getting into more server side programming though, I found that it was harder and harder to keep the formatting looking just right. Dreamweaver is notorious for this; I can't seem to get the app from using tab characters, which ends up making the source look different in other text editors.

I've stopped worrying so much and simply try to improve bits of markup that I am currently working on. Indenting seems like a much bigger deal in programming languages than HTML.

Gothael
A: 

i have to disagree with the answer you have chosen. you should be worrying about what your html looks like. with all the fancy ide's out there it seems developers just aren't as cautious about html as they should be. the resulting html that is output to the browser has a significant impact both on accessibility and search engine optimization for your website. how many people building websites out there even know what a <label> tag does? it ties a label to a form element which is extremely important for visually impaired people who "view" the web via a screen reader.

if your html is nested so deeply that you are more than about ten indentation levels in you really need to think about refactoring your design--just as you would if your program code got that way.

back to the real question, getting your html to look nice is easy if you remember one rule; break out tags into multiple lines. take an <a> tag for example. while it is true that you can't do much about the url itself, outside of using a shortening service like tr.im, break the tag into multiple lines like this:

<a href="http://rads.stackoverflow.com/amzn/click/1430209879"More Joel on Software: Further Thoughts on Diverse and
          Occasionally Related Matters That Will Prove of Interest to
          Software Developers, Designers, and ... Luck, Work with Them
          in Some Capacity (Pro) (Paperback)">Another Joel on Software book</a>

you can also look into methods that will do html formatting automatically. stand alone applications like tidy ui and htmltrim can do this, or you can get the htmltidy library which can be integrated with your application.

cowgod