HTML…One of the easiest thing to learn in programming. Most of the newbies and even kids step into programming picking up HTML. They learn, they build some web pages but a lot of developers even experienced one make some silly mistake while writing the code for frontend. Making these silly mistakes not only annoy other developers (when they need to make some changes) but also hurts your main site and drive the end-user away. We are going to mention some common and best practices which you should follow to write a clean and clear HTML code.
Following some common practices makes debugging easier and saves a lot of time. It also helps in search engine optimization as well.
1. Use Proper Document Structure With Doctype
HTML has nature that it will still render your markup correctly even if you forget to mention some elements such as <html>, <head>, <body> and <!DOCTYPE html>. You will see correct result in your browser as you want but that doesn’t mean you will find the same result in every browser. To avoid this issue it’s a good habit to follow a proper document struture with correct doctype. Doctype is the first thing to mention in HTML document. You can choose the right doctype from the link W3.org.
2. Close the Tags
To avoid validation and compatibility issue don’t forget to close all the tags in your code. Today most of the text editors come up with features that close the HTML tags automatically still, it’s a good practice(and definitely for final check) to make sure that you don’t miss any parent or nested tag which is not closed. In HTML5 it’s optional to close HTML tags but according to W3C specification, you should close all the HTML tags to avoid any validation error in the future.
3. Write Tags in Lowercase
Make a habit to use lowercase for all the tags, attributes, and values in HTML code. It is an industry-standard practice and it also makes your code much more readable. Capitalizing the tags won’t affect the result in your browser but it’s a good practice to write elements in lowercase. Writing your code in lowercase is easy and it also looks cleaner.
4. Add Image Attributes
When you add an image in your HTML code don’t forget to add alt attribute for validation and accessibility reasons. Also, make sure that you choose the right description for the alt attribute. Search engines rank your page lower as a result when you don’t mention alt attributes with an image. It’s also good to mention the height and width of the image. It reduces flickering because the browser can reserve space for the image before loading.
5. Avoid Using Inline Styles
A lot of newbies make this mistake that they add inline-style in HTML tags. It makes your code complicated and difficult to maintain. Make a habit to keep your styles separate from HTML mark-up. Using inline styles can create so many problems. It makes your code cluttered, unreadable and hard to update or maintain. Separating HTML with CSS also helps other developers to make changes in code very easily.
6. Use a Meaningful Title and Descriptive Meta Tags
HTML title really matters a lot when it comes to search engine optimization or ranking of your page. Always try to make your title as meaningful as possible. The title of your HTML page appears in the google search engine result page and the indexing of your site relies on it.
A meta description tells the user what the page is all about, so make it descriptive that clearly specify the purpose of your website or page. Avoid using repeated words and phrases. When a user types some keyword in search engine bar that keyword is picked up by the search engine spiders and then it is used to find the relevant page for users based on the matching keyword used in meta tags.
7. Use Heading Elements Wisely
Heading tags also play a crucial role in making your website search engine friendly. HTML has 6 different types of heading tags that should be used carefully. Learn to use <h1> to <h6> elements to denote your HTML’s content hierarchy also make sure that you use only one h1 per page. In W3C specs it is mentioned that your page content should be described by a single tag so you can add your most important text within h1 such as article title or blog post.
8. Always Use Right HTML Elements
A lot of beginners make a common mistake using wrong HTML elements. They need to learn with time that which element should be used where. We recommend you to learn about all the HTML elements and use them correctly for a meaningful content structure. For example instead of using <br> between two paragraphs you can use CSS margin and/or padding properties. Learn when to use <em> and when to use <i> (both looks the same), learn when to use <b> and when to use <strong> (both looks the same). It comes with practice and when you keep your eyes on good code written by other developers. Another good example is given below..
9. Proper Use of Indentation
it is important to give proper space or indentation in your HTML code to make it more readable and manageable. Avoid writing everything in one line. It makes your code cluttered and flat. When you use the nested element give proper indentation so that users can identify the beginning and end of the tag. A code that follows proper formatting is easy to change and looks nice to other developers. It’s a good coding practice that reduces development time as well.
10. Validate Your Code
Last but not least make a habit to validate your HTML code frequently. Validating your code is a great piece of work and helps in finding the difficult issues. You can download W3C markup validation or Firefox developers tool bar to validate your site by url. Validators are very helpful in detecting the errors and resolving them.
- 5 Programming Tips For Beginners
- Top 10 Projects For Beginners To Practice HTML and CSS Skills
- Best Security Practices in Node.js
- 7 React Best Practices Every Web Developer Should Follow
- Geeks Coding Challenge [GCC 2019] - A Three Day Coding Challenge By GeeksforGeeks
- Android App Development Fundamentals for Beginners
- How To Learn ReactJS: A Complete Guide For Beginners
- Best Books to Learn Java for Beginners and Experts
- Best Books to Learn Python for Beginners and Experts in 2019
- Best Books To Learn Machine Learning For Beginners And Experts
- 7 Common Mistakes That Beginners Should Avoid While Learning to Code
- I don't like Coding. What should I do to Get a Job in IT?
- Why Should I Learn Coding?
- PHP | Coding Standards
- Secure coding - What is it all about?
- How To Approach A Coding Problem ?
- Coding Standards and Guidelines
- Coding the Financial Market
- Building App For Google Assistant Without Any Coding
- Coding good - Important criterias
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.