Magnetis markup code styleguide.
- Disclaimer
- Doctype
lang
- IE compatibility mode
- Encoding
- CSS and JavaScript includes
- Naming
- Attribute order
- Boolean attributes
- Quotes
- Self closing tags
- Optional closing tags
- Inline JavaScript
- Semantics
- Whitespace
- Templates
x-template
id
overclass
- Links
This document is inspired by Mark Otto's Code Guide, Harry Roberts's coding style and Idiomatic HTML.
- Follow HTML5 doctype to enforce standards and a more consistent rendering throughout browsers.
<!doctype html>
<html>
<head></head>
<body></body>
</html>
- You should specify the language of the documents. We currently only support
pt-br
.
<html lang="pt-br">
</html>
Internet Explorer supports the use of a document compatibility <meta>
tag to specify what version of IE the page should be rendered. You should force edge mode.
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
- Use
UTF-8
as de default encoding of HTML documents.
<head>
<meta charset="utf-8">
</head>
- There's no need to specify the
type
attribute of include tags.
<!-- External CSS -->
<link rel="stylesheet" href="path/to/external.css">
<!-- Inline CSS -->
<style>
/* ... */
</style>
<!-- JavaScript -->
<script src="path/to/script.js"></script>
- Always use lowercase tag and attribute names.
<!-- Bad -->
<div id="advertisingModal" class="ModalBase dropShadowFX"></div>
<!-- Good -->
<div id="advertising-modal" class="modal-base drop-shadow-fx"></div>
- The order of attributes should look like:
- Element
id
and/orname
- JavaScript class name
js-selector
- Generic classes (clearfix, grid stuff, etc)
- Other classes
- States
- Data attributes
data-*
- Other attributes
- Boolean attributes
- Element
<!-- Bad -->
<div class="clearfix is-opaque js-button button grid-size-2" id="btn" hidden data-fx="fade"></div>
<!-- Good -->
<div id="btn" class="js-button clearfix grid-size-2 button is-opaque" data-fx="fade" hidden></div>
- Do not add values to boolean attributes.
<!-- Bad -->
<input type="checkbox" value="Bar" checked="checked">Foo</input>
<!-- Good -->
<input type="checkbox" value="Bar" checked>Foo</input>
<!-- Bad -->
<input type="submit" disabled="disabled">Send email</input>
<!-- Good -->
<input type="submit" disabled>Send email</input>
- Always use double quotes
"
to wrap attribute values.
<!-- Bad -->
<button id=my-button data-fx='fade'>Fade!</button>
<!-- Good -->
<button id="my-button" data-fx="fade">Fade!</button>
- Always close self closing tags with a slash
/
, no matter what.
<!-- Bad -->
<p>Lorem ipsum<br>dolor sit</p>
<!-- Good -->
<p>Lorem ipsum<br />dolor sit</p>
<!-- Bad -->
<img src="path/to/image.png">
<!-- Good -->
<img src="path/to/image.png" />
- Always close optional closing tags
<!-- Bad -->
<ul>
<li>Once
<li>Upon
<li>A Time
</ul>
<!-- Good -->
<ul>
<li>Once</li>
<li>Upon</li>
<li>A Time</li>
</ul>
- Avoid writing JavaScript at all in HTML documents as they're hard to spot.
- Strive to maintain HTML standards and semantics, but not at the expense of practicality. Use the least amount of markup with whenever possible.
- Use soft tabs set to 2 spaces and never mix spaces with tabs.
- Always add an empty line at the end of your file.
- It's cool to break multiple attributes to enforce code readability. Just keep the following format:
<div
class="modal"
id="main-modal"
style="display: none;"
hidden
>
</div>
- When closing self closing tags, always add a single space before
/>
.
<!-- Bad -->
<img src="doge.png"/>
<!-- Good -->
<img src="doge.png" />
- Template elements should have their
type
value set tox-template
.
<!-- Bad -->
<script type="text-template" id="box-template">
<div class="box"></div>
</script>
<!-- Good -->
<script type="x-template" id="box-template">
<div class="box"></div>
</script>
- Template elements should be identified through
id
instead ofclass
. Addtemplate
suffix as well.
<!-- Bad -->
<script type="x-template" class="js-component">
<p>Component</p>
</script>
<!-- Good -->
<script type="x-template" id="component-template">
<p>Component</p>
</script>