Logo

dev-resources.site

for different kinds of informations.

7 Old-School Practices in HTML Should Be Avoided

Published at
9/20/2024
Categories
css
a11y
validation
html
Author
bogdanfromkyiv
Categories
4 categories in total
css
open
a11y
open
validation
open
html
open
Author
14 person written this
bogdanfromkyiv
open
7 Old-School Practices in HTML Should Be Avoided

Just some daily habits in HTML coding we all should get rid off.

Using type attribute for <script> and <style>

❌ Stop writing this:

<style type="text/css">/* CSS styles here */</style>
<script type="text/javascript">/* JS code here */</script>
Enter fullscreen mode Exit fullscreen mode

✅ Instead, you should simply write:

<style>/* CSS styles here */</style>
<script>/* JS code here */</script>
Enter fullscreen mode Exit fullscreen mode

No need to specify the type attribute, modern browsers are smart enough to understand that <style> is for CSS, <script> is for JavaScript and V for Vendetta!

Accordion (FAQ) block requires JS code

Easy! Watch:

<details>
  <summary>To be or not to be?</summary>
  <div>
    That is the question!
  </div>
</details>
Enter fullscreen mode Exit fullscreen mode

Here’s the result:

And with some extra styles:

Ok, but what about smooth open/close action, you might ask.

It’s tricky, so if you need fancy smooth animation you should include some JavaScript.

But if you can deal with simple one-direction CSS transition, here you go:

Use <header> and <footer> only once per page

Despite some people thinking that <header> and <footer> elements represent header and footer of the page respectively, it’s a false statement.

Those elements relate to the nearest sectioning content, which means being a child of one of the following elements: <article>, <aside>, <nav>, and <section>.

Thus, you can (actually, you should ) use <header> and/or <footer> elements when you create another section on your page.

Here’s an example of an author info box with the right elements:

Using frameborder="0" to remove border around <iframe>

It’s a deprecated attribute of an <iframe> element. Just like you (hopefully) don’t use align attribute to handle text alignment, you should avoid using frameborder attribute.

Instead, use the CSS property border to handle <iframe> borders.

Nonetheless, when you right click on a YouTube or Vimeo video and then click on “Copy embed code” you get an <iframe> element with that frameborder="0" attribute. So after applying embed code to your page make sure to remove that frameborder attribute and set border property to your <iframe> in CSS.

Include support for IE 8

❌ The following script provides basic HTML5 styling for Internet Explorer 6–8:

<!--[if lt IE 9]>
  <script src="scripts/html5shiv.js"></script>
<![endif]-->
Enter fullscreen mode Exit fullscreen mode

Seriously, it’s a voice from the past. Stop including supporting scripts for Internet Explorer. And I’m not only talking about IE 8, but ALL Explorers! Even Microsoft stopped support for IE 11 in June 2022. So you should.

Randomly choose heading tags

I hope you already know there should be only one <h1> tag on the page as for the primary title. But what about the rest of the heading tags <h2>…<h6> ?

We used to choose them approximately, depending on the size of the heading from the design. So if we have some heading, which looks small in the Figma mockup, we want to set the <h4> tag, for example. Despite the previous heading tag on the page being <h1>!

That’s very bad practice. Your markup becomes invalid and messy for screen readers.

Remember, accessibility matters!

You should provide heading tags depending on page structure, not page design. And those tags should be in a descending order. So if your last heading tag was <h2>, the next one should be either <h2> (if it’s another section on the page) or <h3> if it’s a heading for the child section of <h2> heading.

❌ So DO NOT do like this (randomly provided heading tags):

<h1>Primary title</h1>
<section>
  <h3>Section title</h3>
  <p>Some text inside the section</p>
</section>
<section>
  <h3>Another section title</h3>
  <p>Some text inside the section</p>
  <div>
    <h5>Subtitle</h5>
    <p>Some text inside the section</p>
  </div>
</section>
Enter fullscreen mode Exit fullscreen mode

✅ Keep the order of your heading tags and logic structure (keep the descending order of headings):

<h1>Primary title</h1>
<section>
  <h2>Section title</h2>
  <p>Some text inside the section</p>
</section>
<section>
  <h2>Another section title</h2>
  <p>Some text inside the section</p>
  <div>
    <h3>Subtitle</h3>
    <p>Some text inside the section</p>
  </div>
</section>
Enter fullscreen mode Exit fullscreen mode

Adding ="1" for boolean attributes

I mean such attributes, as disabled for inputs, loop, muted or autoplay for videos etc. The mere fact that those attributes present means they are equal to true.

It won’t affect functionality, so your input will still be disabled, but this is an error for the W3C validator and just an unnecessary piece of code.

❌ So DO NOT code like this:

<input type="text" value="This input is disabled" disabled="1" />

✅ Keep it simple:

<input type="text" value="This input is disabled" disabled />


If you find this article helpful — don’t hesitate to like, subscribe and leave your thoughts in the comments 😊


Read more posts on my Medium blog


Thanks for reading!

Stay safe and peace to you!

validation Article's
30 articles in total
Favicon
"yup" is the new extra virgin olive oil
Favicon
JavaScript Email Validation Regex: Ensuring Accuracy in User Inputs
Favicon
Simplifying JSON Validation with Ajv (Another JSON Validator)
Favicon
Dynamic string validation using go's text/template package
Favicon
Practical Email Validation Using JavaScript: Techniques for Web Developers
Favicon
Validation in Spring REST Framework (SRF)
Favicon
Terraform Validation Rules: Best Practices & Examples
Favicon
Zod for TypeScript Schema Validation: A Comprehensive Guide
Favicon
AI Image Validation Solutions: A Game-Changer for Quality Control
Favicon
Transform zod error into readable error response
Favicon
⛔Stop Excessive Validation, you are ruining my life hack!
Favicon
zod vs class-validator & class-transformer
Favicon
Animated Login Form with Validation Using HTML, CSS & JavaScript
Favicon
How to work with regular expressions
Favicon
User Form Validation in HTML CSS and JavaScript | JavaScript Form Validation
Favicon
Simplify Form Validation with FormGuardJS: A Lightweight and Flexible Solution
Favicon
moment.js Alternative - Luxon
Favicon
Validating REST requests in a NestJS application and displaying errors in Angular application forms
Favicon
Implement data validation in FastAPI
Favicon
The Definitive Guide to the Constraint Validation API
Favicon
Env-Core: An Easy Way to Validate Environment Variables in Node.js Projects
Favicon
File-Type Validation in Multer is NOT SAFE🙃
Favicon
Guaranteed GxP Compliance: Mastering Validation Testing Strategies
Favicon
Assuring Excellence: The Crucial Benefits of GxP Validation Testing
Favicon
7 Old-School Practices in HTML Should Be Avoided
Favicon
Implement data validation in Spring Boot
Favicon
Implement data validation in Express
Favicon
An easy way to validate DTO's using Symfony attributes
Favicon
Implement data validation in .NET
Favicon
Mastering Data Validation in NestJS: A Complete Guide with Class-Validator and Class-Transformer

Featured ones: