SlideShare a Scribd company logo
HTML5, CSS, JavaScript
Style guide and coding conventions
HTML5, CSS, JavaScript
Style guide and coding conventions
Priyanka Wadhwa
Software Consultant
Knoldus Software LLP
Priyanka Wadhwa
Software Consultant
Knoldus Software LLP
Agenda
● Why we need coding Standards?
● HTML5 coding conventions
● CSS style guide
● JavaScript Coding standards
● Why we need coding Standards?
● HTML5 coding conventions
● CSS style guide
● JavaScript Coding standards
What is a need for coding standards ?
Problem
When we learn a new language, we begin to code in a specific style. A style we want and
not the one that has been suggested to us.
Now what?
➢ Can the other person actually read your code? Is it spaced out clearly?
➢ Will he be able to work out what’s happening without needing to look at every line?
➢ Will he be able to know how and why are you commenting the work?
➢ And many more questions may arise…
HTML5, CSS, JavaScript Style guide and coding conventions
Need for coding standards
Solution
Coding conventions are style guidelines for programming.
A coding standards document tells developers how they should write their code. Instead of
each developer coding in their own preferred style, they should write all code to the
standards outlined in the document.
Large projects need to be coded in a consistent style – Not just to make the code easier to
understand, but also to ensure that any developer who looks at the code will know what
naming conventions say and what should he expect from the entire application.
HTML5 coding conventions
HTML5 coding conventions
● HTML5 doctype
Enforce standards mode and more consistent rendering in every browser possible with this simple doctype at
the beginning of every HTML page.
<!DOCTYPE html>
<html>
<head>
</head>
</html>
● Language Attribute
A lang attribute is required to be specified on the root html element, giving the document's language.
<html lang=”en-us”>
<!→-→-->
</html>
● Character encoding
Ensure proper rendering of your content by declaring an explicit character encoding.
<head>
<meta charset=”UTF-8”>
</head>
● Internet Explorer compatibility mode
Internet Explorer supports the use of a document compatibility <meta> tag to specify what version of
IE the page should be rendered as.
<meta http-equiv=”X-UA-Compatibile” content=”IE=Edge”>
● CSS and JavaScript includes
<link rel='stylesheet' href='guide.css'>
<style>/* …… */</style>
HTML5 Coding conventions
● Attribute order
As per the standards, we are required to follow the following order of HTML attributes -
➢ class
➢ id, name
➢ data-*
➢ src, for, type, href, value
➢ title, alt
➢ role, aria-*
<a class=”….” id=”…..” data-toggle=”model” href=”#”>
Example link
</a>
<img src=”...” alt=”...”/>
HTML5 Coding conventions
● Boolean attributes
Requires no declaration.
<input type=”text” disabled>
<input type=”checkbox” value=”1” checked>
<select>
<option value=”1” selected>1</option>
</select>
● Reducing markup
Produce less HTML, avoid superfluous parent elements when writing HTML.
<span class=”avatar”>
<img src=”...”>
</span>
can be re-written as -
<img class=”avatar” src=”...”>
HTML5 Coding conventions
Some more basic standards -
● Always use the alt attribute with images. It is important when the image cannot be viewed.
● Try to avoid code lines longer than 80 characters.
● Spaces and equal signs - HTML5 allows spaces around equal signs. But space-less is easier
to read, and groups entities better together.
● Use the attributes name in lower case letters.
● Close each and every HTML elements even empty HTML elements also.
.
HTML5 Coding conventions
CSS style guide
CSS style guide
Am I following the correct
coding conventions here?
CSS style guide
Am I following the correct
coding conventions here?
And what about me?
CSS style guide
Am I following the correct
coding conventions here?
And what about me?
CSS style guide
Am I following the correct
coding conventions here?
And what about me?
Let's find out the correct way of coding CSS , following the correct coding conventions.
CSS style guide
Syntax
● Use soft tabs with two spaces—they're the only way to guarantee code renders the same in
any environment.
● When grouping selectors, keep individual selectors to a single line.
● Include one space before the opening brace of declaration blocks for legibility.
● Place closing braces of declaration blocks on a new line.
● Include one space after : for each declaration.
● Each declaration should appear on its own line for more accurate error reporting.
● End all declarations with a semi-colon. The last declaration's is optional, but your code is
more error prone without it.
● Comma-separated property values should include a space after each comma (e.g., box-
shadow).
CSS style guide
● Don't include spaces after commas within rgb(), rgba() values. This helps differentiate
multiple color values from multiple property values.
● Don't prefix property values or color parameters with a leading zero (e.g., .5 instead of 0.5
and -.5px instead of -0.5px).
● Lowercase all hex values, e.g., #fff. Lowercase letters are much easier to read.
● Use shorthand hex values when available, e.g., #fff instead of #ffffff.
● Quote attribute values in selectors, e.g., input[type="text"]. They’re only optional in some
cases, and it’s a good practice for consistency.
● Avoid specifying units for zero values, e.g., margin: 0; instead of margin: 0px;.
CSS style guide
● Declaration order
Following properties should be grouped together :
➢ Positioning (position, top, right)
➢ Box model (display, float, width, height)
➢ Typographic (font, line-height, color)
➢ Visual (background-color, border)
➢ Misc (opacity)
Positioning comes first because it can remove an element from the normal flow of the
document and override box model related styles. The box model comes next as it dictates a
component's dimensions and placement.
CSS style guide
● Don't use of @import
From a page speed standpoint, @import from a CSS file should almost never be used, as it
can prevent stylesheets from being downloaded concurrently.
There are occasionally situations where @import is appropriate, but they are generally the
exception, not the rule.
CSS style guide
● Media query placement
➢ Bundling the media query in a separate file is not preferable.
➢ Decision to add it at the end of the CSS file or placing close to their relevant rule sets
depends upon your CSS file.
CSS style guide
● Single declarations
Consider removing line breaks for readability and faster editing. Any rule set with multiple
declarations should be split to separate lines.
Single-line declaration
Multi-line declaration
CSS style guide
● Shorthand notation
Strive to limit use of shorthand declarations to instances where you must explicitly set all the
available values. Common overused shorthand properties include:
➢ padding
➢ margin
➢ font
➢ background
➢ border
➢ Border-radius
Often times we don't need to set all the values a shorthand property represents.
For example, HTML headings only set top and bottom margin, so when necessary, only
override those two values. Excessive use of shorthand properties often leads to sloppier code
with unnecessary overrides and unintended side effects.
CSS style guide
● Comments
Code is written and maintained by people. Ensure your code is descriptive, well
commented, and approachable by others. Great code comments convey context or
purpose. Do not simply reiterate a component or class name.
CSS style guide
● Class names
Do keep the following points in mind before giving class names to the HTML elements -
➢ Keep classes lowercase and use dashes (not underscores or camelCase). Dashes serve as natural
breaks in related class (e.g., .btn and .btn-danger).
➢ Avoid excessive and arbitrary shorthand notation. .btn is useful for button, but .s doesn't mean
anything.
➢ Keep classes as short and succinct as possible.
➢ Use meaningful names; use structural or purposeful names over presentational.
➢ Prefix classes based on the closest parent or base class.
➢ Use .js-* classes to denote behavior (as opposed to style), but keep these classes out of your CSS.
Class to denote behavior : .js-calculate-price
Some good class names : .sequence { … }, .sequence-header { … }, .important { … }
Not so good class names : .s { … }, .header { … }, .red { … }
CSS style guide
● Selectors
Keep the following in mind before using nested CSS selectors -
➢ Use classes over generic element tag for optimum rendering performance.
➢ Avoid using several attribute selectors (eg., [class^="..."]) on commonly occurring
components. Browser performance is known to be impacted by these.
➢ Keep selectors short and strive to limit the number of elements in each selector to three.
➢ Scope classes to the closest parent only when necessary (e.g., when not using prefixed
classes).
CSS style guide
● CSS quotations
Use single ('') rather than double ("") quotation marks for attribute selectors or property
values.
Do not use quotation marks in URI values (url()).
@import url(“//www.google.com/css”);
html {
font-family: “open sans”, arial, sans-serif;
}
@import url(//www.google.com/css);
html {
font-family: 'open sans', arial, sans-serif;
}
JavaScript coding standards
JS coding standards
● Variable Names
Use of letters with camelCase in naming the variables.
✔ firstName = “Knoldus”;
✔ price = 9.90;
✗ middlename = “Softwares”;
● Spaces around operators
Spaces should be used to differentiate operators and also after commas.
✔ var x = y + z;
✔ var values = [“knoldus” , “software”];
✗ var values=[“knoldus”,“software”];
JS coding standards
● Code Indentation
4 spaces for indentation of code block -
function toCelsius(fahrenheit) {
return (5 / 9) * (fahrenheit – 32);
}
* Do not use tabs for indentation Different editors may treat it differently.
● Statement Rules
Simple statements end with a semicolon. Like – declaring a variable
var person = {
firstName: “Knoldus”,
lastName: “Softwares”,
};
JS coding standards
Complex/ compound statements must follow the following -
➢ Opening bracket at the end of first line,
➢ Space before opening bracket.
➢ Closing bracket on a new line, without leading spaces.
➢ And, complex statements doesn't end with a semicolon.
function tocelsius(fahrenheit) {
return (5 / 9) * (fahrenheit - 32);
}
* same applies for the loop and conditional statements.
JS coding standards
● Object Rules -
➢ Placing opening brackets on the same as the object name.
➢ Using colon plus one space between each property and its value.
➢ No adding of comma at the last property-value pair.
➢ Placing of closing brackets on a new line, without leading spaces.
➢ Never forget to end an object with a semicolon.
var person = {
firstName: "Knoldus",
lastName: "Softwares"
};
Short objects can be compressed and written in one line using the spaces only between their properties -
var person = {firstName:"Knoldus", lastName:"Softwares"};
JS coding standards
● Line length < 80 characters
If a javascript statement does not fit on one line, then the best place to break it, is after an
operator or comma.
document.getElementById("knolx").innerHTML =
"Hello Knolders.";
● Naming Conventions
Remember to maintain consistency in the naming convention for all your code.
➢ All variables and functions must be in camelCase.
➢ Global variables and Constants to be written in UPPERCASE.
JS coding standards
● Line length < 80 characters
If a javascript statement does not fit on one line, then the best place to break it, is after an
operator or comma.
document.getElementById("knolx").innerHTML =
"Hello Knolders.";
● Naming Conventions
Remember to maintain consistency in the naming convention for all your code.
➢ All variables and functions must be in camelCase.
➢ Global variables and Constants to be written in UPPERCASE.
Should we use hyp-hens, camelCase or under_scores in variable names?
JS coding standards
Hyphens (-)
● HTML5 attributes can have hyphens (data-element, data-count).
● CSS uses hyphens in property names (background-color, padding-left, font-size)
● JavaScript names does not allow use of hyphens. As they can conflict with subtraction
operator.
Underscores ( _ )
● Underscores (date_of_birth) are mostly used in databases or in documentation. So, we
prefer not to go with using underscores.
PascalCase
● It is often used by C Programmers.
CamelCase
● This is used by javascript, jquery and in various JS libraries.
References
● https://google.github.io/styleguide/htmlcssguide.xml
● http://codeguide.co
● http://www.w3schools.com/js/js_conventions.asp
● http://www.w3schools.com/html/html5_syntax.asp
Thank you :)Thank you :)

More Related Content

PDF
Intro to HTML and CSS basics
Eliran Eliassy
 
PPTX
Javascript 101
Shlomi Komemi
 
ODP
CSS Basics
Sanjeev Kumar
 
PDF
Intro to HTML & CSS
Syed Sami
 
PPT
Coding standard
Shwetketu Rastogi
 
PPTX
Cascading style sheet
Michael Jhon
 
PPTX
Introduction to CSS
Folasade Adedeji
 
PPT
Introduction to Cascading Style Sheets (CSS)
Chris Poteet
 
Intro to HTML and CSS basics
Eliran Eliassy
 
Javascript 101
Shlomi Komemi
 
CSS Basics
Sanjeev Kumar
 
Intro to HTML & CSS
Syed Sami
 
Coding standard
Shwetketu Rastogi
 
Cascading style sheet
Michael Jhon
 
Introduction to CSS
Folasade Adedeji
 
Introduction to Cascading Style Sheets (CSS)
Chris Poteet
 

What's hot (20)

PDF
Workshop 21: React Router
Visual Engineering
 
ODP
Html
irshadahamed
 
PPT
CSS for Beginners
Amit Kumar Singh
 
PDF
CSS3 Media Queries
Russ Weakley
 
PDF
JavaScript - Chapter 4 - Types and Statements
WebStackAcademy
 
PPT
Introduction to JavaScript (1).ppt
MuhammadRehan856177
 
PPT
Css
Manav Prasad
 
PPT
Cascading Style Sheets (CSS) help
casestudyhelp
 
PDF
Introduction to CSS3
Seble Nigussie
 
PPTX
Html 5
manujayarajkm
 
PPTX
New Elements & Features in CSS3
Jamshid Hashimi
 
PPT
cascading style sheet ppt
abhilashagupta
 
PDF
Sass - Getting Started with Sass!
Eric Sembrat
 
PDF
Introduction to HTML and CSS
Mario Hernandez
 
PPSX
Coding standard
FAROOK Samath
 
PPT
Clean Code summary
Jan de Vries
 
PDF
Javascript
Momentum Design Lab
 
PPTX
Introduction to HTML+CSS+Javascript.pptx
KADAMBARIPUROHIT
 
PDF
Basic Details of HTML and CSS.pdf
Kalyani Government Engineering College
 
Workshop 21: React Router
Visual Engineering
 
CSS for Beginners
Amit Kumar Singh
 
CSS3 Media Queries
Russ Weakley
 
JavaScript - Chapter 4 - Types and Statements
WebStackAcademy
 
Introduction to JavaScript (1).ppt
MuhammadRehan856177
 
Cascading Style Sheets (CSS) help
casestudyhelp
 
Introduction to CSS3
Seble Nigussie
 
New Elements & Features in CSS3
Jamshid Hashimi
 
cascading style sheet ppt
abhilashagupta
 
Sass - Getting Started with Sass!
Eric Sembrat
 
Introduction to HTML and CSS
Mario Hernandez
 
Coding standard
FAROOK Samath
 
Clean Code summary
Jan de Vries
 
Introduction to HTML+CSS+Javascript.pptx
KADAMBARIPUROHIT
 
Basic Details of HTML and CSS.pdf
Kalyani Government Engineering College
 
Ad

Viewers also liked (20)

ODP
Functional programming in Javascript
Knoldus Inc.
 
ODP
Mailchimp and Mandrill - The ‘Hominidae’ kingdom
Knoldus Inc.
 
ODP
Introduction to BDD
Knoldus Inc.
 
ODP
Deep dive into sass
Knoldus Inc.
 
ODP
Akka Finite State Machine
Knoldus Inc.
 
ODP
Mandrill Templates
Knoldus Inc.
 
PDF
Fast dataarchitecture
Knoldus Inc.
 
ODP
Introduction to Quasiquotes
Knoldus Inc.
 
ODP
Introduction to Knockout Js
Knoldus Inc.
 
ODP
Lambda Architecture with Spark
Knoldus Inc.
 
ODP
Cassandra - Tips And Techniques
Knoldus Inc.
 
ODP
Introduction to Apache Cassandra
Knoldus Inc.
 
ODP
Event sourcing with Eventuate
Knoldus Inc.
 
ODP
Walk-through: Amazon ECS
Knoldus Inc.
 
ODP
Getting started with typescript and angular 2
Knoldus Inc.
 
ODP
Introduction to Structured Streaming
Knoldus Inc.
 
ODP
Introduction to AWS IAM
Knoldus Inc.
 
PPTX
Petex 2016 Future Working Zone
Andrew Zolnai
 
PDF
How the web was won
Andrew Zolnai
 
ODP
Drilling the Async Library
Knoldus Inc.
 
Functional programming in Javascript
Knoldus Inc.
 
Mailchimp and Mandrill - The ‘Hominidae’ kingdom
Knoldus Inc.
 
Introduction to BDD
Knoldus Inc.
 
Deep dive into sass
Knoldus Inc.
 
Akka Finite State Machine
Knoldus Inc.
 
Mandrill Templates
Knoldus Inc.
 
Fast dataarchitecture
Knoldus Inc.
 
Introduction to Quasiquotes
Knoldus Inc.
 
Introduction to Knockout Js
Knoldus Inc.
 
Lambda Architecture with Spark
Knoldus Inc.
 
Cassandra - Tips And Techniques
Knoldus Inc.
 
Introduction to Apache Cassandra
Knoldus Inc.
 
Event sourcing with Eventuate
Knoldus Inc.
 
Walk-through: Amazon ECS
Knoldus Inc.
 
Getting started with typescript and angular 2
Knoldus Inc.
 
Introduction to Structured Streaming
Knoldus Inc.
 
Introduction to AWS IAM
Knoldus Inc.
 
Petex 2016 Future Working Zone
Andrew Zolnai
 
How the web was won
Andrew Zolnai
 
Drilling the Async Library
Knoldus Inc.
 
Ad

Similar to HTML5, CSS, JavaScript Style guide and coding conventions (20)

PDF
Web Design for Literary Theorists II: Overview of CSS (v 1.0)
Patrick Mooney
 
PDF
Part 2 in depth guide on word-press coding standards for css &amp; js big
eSparkBiz
 
PDF
4. Web Technology CSS Basics-1
Jyoti Yadav
 
PDF
Advanced Web Programming Chapter 8
RohanMistry15
 
PPTX
Introduction to HTML+CSS+Javascript.pptx
AliRaza899305
 
PDF
CascadingStyleSheets in ONESHOT By DeathCodeYT .pdf
balbirnainnain496
 
PPT
CSS Overview
Doncho Minkov
 
PPT
CSS Methodology
Zohar Arad
 
PDF
css-tutorial
tutorialsruby
 
PDF
css-tutorial
tutorialsruby
 
ODP
Cascading Style Sheets - Part 02
Hatem Mahmoud
 
PPTX
Css.html
Anaghabalakrishnan
 
PPTX
HTML5 and CSS Fundamentals MOOC Course College Presentation
KuchBhi90
 
PPTX
Introduction to HTML+CSS+Javascript.pptx
PedroGonzalez915307
 
PPTX
Introduction to HTML+CSS+Javascript.pptx
SadiaBaig6
 
PPTX
GDG On Campus NBNSCOE Web Workshop Day 1 : HTML & CSS
udaymore742
 
PPTX
wd project.pptx
dsffsdf1
 
PPT
Basics Of Css And Some Common Mistakes
sanjay2211
 
PPTX
gdg Introduction to HTML-CSS-Javascript.pptx
saurabh45tiwari
 
PPTX
Introduction to HTML-CSS-Javascript.pptx
deepak37877
 
Web Design for Literary Theorists II: Overview of CSS (v 1.0)
Patrick Mooney
 
Part 2 in depth guide on word-press coding standards for css &amp; js big
eSparkBiz
 
4. Web Technology CSS Basics-1
Jyoti Yadav
 
Advanced Web Programming Chapter 8
RohanMistry15
 
Introduction to HTML+CSS+Javascript.pptx
AliRaza899305
 
CascadingStyleSheets in ONESHOT By DeathCodeYT .pdf
balbirnainnain496
 
CSS Overview
Doncho Minkov
 
CSS Methodology
Zohar Arad
 
css-tutorial
tutorialsruby
 
css-tutorial
tutorialsruby
 
