HTML5
Best Practices
<header>
<nav>
<ul>
<li>My Nav Item 1</li>
<li>My Nav Item 2</li>
</ul>
</nav>
</header>
<section class="article">
<h1>Lorem Ipsum...</h1>
</section>
<footer>
<p>Copyright & Legal...</p>
</footer>
<div id="header">
<div id="nav">
<ul>
<li>My Nav Item 1</li>
<li>My Nav Item 2</li>
</ul>
</div>
</div>
<div class="article">
<h1>Lorem Ipsum...</h1>
</div>
<div id ="footer">
<p>Copyright & Legal...</p>
</div>
<div id="my-data-div">
<input type="hidden" name="my-data-div-count" value="1" />
<input type="hidden" name="my-date-div-color" value="red" />
</div>
<div id="my-data-div" data-count=1 data-color="red"></div>
<div id="header">
<div id="nav">
<ul>
<li>...</li>
</ul>
</div>
</div>
<div id="sidebar-1">
<div id="nav">
<ul>
<li>...</li>
</ul>
</div>
</div>
<div id="sidebar-2">
<div id="nav">
<ul>
<li>...</li>
</ul>
</div>
</div>
...
<header>
<nav>
<ul>
<li>...</li>
</ul>
</nav>
</header>
<div class="sidebar" id="main-sidebar">
<nav>
<ul>
<li>...</li>
</ul>
</nav>
</div>
<div class="sidebar" id="user-sidebar">
<nav>
<ul>
<li>...</li>
</ul>
</nav>
</div>
...
Will it be used to apply styles, or describe a type of component? Use a class.
Will it be used to identify this particular element only, ever? Ok, use an ID.
When using a class for javascript purposes (i.e. a selector), prefix it with js-.
Yes, create an extra class. Yes, it will pay off.
<div class="click-area js-click-area"></div>
Always
be
<div id="foo">
<ul>
<li class="list-item"></li>
</ul>
</div>
<div class="wrapper">
<div class="container">
<div id="important-container">
</div>
</div>
</div>
<form id="user-form">
<input name="i" type="text" />
</form>
A language that you write instead of writing CSS, because CSS is pretty awful.
header {
nav {
background-color: #ccc;
color: #000;
a {
color: #333;
}
}
}
header nav {
background-color: #ccc;
color: #000;
}
header nav a {
color: #333;
}
header {
&.alternate {
background-color: #000;
color: #fff;
a {
color: #ccc;
}
}
}
header.alternate {
background-color: #000;
color: #fff;
}
header.alternate a {
color: #ccc;
}
$red: #F00;
a {
color: $red;
}
a {
color: #F00;
}
@mixin rounded-corners( $radius: 5px; ){
border-radius: $radius;
}
.my-div {
@include rounded-corners(10px);
}
.my-other-div {
@include rounded-corners;
}
.my-div {
border-radius: 10px;
}
.my-other-div {
border-radius: 5px;
}
.error {
border: 1px #f00;
background-color: #fdd;
}
.seriousError {
@extend .error;
border-width: 3px;
}
.error, .seriousError {
border: 1px #f00;
background-color: #fdd;
}
.seriousError {
border-width: 3px;
}
When in doubt, just write it in CSS.
<script src="js/handlebars.js"></script>
<script id="entry-template" type="text/x-handlebars-template">
<div class="entry">
<h1>{{title}}</h1>
<div class="body">
{{body}}
</div>
</div>
</script>
var template = document.querySelector("#entry-template");
var templateFunction = Handlebars.compile(template.innerHTML);
document.querySelector("#my-component").innerHTML = templateFunction({
title: 'Hello',
body: 'World'
});