Coding Style & Requirements
This guide was designed to help you write well-organized code that is easier to read, edit, and troubleshoot for functional and responsive designs. Although it includes many widely adopted conventions, it is by no means intended to be the universal "best" or "correct" approach. After taking this class, you may consider other styles based on personal preference or professional expectations. However, keep in mind that much of the criteria below directly impacts your project grades.
This page is divided into two sections:
-
General Requirements: These requirements apply to all projects and will be assessed by the instructor according to the associated rubric in Canvas. They will be provided in every project description, as well.
-
Suggestions: This section includes other miscellaneous suggestions. While they do not directly affect your assignment grades, implementing these suggestions may help improve your results.
General Requirements
All projects are expected to meet the following criteria.
Naming Conventions
- Name
classandidattributes in HTML and CSS using the "kebab-case" convention, where lowercase words are separated by hyphens. - Name
functionsandvariablesin JavaScript using the "camelCase" convention, where each word (except the first) begins with a capital letter and contains no spaces or hyphens. - Do not use spaces or special characters in file names other than hyphens
-or underscores_. Rename long or cryptic file names as needed. For example,dog.jpeg, notneom-9E9NsEiUGxg-unsplash.jpeg.
Files and Images
- Each project submission must include at minimum an
index.html,styles.css, andmain.jsfile. These are provided in the template repository, so do not delete or rename them. - All images must be in
jpegorwebpformat with a resolution no larger than3840pixels in width or2160pixels in height. Logos, icons, or other vector drawings may be insvgformat. Use Squoosh or another image editor to resize or compress images before adding them to your project. - All image files must be stored inside an
imagesfolder.
This:
Not this:
Comments
- Use comments to explain and organize your code. At a minimum, comments should be used to separate code into logical sections.
HTML
- Do not change the provided
<link>or<script>tags in the<head>ofindex.html. These are already configured to correctly loadstyles.cssandmain.js. You may add additional<link>or<script>tags in the<head>if needed (e.g., to load external fonts). - Do not write any styling or scripting in your HTML. This includes:
- Inline
styleor event attributes (such asonclick) <style>tags<script>tags outside of the<head><script>tags that do not reference asrcfile
This:
const counterButton = document.getElementById('counter-button');
counterButton.addEventListener('click', addCount);
Not this:
CSS
- All colors should use HEX or RGB values taken directly from wireframe designs. Minor variations are acceptable.
- Example HEX code:
#e9967a - Example RGB code:
rgb(233, 150, 122) - Always specify font families. If you are not using a "web safe font," make sure the font is properly loaded in your project. If you are using a single font family for all elements, apply it globally in the
mainselector. - Layouts should adapt to any window width from
320pxand above. A default media query breakpoint at640pxis provided instyles.cssto make size and layout adjustments as needed for wider windows.
JavaScript
- Use the keywords
letorconstwhen declaring variables in JavaScript. Do not usevar. - Do not use a
DOMContentLoadedevent listener orwindow.onloadproperty. Many online examples may suggest these, but they are unnecessary and may cause conflicts in later projects. - Do not use the ternary
?operator for conditionals. This is often suggested in AI-generated code. Useifstatements instead.
This:
Not this:
var favoriteFruit = 'apple';
var birthYear = 1981;
message = birthYear < 2000 ? 'No spring chicken!' : '';
Suggestions
Formatting
In both CodePen and VS Code, js-beautify is used for HTML formatting and Prettier is used for CSS and JavaScript formatting. Formatting occurs automatically on save if both platforms are configured according to the Setup guide.
Provided your code is completely and properly structured (e.g., not missing tags or brackets), the following formatting should apply:
- Blank lines and indentation
- Proper spacing around operators, parentheses, and curly brackets
- Semicolons at the end of statements in JavaScript
- Double quotes
"replaced with single quotes'in JavaScript
HTML
<body>
<header>
<h1>My Big Project</h1>
</header>
<main>
<p>Tons of great content here.</p>
<img src="headshot.webp" alt="Headshot" />
</main>
<footer>
<p>Like and subscribe!</p>
</footer>
</body>
CSS
JavaScript
let favoriteFruit = 'apple';
if (favoriteFruit === 'apple') {
declareLove();
}
function declareLove() {
console.log('I like apples, too!');
}
Relative Units
Prioritize using relative units in CSS whenever possible to improve responsiveness and consistency in sizing and spacing. Common examples of relative units include:
%- Percentage relative to the parent element.ch- The width of a character in the element's font size.rem- Relative to the root element's font size (the browser default).
When using absolute units, such as px, aim for consistent and logical increments e.g., multiples of 2 or 10. Adopting CSS variables can help with this by allowing you to define and adjust size denominations in one place.
You can read more about valid absolute and relative units in this MDN Web Docs reference.
CSS Style Selectors
Use element and class selectors, as well as CSS nesting, instead of id selectors in CSS. This approach helps reinforce a "separation of concerns" by reserving id attributes for assigning HTML elements to variables in JavaScript.