Cascading Style Sheets - Part 02
Hatem Mahmoud
 
HTML5 and CSS Fundamentals MOOC Course College Presentation
KuchBhi90
 
Introduction to HTML+CSS+Javascript.pptx
PedroGonzalez915307
 
Introduction to HTML+CSS+Javascript.pptx
SadiaBaig6
 
GDG On Campus NBNSCOE Web Workshop Day 1 : HTML & CSS
udaymore742
 
wd project.pptx
dsffsdf1
 
Basics Of Css And Some Common Mistakes
sanjay2211
 
gdg Introduction to HTML-CSS-Javascript.pptx
saurabh45tiwari
 
Introduction to HTML-CSS-Javascript.pptx
deepak37877
 

More from Knoldus Inc. (20)

PPTX
Angular Hydration Presentation (FrontEnd)
Knoldus Inc.
 
PPTX
Optimizing Test Execution: Heuristic Algorithm for Self-Healing
Knoldus Inc.
 
PPTX
Self-Healing Test Automation Framework - Healenium
Knoldus Inc.
 
PPTX
Kanban Metrics Presentation (Project Management)
Knoldus Inc.
 
PPTX
Java 17 features and implementation.pptx
Knoldus Inc.
 
PPTX
Chaos Mesh Introducing Chaos in Kubernetes
Knoldus Inc.
 
PPTX
GraalVM - A Step Ahead of JVM Presentation
Knoldus Inc.
 
PPTX
Nomad by HashiCorp Presentation (DevOps)
Knoldus Inc.
 
PPTX
Nomad by HashiCorp Presentation (DevOps)
Knoldus Inc.
 
PPTX
DAPR - Distributed Application Runtime Presentation
Knoldus Inc.
 
PPTX
Introduction to Azure Virtual WAN Presentation
Knoldus Inc.
 
PPTX
Introduction to Argo Rollouts Presentation
Knoldus Inc.
 
PPTX
Intro to Azure Container App Presentation
Knoldus Inc.
 
PPTX
Insights Unveiled Test Reporting and Observability Excellence
Knoldus Inc.
 
PPTX
Introduction to Splunk Presentation (DevOps)
Knoldus Inc.
 
PPTX
Code Camp - Data Profiling and Quality Analysis Framework
Knoldus Inc.
 
PPTX
AWS: Messaging Services in AWS Presentation
Knoldus Inc.
 
PPTX
Amazon Cognito: A Primer on Authentication and Authorization
Knoldus Inc.
 
PPTX
ZIO Http A Functional Approach to Scalable and Type-Safe Web Development
Knoldus Inc.
 
PPTX
Managing State & HTTP Requests In Ionic.
Knoldus Inc.
 
Angular Hydration Presentation (FrontEnd)
Knoldus Inc.
 
Optimizing Test Execution: Heuristic Algorithm for Self-Healing
Knoldus Inc.
 
Self-Healing Test Automation Framework - Healenium
Knoldus Inc.
 
Kanban Metrics Presentation (Project Management)
Knoldus Inc.
 
Java 17 features and implementation.pptx
Knoldus Inc.
 
Chaos Mesh Introducing Chaos in Kubernetes
Knoldus Inc.
 
GraalVM - A Step Ahead of JVM Presentation
Knoldus Inc.
 
Nomad by HashiCorp Presentation (DevOps)
Knoldus Inc.
 
Nomad by HashiCorp Presentation (DevOps)
Knoldus Inc.
 
DAPR - Distributed Application Runtime Presentation
Knoldus Inc.
 
Introduction to Azure Virtual WAN Presentation
Knoldus Inc.
 
Introduction to Argo Rollouts Presentation
Knoldus Inc.
 
Intro to Azure Container App Presentation
Knoldus Inc.
 
Insights Unveiled Test Reporting and Observability Excellence
Knoldus Inc.
 
Introduction to Splunk Presentation (DevOps)
Knoldus Inc.
 
Code Camp - Data Profiling and Quality Analysis Framework
Knoldus Inc.
 
AWS: Messaging Services in AWS Presentation
Knoldus Inc.
 
Amazon Cognito: A Primer on Authentication and Authorization
Knoldus Inc.
 
ZIO Http A Functional Approach to Scalable and Type-Safe Web Development
Knoldus Inc.
 
Managing State & HTTP Requests In Ionic.
Knoldus Inc.
 

Recently uploaded (20)

PPTX
Services offered by Dynamic Solutions in Pakistan
DaniyaalAdeemShibli1
 
PPTX
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
PDF
A REACT POMODORO TIMER WEB APPLICATION.pdf
Michael624841
 
PPTX
Materi_Pemrograman_Komputer-Looping.pptx
RanuFajar1
 
PPTX
Hire Expert Blazor Developers | Scalable Solutions by OnestopDA
OnestopDA
 
PDF
Solar Panel Installation Guide – Step By Step Process 2025.pdf
CRMLeaf
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
Winning Business in a Slowing Economy, How CPQ helps Manufacturers Protect Ma...
systemscincom
 
