Cheat Sheet covering best practices for HTML, CSS, and JavaScript
Cheat Sheet covering best practices for HTML, CSS, and JavaScript
• Semantic Markup:
Use elements like <header>, <footer>, <main>, <section>, and <article> to clearly
define the document structure.
• Document Structure:
• Begin with a valid <!DOCTYPE html>.
• Include <html lang="en"> and appropriate <head> metadata.
• Accessibility:
• Use alt attributes for images.
• Apply ARIA roles when needed.
• Responsive Setup:
Include the viewport meta tag:
html
CopyEdit
<meta name="viewport" content="width=device-width, initial-scale=1.0">
css
CopyEdit
/* Mobile styles */
@media only screen and (max-width: 480px) {
/* Mobile-specific CSS */
}
/* Tablet styles */
@media only screen and (min-width: 481px) and (max-width: 768px) {
/* Tablet-specific CSS */
}
/* Desktop styles */
@media only screen and (min-width: 769px) {
/* Desktop-specific CSS */
}
• Coding Conventions:
• Prefer shorthand properties where possible.
• Keep specificity low to avoid conflicts.
• Avoid overusing !important.
css
CopyEdit
@media only screen and (max-width: 480px) {
/* Mobile-specific styles */
}
• Breakpoints Strategy:
Define breakpoints based on target devices:
o Mobile: 320px–480px
o Tablet: 481px–768px
o Desktop: 769px and above
• Viewport Meta Tag:
Ensure your HTML includes:
html
CopyEdit
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Debugging Tips
• Browser Developer Tools:
• Use the Elements panel to inspect and modify HTML/CSS live.
• Utilize the Console for logging errors (console.log(), console.error()) and
debugging.
• Breakpoints & Debugger Statements:
• Set breakpoints in your JavaScript code via DevTools to step through code execution.
• Insert debugger; in your code to pause execution.
• Linting & Validation:
• Run tools like ESLint for JavaScript and Stylelint for CSS to catch syntax and style
issues.
• Validate your HTML/CSS using online validators.
• Cross-Browser Testing:
Test your website on multiple browsers and devices to catch compatibility issues.
• Missing DOCTYPE:
Can trigger quirks mode and lead to inconsistent rendering.
• Improper Nesting/Unclosed Tags:
Causes unpredictable layout and rendering issues.
• Deprecated Elements:
Using old or unsupported tags can affect accessibility and SEO.
CSS Errors
• Specificity Wars:
Overly specific selectors (or excessive use of !important) make overrides difficult.
• Syntax Issues:
Missing semicolons or braces can break style rules.
• Cascade Problems:
Unintended cascading of styles due to poorly ordered rules.
• Browser Compatibility:
Newer CSS features may not be supported uniformly; consider fallbacks or vendor
prefixes.
JavaScript Errors