PPTX
Presentation of Computer CLASS 2 .pptx
darshilchaudhary558
 
PDF
Exploring AI Agents in Process Industries
amoreira6
 
PPTX
What to Capture When It Breaks: 16 Artifacts That Reveal Root Causes
Tier1 app
 
PDF
Become an Agentblazer Champion Challenge
Dele Amefo
 
PPTX
Why Use Open Source Reporting Tools for Business Intelligence.pptx
Varsha Nayak
 
PDF
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
DOCX
The Five Best AI Cover Tools in 2025.docx
aivoicelabofficial
 
PDF
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Q-Advise
 
PDF
The Role of Automation and AI in EHS Management for Data Centers.pdf
TECH EHS Solution
 
PDF
Emergency Mustering solutions – A Brief overview
Personnel Tracking
 
PDF
Micromaid: A simple Mermaid-like chart generator for Pharo
ESUG
 
PDF
Microsoft Teams Essentials; The pricing and the versions_PDF.pdf
Q-Advise
 
Services offered by Dynamic Solutions in Pakistan
DaniyaalAdeemShibli1
 
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
A REACT POMODORO TIMER WEB APPLICATION.pdf
Michael624841
 
Materi_Pemrograman_Komputer-Looping.pptx
RanuFajar1
 
Hire Expert Blazor Developers | Scalable Solutions by OnestopDA
OnestopDA
 
Solar Panel Installation Guide – Step By Step Process 2025.pdf
CRMLeaf
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Winning Business in a Slowing Economy, How CPQ helps Manufacturers Protect Ma...
systemscincom
 
Presentation of Computer CLASS 2 .pptx
darshilchaudhary558
 
Exploring AI Agents in Process Industries
amoreira6
 
What to Capture When It Breaks: 16 Artifacts That Reveal Root Causes
Tier1 app
 
Become an Agentblazer Champion Challenge
Dele Amefo
 
Why Use Open Source Reporting Tools for Business Intelligence.pptx
Varsha Nayak
 
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
The Five Best AI Cover Tools in 2025.docx
aivoicelabofficial
 
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Q-Advise
 
The Role of Automation and AI in EHS Management for Data Centers.pdf
TECH EHS Solution
 
Emergency Mustering solutions – A Brief overview
Personnel Tracking
 
Micromaid: A simple Mermaid-like chart generator for Pharo
ESUG
 
Microsoft Teams Essentials; The pricing and the versions_PDF.pdf
Q-Advise
 

HTML5, CSS, JavaScript Style guide and coding conventions

  • 1. HTML5, CSS, JavaScript Style guide and coding conventions HTML5, CSS, JavaScript Style guide and coding conventions Priyanka Wadhwa Software Consultant Knoldus Software LLP Priyanka Wadhwa Software Consultant Knoldus Software LLP
  • 2. Agenda ● Why we need coding Standards? ● HTML5 coding conventions ● CSS style guide ● JavaScript Coding standards ● Why we need coding Standards? ● HTML5 coding conventions ● CSS style guide ● JavaScript Coding standards
  • 3. What is a need for coding standards ? Problem When we learn a new language, we begin to code in a specific style. A style we want and not the one that has been suggested to us. Now what? ➢ Can the other person actually read your code? Is it spaced out clearly? ➢ Will he be able to work out what’s happening without needing to look at every line? ➢ Will he be able to know how and why are you commenting the work? ➢ And many more questions may arise…
  • 5. Need for coding standards Solution Coding conventions are style guidelines for programming. A coding standards document tells developers how they should write their code. Instead of each developer coding in their own preferred style, they should write all code to the standards outlined in the document. Large projects need to be coded in a consistent style – Not just to make the code easier to understand, but also to ensure that any developer who looks at the code will know what naming conventions say and what should he expect from the entire application.
  • 7. HTML5 coding conventions ● HTML5 doctype Enforce standards mode and more consistent rendering in every browser possible with this simple doctype at the beginning of every HTML page. <!DOCTYPE html> <html> <head> </head> </html> ● Language Attribute A lang attribute is required to be specified on the root html element, giving the document's language. <html lang=”en-us”> <!→-→--> </html>
  • 8. ● Character encoding Ensure proper rendering of your content by declaring an explicit character encoding. <head> <meta charset=”UTF-8”> </head> ● Internet Explorer compatibility mode Internet Explorer supports the use of a document compatibility <meta> tag to specify what version of IE the page should be rendered as. <meta http-equiv=”X-UA-Compatibile” content=”IE=Edge”> ● CSS and JavaScript includes <link rel='stylesheet' href='guide.css'> <style>/* …… */</style> HTML5 Coding conventions
  • 9. ● Attribute order As per the standards, we are required to follow the following order of HTML attributes - ➢ class ➢ id, name ➢ data-* ➢ src, for, type, href, value ➢ title, alt ➢ role, aria-* <a class=”….” id=”…..” data-toggle=”model” href=”#”> Example link </a> <img src=”...” alt=”...”/> HTML5 Coding conventions
  • 10. ● Boolean attributes Requires no declaration. <input type=”text” disabled> <input type=”checkbox” value=”1” checked> <select> <option value=”1” selected>1</option> </select> ● Reducing markup Produce less HTML, avoid superfluous parent elements when writing HTML. <span class=”avatar”> <img src=”...”> </span> can be re-written as - <img class=”avatar” src=”...”> HTML5 Coding conventions
  • 11. Some more basic standards - ● Always use the alt attribute with images. It is important when the image cannot be viewed. ● Try to avoid code lines longer than 80 characters. ● Spaces and equal signs - HTML5 allows spaces around equal signs. But space-less is easier to read, and groups entities better together. ● Use the attributes name in lower case letters. ● Close each and every HTML elements even empty HTML elements also. . HTML5 Coding conventions
  • 13. CSS style guide Am I following the correct coding conventions here?
  • 14. CSS style guide Am I following the correct coding conventions here? And what about me?
  • 15. CSS style guide Am I following the correct coding conventions here? And what about me?
  • 16. CSS style guide Am I following the correct coding conventions here? And what about me? Let's find out the correct way of coding CSS , following the correct coding conventions.
  • 17. CSS style guide Syntax ● Use soft tabs with two spaces—they're the only way to guarantee code renders the same in any environment. ● When grouping selectors, keep individual selectors to a single line. ● Include one space before the opening brace of declaration blocks for legibility. ● Place closing braces of declaration blocks on a new line. ● Include one space after : for each declaration. ● Each declaration should appear on its own line for more accurate error reporting. ● End all declarations with a semi-colon. The last declaration's is optional, but your code is more error prone without it. ● Comma-separated property values should include a space after each comma (e.g., box- shadow).
  • 18. CSS style guide ● Don't include spaces after commas within rgb(), rgba() values. This helps differentiate multiple color values from multiple property values. ● Don't prefix property values or color parameters with a leading zero (e.g., .5 instead of 0.5 and -.5px instead of -0.5px). ● Lowercase all hex values, e.g., #fff. Lowercase letters are much easier to read. ● Use shorthand hex values when available, e.g., #fff instead of #ffffff. ● Quote attribute values in selectors, e.g., input[type="text"]. They’re only optional in some cases, and it’s a good practice for consistency. ● Avoid specifying units for zero values, e.g., margin: 0; instead of margin: 0px;.
  • 19. CSS style guide ● Declaration order Following properties should be grouped together : ➢ Positioning (position, top, right) ➢ Box model (display, float, width, height) ➢ Typographic (font, line-height, color) ➢ Visual (background-color, border) ➢ Misc (opacity) Positioning comes first because it can remove an element from the normal flow of the document and override box model related styles. The box model comes next as it dictates a component's dimensions and placement.
  • 20. CSS style guide ● Don't use of @import From a page speed standpoint, @import from a CSS file should almost never be used, as it can prevent stylesheets from being downloaded concurrently. There are occasionally situations where @import is appropriate, but they are generally the exception, not the rule.
  • 21. CSS style guide ● Media query placement ➢ Bundling the media query in a separate file is not preferable. ➢ Decision to add it at the end of the CSS file or placing close to their relevant rule sets depends upon your CSS file.
  • 22. CSS style guide ● Single declarations Consider removing line breaks for readability and faster editing. Any rule set with multiple declarations should be split to separate lines. Single-line declaration Multi-line declaration
  • 23. CSS style guide ● Shorthand notation Strive to limit use of shorthand declarations to instances where you must explicitly set all the available values. Common overused shorthand properties include: ➢ padding ➢ margin ➢ font ➢ background ➢ border ➢ Border-radius Often times we don't need to set all the values a shorthand property represents. For example, HTML headings only set top and bottom margin, so when necessary, only override those two values. Excessive use of shorthand properties often leads to sloppier code with unnecessary overrides and unintended side effects.
  • 24. CSS style guide ● Comments Code is written and maintained by people. Ensure your code is descriptive, well commented, and approachable by others. Great code comments convey context or purpose. Do not simply reiterate a component or class name.
  • 25. CSS style guide ● Class names Do keep the following points in mind before giving class names to the HTML elements - ➢ Keep classes lowercase and use dashes (not underscores or camelCase). Dashes serve as natural breaks in related class (e.g., .btn and .btn-danger). ➢ Avoid excessive and arbitrary shorthand notation. .btn is useful for button, but .s doesn't mean anything. ➢ Keep classes as short and succinct as possible. ➢ Use meaningful names; use structural or purposeful names over presentational. ➢ Prefix classes based on the closest parent or base class. ➢ Use .js-* classes to denote behavior (as opposed to style), but keep these classes out of your CSS. Class to denote behavior : .js-calculate-price Some good class names : .sequence { … }, .sequence-header { … }, .important { … } Not so good class names : .s { … }, .header { … }, .red { … }
  • 26. CSS style guide ● Selectors Keep the following in mind before using nested CSS selectors - ➢ Use classes over generic element tag for optimum rendering performance. ➢ Avoid using several attribute selectors (eg., [class^="..."]) on commonly occurring components. Browser performance is known to be impacted by these. ➢ Keep selectors short and strive to limit the number of elements in each selector to three. ➢ Scope classes to the closest parent only when necessary (e.g., when not using prefixed classes).
  • 27. CSS style guide ● CSS quotations Use single ('') rather than double ("") quotation marks for attribute selectors or property values. Do not use quotation marks in URI values (url()). @import url(“//www.google.com/css”); html { font-family: “open sans”, arial, sans-serif; } @import url(//www.google.com/css); html { font-family: 'open sans', arial, sans-serif; }
  • 29. JS coding standards ● Variable Names Use of letters with camelCase in naming the variables. ✔ firstName = “Knoldus”; ✔ price = 9.90; ✗ middlename = “Softwares”; ● Spaces around operators Spaces should be used to differentiate operators and also after commas. ✔ var x = y + z; ✔ var values = [“knoldus” , “software”]; ✗ var values=[“knoldus”,“software”];
  • 30. JS coding standards ● Code Indentation 4 spaces for indentation of code block - function toCelsius(fahrenheit) { return (5 / 9) * (fahrenheit – 32); } * Do not use tabs for indentation Different editors may treat it differently. ● Statement Rules Simple statements end with a semicolon. Like – declaring a variable var person = { firstName: “Knoldus”, lastName: “Softwares”, };
  • 31. JS coding standards Complex/ compound statements must follow the following - ➢ Opening bracket at the end of first line, ➢ Space before opening bracket. ➢ Closing bracket on a new line, without leading spaces. ➢ And, complex statements doesn't end with a semicolon. function tocelsius(fahrenheit) { return (5 / 9) * (fahrenheit - 32); } * same applies for the loop and conditional statements.
  • 32. JS coding standards ● Object Rules - ➢ Placing opening brackets on the same as the object name. ➢ Using colon plus one space between each property and its value. ➢ No adding of comma at the last property-value pair. ➢ Placing of closing brackets on a new line, without leading spaces. ➢ Never forget to end an object with a semicolon. var person = { firstName: "Knoldus", lastName: "Softwares" }; Short objects can be compressed and written in one line using the spaces only between their properties - var person = {firstName:"Knoldus", lastName:"Softwares"};
  • 33. JS coding standards ● Line length < 80 characters If a javascript statement does not fit on one line, then the best place to break it, is after an operator or comma. document.getElementById("knolx").innerHTML = "Hello Knolders."; ● Naming Conventions Remember to maintain consistency in the naming convention for all your code. ➢ All variables and functions must be in camelCase. ➢ Global variables and Constants to be written in UPPERCASE.
  • 34. JS coding standards ● Line length < 80 characters If a javascript statement does not fit on one line, then the best place to break it, is after an operator or comma. document.getElementById("knolx").innerHTML = "Hello Knolders."; ● Naming Conventions Remember to maintain consistency in the naming convention for all your code. ➢ All variables and functions must be in camelCase. ➢ Global variables and Constants to be written in UPPERCASE. Should we use hyp-hens, camelCase or under_scores in variable names?
  • 35. JS coding standards Hyphens (-) ● HTML5 attributes can have hyphens (data-element, data-count). ● CSS uses hyphens in property names (background-color, padding-left, font-size) ● JavaScript names does not allow use of hyphens. As they can conflict with subtraction operator. Underscores ( _ ) ● Underscores (date_of_birth) are mostly used in databases or in documentation. So, we prefer not to go with using underscores. PascalCase ● It is often used by C Programmers. CamelCase ● This is used by javascript, jquery and in various JS libraries.
  • 36. References ● https://google.github.io/styleguide/htmlcssguide.xml ● http://codeguide.co ● http://www.w3schools.com/js/js_conventions.asp ● http://www.w3schools.com/html/html5_syntax.asp

Editor's Notes

  • #8: specify a lang attribute on the root html element
  • #9: UTF-8 is for chracter encoding. It is capable of encoding all possivle characters, defined by unicode. UTF-8: UTF-8 is backwards compatible with ASCII. UTF-8 is the preferred encoding for e-mail and web pages 1 byte: Standard ASCII 2 bytes: Arabic, Hebrew, most European scripts (most notably excluding Georgian) 3 bytes: BMP 4 bytes: All Unicode characters UTF-16: capable of encoding the entire Unicode repertoire. UTF-16 is used in major operating systems and environments, like Microsoft Windows, Java and .NET. 2 bytes: BMP 4 bytes: All Unicode characters
  • #21: For instance, if stylesheet A contains the text: @import url(&amp;quot;stylesheetB.css&amp;quot;); then the download of the second stylesheet may not start until the first stylesheet has been downloaded. If, on the other hand, both stylesheets are referenced in &amp;lt;link&amp;gt; elements in the main HTML page, both can be downloaded at the same time. If both stylesheets are always loaded together, it can also be helpful to simply combine them into a single file.