Repository: mdo/code-guide
Branch: gh-pages
Commit: 6918226689cb
Files: 16
Total size: 39.3 KB
Directory structure:
gitextract_lhpf4avt/
├── .editorconfig
├── .gitignore
├── CNAME
├── LICENSE.md
├── README.md
├── _config.yml
├── _includes/
│ ├── footer.html
│ ├── header.html
│ ├── js.html
│ └── tweet-button.html
├── _layouts/
│ └── default.html
├── _sass/
│ ├── _ads.scss
│ ├── _og.scss
│ └── _syntax.scss
├── cg.scss
└── index.md
================================================
FILE CONTENTS
================================================
================================================
FILE: .editorconfig
================================================
# editorconfig.org
root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
[*.md]
trim_trailing_whitespace = false
================================================
FILE: .gitignore
================================================
# Ignore docs files
_gh_pages
_site
.jekyll-cache
.ruby-version
.sass-cache
# Numerous always-ignore extensions
*.diff
*.err
*.orig
*.log
*.rej
*.swo
*.swp
*.zip
*.vi
*~
# OS or Editor folders
.DS_Store
._*
Thumbs.db
.cache
.project
.settings
.tmproj
*.esproj
nbproject
*.sublime-project
*.sublime-workspace
.idea
# Komodo
*.komodoproject
.komodotools
# grunt-html-validation
validation-status.json
validation-report.json
# Folders to ignore
node_modules
bower_components
================================================
FILE: CNAME
================================================
codeguide.co
================================================
FILE: LICENSE.md
================================================
The MIT License (MIT)
Copyright (c) Mark Otto.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: README.md
================================================
<h3 align="center">
<a href="https://codeguide.co/">
<img src="code-guide-logo.png" alt="Code Guide logo" width="192" height="192">
</a>
<br>
Code Guide
</h3>
<p align="center" markdown="1">
Standards for developing consistent, flexible, and sustainable HTML and CSS.
<br>
<a href="https://codeguide.co"><strong>Start reading ☞</strong></a>
</p>
---
## Setup
Code Guide is built on Jekyll and hosted on GitHub Pages. To install Jekyll:
```sh
gem install jekyll
```
To start the local server:
```sh
jekyll serve
```
Open `localhost:4000` in your browser.
## License
Released under MIT by, and copyright, @mdo.
## Translations
Translations are maintained by their creators and may not always be up to date with the original here.
- [Chinese](http://zoomzhao.github.io/code-guide/) - Translated by [Zoom Zhao](https://github.com/ZoomZhao)
- [Chinese](http://codeguide.bootcss.com/) - Translated by [Wang Sai](https://github.com/wangsai)
- [Dutch](http://chunfeilung.github.io/code-guide/) - Translated by [Chun Fei Lung](https://github.com/chunfeilung)
- [French](http://pixelastic.github.io/code-guide/) - Translated by [Tim Carry](https://github.com/pixelastic/)
- [German](http://BackendButters.github.io/code-guide/) - Translated by [BackendButters](https://github.com/BackendButters)
- [German](https://philipbrembeck.github.io/code-guide/) - Translated by [Philip Brembeck](https://github.com/philipbrembeck)
- [Hindi](https://hidaytrahman.github.io/code-guide/) - Translated by [Hidaytullah Rahmani](https://github.com/hidaytrahman)
- [Indonesian](http://diagramatics.github.io/code-guide-id) - Translated by [Steven Sinatra](http://diagramatics.me)
- [Japanese](http://kia-king.com/code-guide/) - Translated by [Kia King Ishii](https://github.com/kiaking)
- [Korean](http://code-guide.aliencube.org/) - Translated by [Aliencube](https://github.com/aliencube)
- [Persian](https://cg.arash-hatami.ir/) - Translated by [Arash Hatami](https://github.com/hatamiarash7)
- [Polish](http://bondarewicz.github.io/code-guide/) - Translated by [Łukasz Bondarewicz](https://github.com/bondarewicz)
- [Portuguese](http://diegoeis.github.io/code-guide/) - Translated by [Diego Eis](http://tableless.com.br/)
- [Romanian](http://vmazare.github.io/code-guide-romanian/) - Translated by [Valeriu Mazare](https://github.com/vmazare)
- [Russian](http://sadcitizen.github.io/code-guide/) - Translated by [Eugene Abrosimov](https://github.com/sadcitizen)
- [Spanish](https://hansfelix.github.io/code-guide) - Translated by [Hans Felix](https://github.com/hansfelix)
- [Traditional Chinese](https://codeguide.intersection.tw) - Translated by [Yuming Cheung](https://github.com/ymcheung)
Have a translation you'd like to link to? Open a pull request to add it here. Be sure to keep it alphabetical.
<3
================================================
FILE: _config.yml
================================================
name: Code Guide by @mdo
description: Standards for developing consistent, flexible, and sustainable HTML and CSS.
url: https://codeguide.co
permalink: pretty
sass:
style: compressed
version: 4.0.0
og_image: https://raw.githubusercontent.com/mdo/code-guide/gh-pages/code-guide-social.png
================================================
FILE: _includes/footer.html
================================================
<footer class="footer">
<p>
Open sourced by <a href="https://twitter.com/mdo">@mdo</a> under MIT license. Copyright {{ site.time | date: '%Y' }}.
</p>
<ul class="quick-links">
<li>
<iframe class="github-btn" src="https://ghbtns.com/github-btn.html?user=mdo&repo=code-guide&type=watch&count=true" allowtransparency="true" frameborder="0" scrolling="0" width="100px" height="20px"></iframe>
</li>
<li>
<iframe class="github-btn" src="https://ghbtns.com/github-btn.html?user=mdo&repo=code-guide&type=fork&count=true" allowtransparency="true" frameborder="0" scrolling="0" width="98px" height="20px"></iframe>
</li>
</ul>
<ul class="quick-links">
<li class="follow-btn">
<a href="https://twitter.com/mdo" class="twitter-follow-button" data-link-color="#0069D6" data-show-count="true">Follow @mdo</a>
</li>
<li class="tweet-btn">
<a href="https://twitter.com/share" class="twitter-share-button" data-url="{{ site.url }}" data-count="horizontal" data-via="mdo">Tweet</a>
</li>
</ul>
</footer>
================================================
FILE: _includes/header.html
================================================
<header class="cg-masthead">
<div class="container">
<img class="cg-icon" src="code-guide-logo.png" alt="Code Guide logo">
<h1 class="cg-masthead-title">
Code Guide
</h1>
<p class="cg-masthead-subtitle">
{{ site.description }}
</p>
<p>
Created by <a href="https://markdotto.com">@mdo</a> · v{{ site.version }} · <a href="https://github.com/mdo/code-guide/">GitHub repo</a>
</p>
<script async src="https://cdn.carbonads.com/carbon.js?serve=CKYI52JU&placement=codeguide" id="_carbonads_js"></script>
</div>
</header>
================================================
FILE: _includes/js.html
================================================
<script src="https://platform.twitter.com/widgets.js"></script>
================================================
FILE: _includes/tweet-button.html
================================================
<div class="tweet-button">
<a href="https://twitter.com/share" class="twitter-share-button" data-size="large" data-count="horizontal" data-via="mdo" data-url="{{ site.url }}" data-text="{{ site.name }}:">Tweet</a>
</div>
================================================
FILE: _layouts/default.html
================================================
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>
{{ site.name }}
</title>
<link rel="preload" as="font" href="https://rsms.me/inter/font-files/Inter-Regular.woff2" type="font/woff2" crossorigin="anonymous">
<link rel="preload" as="font" href="https://rsms.me/inter/font-files/Inter-Italic.woff2" type="font/woff2" crossorigin="anonymous">
<link rel="preload" as="font" href="https://rsms.me/inter/font-files/Inter-SemiBold.woff2" type="font/woff2" crossorigin="anonymous">
<link rel="stylesheet" href="https://rsms.me/inter/inter.css">
<link rel="stylesheet" href="https://rsms.me/inter/inter-display.css">
<meta name="description" content="Standards for developing consistent, flexible, and sustainable HTML and CSS">
<link rel="canonical" href="https://codeguide.co">
<link rel="stylesheet" href="cg.css">
<link href="/favicon.ico" rel="icon" type="image/x-icon">
<link href="/code-guide.svg" rel="icon" type="image/svg+xml">
<meta property="og:image" content="{{ site.og_image }}">
<meta property="og:title" content="Code Guide">
<meta property="og:description" content="{{ site.description }}">
<meta property="og:image:width" content="2000">
<meta property="og:image:height" content="1000">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:creator" content="@mdo">
<meta name="twitter:title" content="Code Guide">
<meta name="twitter:description" content="{{ site.description }}">
<meta name="twitter:image" content="{{ site.og_image }}">
<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', 'UA-146052-17', 'codeguide.co');
ga('set', 'anonymizeIp', true);
ga('send', 'pageview');
</script>
<script async src="https://www.google-analytics.com/analytics.js"></script>
</head>
<body>
{% include header.html %}
<main class="container content">
{{ content }}
{% include footer.html %}
</main>
{% include js.html %}
</body>
</html>
================================================
FILE: _sass/_ads.scss
================================================
// stylelint-disable declaration-no-important, selector-max-id
//
// Carbon ads
//
#carbonads {
position: static;
display: block;
max-width: 400px;
padding: 15px 15px 15px 160px;
margin: 2rem 0 0;
overflow: hidden;
font-size: .8125rem;
line-height: 1.4;
text-align: left;
background-color: var(--body-bg);
border: 1px solid var(--border-color);
border-radius: .5rem;
a {
color: var(--body-color);
text-decoration: none;
}
}
.carbon-img {
float: left;
margin-left: -145px;
}
.carbon-poweredby {
display: block;
margin-top: .75rem;
color: var(--accent-color) !important;
}
================================================
FILE: _sass/_og.scss
================================================
//
// Global
//
:root {
--blue: #0d6efd;
--indigo: #6610f2;
--purple: #6f42c1;
--pink: #d63384;
--red: #dc3545;
--orange: #fd7e14;
--yellow: #ffc107;
--green: #198754;
--teal: #20c997;
--cyan: #0dcaf0;
--white: #fff;
--gray: #6c757d;
--gray-dark: #343a40;
--blue-300: #6ea8fe;
--blue-700: #084298;
--font: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
--font-heading: InterDisplay;
--body-bg: #fff;
--body-color: #212529;
--secondary-color: #6c757d;
--border-color: rgba(0, 0, 0, .1);
--accent-bg: #f8f9fa;
--link-color: var(--blue);
--masthead-bg: var(--accent-bg);
--gap: 2rem;
}
@media (prefers-color-scheme: dark) {
:root {
--body-bg: #212529;
--body-color: #dee2e6;
--accent-bg: #2a2f34;
--link-color: var(--blue-300);
--border-color: rgba(255, 255, 255, .1);
}
}
*,
*::before,
*::after {
box-sizing: border-box;
}
html {
font-size: 16px;
font-family: var(--font);
line-height: 1.5;
}
body {
margin: 0;
color: var(--body-color);
background-color: var(--body-bg);
}
a {
color: var(--link-color);
text-decoration: none;
&:hover {
text-decoration: underline;
}
code {
color: inherit;
}
}
h1, h2, h3, h4 {
margin-top: 0;
margin-bottom: .5rem;
font-family: var(--font-heading), var(--font);
font-weight: 600;
line-height: 1;
}
h1 { font-size: 2.5rem; }
h2 { font-size: 2rem; }
h3 { font-size: 1.75rem; }
h4 { font-size: 1.5rem }
p {
margin-top: 0;
margin-bottom: 1rem;
}
blockquote {
position: relative;
margin: 0 1rem 1rem;
font-style: italic;
color: #7a7a7a;
p {
margin-bottom: 0;
}
}
ul {
padding-left: 1.5em;
}
li {
padding-inline-start: 1.5ch;
margin-bottom: .25rem;
list-style-type: "—";
&::marker { color: var(--secondary-color); }
}
p:last-child,
ul:last-child,
blockquote:last-child {
margin-bottom: 0;
}
//
// Code
//
code,
pre {
font-family: "SFMono-Regular", Menlo, "Courier New", monospace;
font-size: 95%;
}
code {
font-size: 85%;
color: var(--pink);
border-radius: .2rem;
}
pre {
display: block;
margin: 0 0 1rem;
line-height: 1.4;
white-space: pre;
white-space: pre-wrap;
code {
padding: 0;
color: inherit;
background-color: transparent;
border: 0;
}
}
.highlight {
margin: 0;
background-color: var(--accent-bg);
pre {
margin-bottom: 0;
padding: var(--gap);
}
& + .highlight {
margin-top: 1rem;
}
}
//
// Masthead
//
.cg-icon {
display: block;
width: 128px;
height: 128px;
margin-left: -.75rem;
margin-bottom: 1.5rem;
@media (min-width: 768px) {
width: 192px;
height: 192px;
margin-left: -1.25rem;
}
}
.cg-masthead {
padding-top: 2rem;
padding-bottom: 3rem;
margin-bottom: 1.5rem;
background-color: var(--masthead-bg);
p {
margin-bottom: 1rem;
}
@media (min-width: 960px) {
padding-top: 3rem;
padding-bottom: 4rem;
}
}
.cg-masthead-title {
margin-bottom: 1rem;
font-size: 4rem;
font-weight: 700;
line-height: .9;
color: inherit;
@media (min-width: 768px) {
font-size: 6rem;
}
}
.cg-masthead-subtitle {
max-width: 46rem;
margin-bottom: 1.5rem;
font-size: 1.5rem;
line-height: 1.25;
@media (min-width: 768px) {
font-size: 2rem;
}
}
//
// Content
//
.container {
max-width: 80em;
padding-right: 2rem;
padding-left: 2rem;
margin-right: auto;
margin-left: auto;
@media (min-width: 768px) {
padding-right: 3rem;
padding-left: 3rem;
}
}
.content {
display: flex;
flex-direction: column;
gap: var(--gap);
@media (min-width: 768px) {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: calc(var(--gap) * 2) var(--gap);
align-items: stretch;
h2 {
grid-column: auto / span 2;
}
.highlight {
height: 100%;
}
}
}
//
// The Grid
//
@media (min-width: 768px) {
.col,
blockquote {
grid-column: auto / span 1;
}
}
//
// Sections
//
h2 {
padding-top: 1rem;
margin-top: 3rem;
margin-bottom: 0;
border-top: .25rem solid;
}
//
// Footer
//
.footer {
grid-column: auto / span 2;
padding-top: 3rem;
padding-bottom: 3rem;
margin-top: 5rem;
border-top: .25rem solid;
}
.footer p {
margin-bottom: .5rem;
}
.quick-links {
display: flex;
gap: 1.25rem;
list-style: none;
margin-left: 0;
padding-left: 0;
}
.quick-links li {
display: inline;
padding-inline-start: 0;
}
================================================
FILE: _sass/_syntax.scss
================================================
// GitHub
:root {
--base00: #ffffff;
--base01: #f5f5f5;
--base02: #c8c8fa;
--base03: #969896;
--base04: #030303;
--base05: #333333;
--base06: #ffffff;
--base07: #b08800;
--base08: #ed6a43;
--base09: #0086b3;
--base0A: #795da3;
--base0B: #183691;
--base0C: #183691;
--base0D: #795da3;
--base0E: #a71d5d;
--base0F: #333333;
}
// One Light
// :root {
// --base00: #fafafa;
// --base01: #f0f0f1;
// --base02: #e5e5e6;
// --base03: #a0a1a7;
// --base04: #696c77;
// --base05: #383a42;
// --base06: #202227;
// --base07: #090a0b;
// --base08: #ca1243;
// --base09: #d75f00;
// --base0A: #c18401;
// --base0B: #50a14f;
// --base0C: #0184bc;
// --base0D: #4078f2;
// --base0E: #a626a4;
// --base0F: #986801;
// }
@media (prefers-color-scheme: dark) {
// One Dark
:root {
--base00: #282c34;
--base01: #353b45;
--base02: #3e4451;
--base03: #545862;
--base04: #565c64;
--base05: #abb2bf;
--base06: #b6bdca;
--base07: #d19a66;
--base08: #e06c75;
--base09: #d19a66;
--base0A: #e5c07b;
--base0B: #98c379;
--base0C: #56b6c2;
--base0D: #61afef;
--base0E: #c678dd;
--base0F: #be5046;
}
}
.hll { background-color: #ffffff }
.c { color: var(--base03) }
.err { color: var(--base08) }
.k { color: var(--base0E) }
.l { color: var(----base09) }
.n { color: var(--base08) }
.o { color: var(--base08) }
.p { color: var(--base05) }
.cm { color: var(--base04) }
.cp { color: var(--base04) }
.c1 { color: var(--base03) }
.cs { color: var(--base04) }
.gd { color: var(--base08) }
.ge { font-style: italic }
.gh { color: #ffffff; font-weight: bold }
.gi { color: var(--base0C) }
.gp { color: var(--base04); font-weight: bold }
.gs { font-weight: bold }
.gu { color: var(--base0C); font-weight: bold }
.kc { color: var(--base0E) }
.kd { color: var(--base0E) }
.kn { color: var(--base0C) }
.kp { color: var(--base0E) }
.kr { color: var(--base0E) }
.kt { color: var(--base0A) }
.ld { color: var(--base0C) }
.m { color: var(--base09) }
.s { color: var(--base0C) }
.na { color: var(--base08) }
.nb { color: var(--base05) }
.nc { color: var(--base07) }
.no { color: var(--base08) }
.nd { color: var(--base07) }
.ni { color: #ffffff }
.ne { color: var(--base08) }
.nf { color: var(--base0B) }
.nl { color: var(--base05) }
.nn { color: var(--base0A) }
.nx { color: var(--base0A) }
.py { color: var(--base08) }
.nt { color: var(--base08) }
.nv { color: var(--base08) }
.ow { color: var(--base0C) }
.w { color: #ffffff }
.mf { color: var(--base09) }
.mh { color: var(--base09) }
.mi { color: var(--base09) }
.mo { color: var(--base09) }
.sb { color: var(--base0C) }
.sc { color: #ffffff }
.sd { color: var(--base04) }
.s2 { color: var(--base0C) }
.se { color: var(--base09) }
.sh { color: var(--base0C) }
.si { color: var(--base09) }
.sx { color: var(--base0C) }
.sr { color: var(--base0C) }
.s1 { color: var(--base0C) }
.ss { color: var(--base0C) }
.bp { color: var(--base05) }
.vc { color: var(--base08) }
.vg { color: var(--base08) }
.vi { color: var(--base08) }
.il { color: var(--base09) }
// Color commas in rgba() values
.m + .o { color: var(--base03) }
// Fix bash
.language-sh .c { color: var(--base03) }
================================================
FILE: cg.scss
================================================
---
# Front matter comment to ensure Jekyll properly reads file.
---
/*
* Code Guide by @mdo
* Licensed MIT
* https://codeguide.co
*/
@import "ads";
@import "syntax";
@import "og";
================================================
FILE: index.md
================================================
---
layout: default
---
<h2 id="toc">Table of contents</h2>
<div markdown="1">
### [HTML](#html)
- [HTML syntax](#html-syntax)
- [HTML5 doctype](#html5-doctype)
- [Language attribute](#language-attribute)
- [IE compatibility mode](#ie-compatibility-mode)
- [Character encoding](#character-encoding)
- [CSS and JavaScript includes](#css-and-javascript-includes)
- [Practicality over purity](#practicality-over-purity)
- [Attribute order](#attribute-order)
- [Boolean attributes](#boolean-attributes)
- [Reduce markup](#reduce-markup)
- [Editor preferences](#editor-preferences)
</div>
<div markdown="1">
### [CSS](#css)
- [CSS syntax](#css-syntax)
- [Declaration order](#declaration-order)
- [Colors](#colors)
- [Logical properties](#logical-properties)
- [Avoid @import`s](#avoid-imports)
- [Media query placement](#media-query-placement)
- [Single declarations](#single-declarations)
- [Shorthand notation](#shorthand-notation)
- [Nesting in preprocessors](#nesting-in-preprocessors)
- [Operators in preprocessors](#operators-in-preprocessors)
- [Comments](#comments)
- [Class names](#class-names)
- [Selectors](#selectors)
- [Child and descendant selectors](#child-and-descendant-selectors)
- [Organization](#organization)
</div>
## Golden rule
Enforce these, or your own, agreed upon guidelines at all times. Small or large, call out what's incorrect. For additions or contributions to this Code Guide, please [open an issue on GitHub](https://github.com/mdo/code-guide/issues/new).
> Every line of code should appear to be written by a single person, no matter the number of contributors.
## HTML
<div markdown="1">
### Syntax
{: #html-syntax }
- Don't capitalize tags, including the doctype.
- Use soft tabs with two spaces—they're the only way to guarantee code renders the same in any environment.
- Nested elements should be indented once (two spaces).
- Always use double quotes, never single quotes, on attributes.
- Don't include a trailing slash in self-closing elements—the [HTML5 spec](https://html.spec.whatwg.org/multipage/syntax.html#syntax-start-tag) says they're optional.
- Don’t omit optional closing tags (e.g. `</li>` or `</body>`).
</div>
```html
<!doctype html>
<html>
<head>
<title>Page title</title>
</head>
<body>
<img src="images/company-logo.png" alt="Company">
<h1 class="hello-world">Hello, world!</h1>
</body>
</html>
```
<div markdown="1">
### HTML5 doctype
Enforce [standards mode](https://developer.mozilla.org/en-US/docs/Web/HTML/Quirks_Mode_and_Standards_Mode) and more consistent rendering in every browser possible with this simple doctype at the beginning of every HTML page. In keeping with the suggested syntax, keep it lowercase.
</div>
```html
<!doctype html>
<html>
<head>
<!-- ... -->
</head>
<body>
<!-- ... -->
</body>
</html>
```
<div markdown="1">
### Language attribute
From the HTML5 spec:
> Authors are encouraged to specify a lang attribute on the root html element, giving the document's language. This aids speech synthesis tools to determine what pronunciations to use, translation tools to determine what rules to use, and so forth.
Read more about the `lang` attribute [in the spec](https://html.spec.whatwg.org/multipage/semantics.html#the-html-element). Head to the <abbr title="Internet Assigned Numbers Authority">IANA</abbr> for a [list of language codes](https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry).
</div>
```html
<html lang="en">
<!-- ... -->
</html>
```
<div markdown="1">
### IE compatibility mode
There's no need to include the Internet Explorer document compatibility `<meta>` tag these days, unless you need support for IE10 and older. The tag was dropped in IE11 and isn't used in Microsoft Edge (legacy or otherwise).
For more information, [read this awesome Stack Overflow article](https://stackoverflow.com/questions/6771258/what-does-meta-http-equiv-x-ua-compatible-content-ie-edge-do).
</div>
```html
<!-- IE10 and below only -->
<meta http-equiv="x-ua-compatible" content="ie=edge">
```
<div markdown="1">
### Character encoding
Ensure proper content rendering by declaring an explicit character encoding. This also allows you to use regular characters instead of their HTML entities, like `—` instead of `&emdash;`, provided their encoding matches that of the document. For some reserved XML characters—like ampersand, non-breaking spaces, less/greater than, and quotes—you may still need to use the HTML character entities.
UTF-8 is the recommended encoding.
</div>
```html
<head>
<meta charset="utf-8">
</head>
<body>
<p>Use an em dash like so—no HTML entity required.</p>
</body>
```
<div markdown="1">
### CSS and JavaScript includes
Per HTML5 spec, typically there is no need to specify a `type` when including CSS and JavaScript files as `text/css` and `text/javascript` are their respective defaults.
#### HTML5 spec links
- [Using link](https://www.w3.org/TR/2011/WD-html5-20110525/semantics.html#the-link-element)
- [Using style](https://www.w3.org/TR/2011/WD-html5-20110525/semantics.html#the-style-element)
- [Using script](https://www.w3.org/TR/2011/WD-html5-20110525/scripting-1.html#the-script-element)
</div>
```html
<!-- External CSS -->
<link rel="stylesheet" href="code-guide.css">
<!-- In-document CSS -->
<style>
/* ... */
</style>
<!-- JavaScript -->
<script src="code-guide.js"></script>
```
<div markdown="1">
### Practicality over purity
Strive to maintain HTML standards and semantics, but not at the expense of practicality. Use the least amount of markup with the fewest intricacies whenever possible.
</div>
```html
<!-- Good -->
<button>...</button>
<!-- Not good -->
<div class="btn" onClick="...">...</div>
```
<div markdown="1">
### Attribute order
HTML attributes should come in this particular order for easier reading of code.
- `class`
- `id`, `name`
- `data-*`
- `src`, `for`, `type`, `href`, `value`
- `title`, `alt`
- `role`, `aria-*`
- `tabindex`
- `style`
Attributes that are most commonly used for identifying elements should come first—`class`, `id`, `name`, and `data` attributes. Miscellaneous attributes unique to specific elements come second, followed by accessibility and style related attributes.
</div>
```html
<a class="..." id="..." data-toggle="modal" href="#">
Example link
</a>
<input class="form-control" type="text">
<img src="..." alt="...">
```
<div markdown="1">
### Boolean attributes
A boolean attribute is one that needs no declared value. XHTML required you to declare a value, but HTML5 has no such requirement.
For further reading, consult the [WhatWG section on boolean attributes](https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attributes)
> The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.
If you <em>must</em> include the attribute's value, and **you don't need to**, follow this WhatWG guideline:
> If the attribute is present, its value must either be the empty string or [...] the attribute's canonical name, with no leading or trailing whitespace.
In short, **don't add a value**.
</div>
```html
<input type="text" disabled>
<input type="checkbox" value="1" checked>
<select>
<option value="1" selected>1</option>
</select>
```
<div markdown="1">
### Reduce markup
Whenever possible, avoid superfluous parent elements when writing HTML. Many times this requires iteration and refactoring, but produces less HTML.
</div>
```html
<!-- Not so great -->
<span class="avatar">
<img src="...">
</span>
<!-- Better -->
<img class="avatar" src="...">
```
<div markdown="1">
### Editor preferences
Set your editor to the following settings to avoid common code inconsistencies and dirty diffs:
- Use soft-tabs set to two spaces.
- Trim trailing white space on save.
- Set encoding to UTF-8.
- Add new line at end of files.
Consider documenting and applying these preferences to your project's `.editorconfig` file. For an example, see [the one in Bootstrap](https://github.com/twbs/bootstrap/blob/main/.editorconfig). Learn more [about EditorConfig](https://editorconfig.org).
</div>
## CSS
<div markdown="1">
### Syntax
{: #css-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`).
- Use space-separated values for color properties (e.g., `color: rgb(0 0 0 / .5)`). [See the Colors section for more information.](#colors)
- 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 discern when scanning a document as they tend to have more unique shapes.
- Use shorthand hex values where available, e.g., `#fff` instead of `#ffffff`.
- Quote attribute values in selectors, e.g., `input[type="text"]`. [They’re only optional in some cases](https://mathiasbynens.be/notes/unquoted-attribute-values#css), and it’s a good practice for consistency.
- Avoid specifying units for zero values, e.g., `margin: 0;` instead of `margin: 0px;`.
Questions on the terms used here? See the [syntax section of the Cascading Style Sheets article](https://en.wikipedia.org/wiki/Cascading_Style_Sheets#Syntax) on Wikipedia.
</div>
```scss
// Bad CSS
.selector, .selector-secondary, .selector[type=text] {
padding:15px;
margin:0px 0px 15px;
background-color:rgba(0, 0, 0, 0.5);
box-shadow:0px 1px 2px #CCC,inset 0 1px 0 #FFFFFF
}
// Good CSS
.selector,
.selector-secondary,
.selector[type="text"] {
padding: 15px;
margin-bottom: 15px;
background-color: rgb(0 0 0 / .5);
box-shadow: 0 1px 2px #ccc, inset 0 1px 0 #fff;
}
```
<div markdown="1">
### Declaration order
Property declarations should be grouped together in the following order:
1. Positioning
2. Box model
3. Typographic
4. Visual
5. Misc
Positioning comes first because it can remove an element from the normal document flow and override box model related styles. The box model—whether it's flex, float, grid, or table—follows as it dictates a component's dimensions, placement, and alignment. Everything else takes place _inside_ the component or without impacting the previous two sections, and thus they come last.
While `border` is part of the box model, most systems globally reset the [`box-sizing`](https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing) to `border-box` so that `border-width` doesn't affect overall dimensions. This, combined with keeping `border` near `border-radius`, is why it's under the Visual section instead.
Preprocessor mixins and functions should appear wherever most appropriate. For example, a `border-top-radius()` mixin would go in place of `border-radius` properties, while a `responsive-font-size()` function would go in place of `font-size` properties.
For a complete list of properties and their order, please see the [property order for Stylelint](https://github.com/stormwarning/stylelint-config-recess-order) used by [Bootstrap](https://getbootstrap.com).
</div>
```scss
.declaration-order {
// Positioning
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 100;
// Box model
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 100px;
height: 100px;
// Typography
font: normal 14px "Helvetica Neue", sans-serif;
line-height: 1.5;
color: #333;
text-align: center;
text-decoration: underline;
// Visual
background-color: #f5f5f5;
border: 1px solid #e5e5e5;
border-radius: 3px;
// Misc
opacity: 1;
}
```
<div markdown="1">
### Logical properties
Logical properties are alternatives to directional and dimensonal properties based on abstract terms like *block* and *inline*. By default, block refers to the vertical direction (top and bottom) while inline refers to the horizontal direction (right and left). You can begin to use these values in your CSS in all modern, evergreen browsers.
**Why use logical properties?** Not every language flows left-ro-right like English, so the [writing mode](https://developer.mozilla.org/en-US/docs/Web/CSS/writing-mode) needs to be flexible. With logical properties, you can easily support languages that can be written horizontally or vertically (like Chinese, Japanese, and Korean). Plus, they're usually shorter and simpler to write.
**Additional reading:**
- [CSS Logical Properties and Values – MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Logical_Properties)
- [CSS Logical Properties and Values — CSS Tricks](https://css-tricks.com/css-logical-properties-and-values/)
- [CSS Writing Modes – MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Writing_Modes)
</div>
```scss
// Without logical properties
.element {
margin-right: auto;
margin-left: auto;
border-top: 1px solid #eee;
border-bottom: 1px solid #eee;
}
// With logical properties
.element {
margin-inline: auto;
border-block: 1px solid #eee;
}
```
<div markdown="1">
### Colors
With the support of [CSS Color Levels 4](https://www.w3.org/TR/css-color-4/) [in all major browsers](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/rgb#space-separated_values), `rgba()` and `hsla()` are now aliases for `rgb()` and `hsl()`, meaning you can modify alpha values in `rgb()` and `hsl()`. Along with this comes support for new space-separated syntax for color values. For compability with future CSS color functions, use this new syntax.
Regardless of your color values and syntax, always ensure your color choices meet [WCAG minimum contrast ratios](https://webaim.org/articles/contrast/) (4.5:1 for 16px and smaller, 3:1 for larger).
**Additional reading:**
- [Smashing Magazine - A Guide To Modern CSS Colors](https://www.smashingmagazine.com/2021/11/guide-modern-css-colors/)
- [`rgb()` - MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/rgb)
</div>
```css
.element {
color: rgb(255 255 255 / .65);
background-color: rgb(0 0 0 / .95);
}
```
<div markdown="1">
### Avoid `@import`s
Compared to `<link>`s, `@import` is slower, adds extra page requests, and can cause other unforeseen problems. Avoid them and instead opt for an alternate approach:
- Use multiple `<link>`elements
- Compile your CSS with a preprocessor like [Sass](https://sass-lang.com/) or [Less](https://lesscss.org/) into a single file
- Concatenate your CSS files with features provided in Rails, Jekyll, and other environments
For more information, [read this article by Steve Souders](https://www.stevesouders.com/blog/2009/04/09/dont-use-import/).
</div>
```html
<!-- Use link elements -->
<link rel="stylesheet" href="core.css">
<!-- Avoid @imports -->
<style>
@import url("more.css");
</style>
```
<div markdown="1">
### Media query placement
Place media queries as close to their relevant rule sets whenever possible. Don't bundle them all in a separate stylesheet or at the end of the document. Doing so only makes it easier for folks to miss them in the future. Here's a typical setup.
</div>
```css
.element { ... }
.element-avatar { ... }
.element-selected { ... }
@media (min-width: 480px) {
.element { ... }
.element-avatar { ... }
.element-selected { ... }
}
```
<div markdown="1">
### Single declarations
In instances where a rule set includes **only one declaration**, consider removing line breaks for readability and faster editing. Any rule set with multiple declarations should be split to separate lines.
The key factor here is error detection—e.g., a CSS validator stating you have a syntax error on Line 183. With a single declaration, there's no missing it. With multiple declarations, separate lines is a must for your sanity.
</div>
```scss
// Single declarations on one line
.span1 { width: 60px; }
.span2 { width: 140px; }
.span3 { width: 220px; }
// Multiple declarations, one per line
.sprite {
display: inline-block;
width: 16px;
height: 15px;
background-image: url("../img/sprite.png");
}
.icon { background-position: 0 0; }
.icon-home { background-position: 0 -20px; }
.icon-account { background-position: 0 -40px; }
```
<div markdown="1">
### Shorthand notation
Limit shorthand declaration usage to instances where you must explicitly set all available values. Frequently overused shorthand properties include:
- `padding`
- `margin`
- `font`
- `background`
- `border`
- `border-radius`
Usually 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. A `0` value implies an override of either a browser default or previously specified value.
Excessive use of shorthand properties leads to sloppier code with unnecessary overrides and unintended side effects.
The Mozilla Developer Network has a great article on [shorthand properties](https://developer.mozilla.org/en-US/docs/Web/CSS/Shorthand_properties) for those unfamiliar with notation and behavior.
</div>
```scss
// Bad example
.element {
margin: 0 0 10px;
background: red;
background: url("image.jpg");
border-radius: 3px 3px 0 0;
}
// Good example
.element {
margin-bottom: 10px;
background-color: red;
background-image: url("image.jpg");
border-top-left-radius: 3px;
border-top-right-radius: 3px;
}
```
<div markdown="1">
### Nesting in preprocessors
Avoid unnecessary nesting in preprocessors whenever possible—keep it simple and avoid reverse nesting. Consider nesting only if you must scope styles to a parent and if there are multiple elements to be nested.
**Additional reading:**
- <a href="https://markdotto.com/2015/07/20/css-nesting/">Nesting in Sass and Less</a>
</div>
```scss
// Without nesting
.table > thead > tr > th { … }
.table > thead > tr > td { … }
// With nesting
.table > thead > tr {
> th { … }
> td { … }
}
```
<div markdown="1">
### Operators in preprocessors
For improved readability, wrap all math operations in parentheses with a single space between values, variables, and operators.
</div>
```scss
// Bad example
.element {
margin: 10px 0 @variable*2 10px;
}
// Good example
.element {
margin: 10px 0 (@variable * 2) 10px;
}
```
<div markdown="1">
### 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. Use the `//` syntax when writing CSS with preprocessors. When shipping CSS to production, remove all comments.
Be sure to write in complete sentences for larger comments and succinct phrases for general notes.
</div>
```scss
// Bad example
// Modal header
.modal-header {
...
}
// Good example
// Wrapping element for .modal-title and .modal-close
.modal-header {
...
}
```
<div markdown="1">
### Class names
- 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.
It's also useful to apply many of these same rules when creating custom properties and preprocessor variable names.
</div>
```scss
// Bad example
.t { ... }
.red { ... }
.header { ... }
// Good example
.tweet { ... }
.important { ... }
.tweet-header { ... }
```
<div markdown="1">
### Selectors
- Use classes over generic element tags for more explicit and reliable styling that isn't dependent on your markup.
- Avoid using several attribute selectors (e.g., `[class^="..."]`) on commonly occuring 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).
**Additional reading:**
- [Scope CSS classes with prefixes](https://markdotto.com/2012/02/16/scope-css-classes-with-prefixes/)
- [Stop the cascade](https://markdotto.com/2012/03/02/stop-the-cascade/)
</div>
```scss
// Bad example
span { ... }
.page-container #stream .stream-item .tweet .tweet-header .username { ... }
.avatar { ... }
// Good example
.avatar { ... }
.tweet-header .username { ... }
.tweet .avatar { ... }
```
<div markdown="1">
### Child and descendant selectors
When necessary, it may be helpful to use [the child combinator (`>`)](https://developer.mozilla.org/en-US/docs/Web/CSS/Child_combinator) to limit the cascade of some styles in elements like `<table>`s that are often recursively nested. Use it to limit styles to the immediate children elements of a parent element to avoid unnecessary overrides later on.
</div>
```css
.custom-table > tbody > tr > td,
.custom-table > tbody > tr > th {
/* ... */
}
```
<div markdown="1">
### Organization
- Organize sections of code by component.
- Develop a consistent commenting hierarchy.
- Use consistent white space to your advantage when separating sections of code for scanning larger documents.
- When using multiple CSS files, break them down by component instead of page. Pages can be rearranged and components moved.
</div>
```scss
//
// Component section heading
//
.element { ... }
//
// Component section heading
//
// Sometimes you need to include optional context for the entire component. Do that up here if it's important enough.
//
.element { ... }
// Contextual sub-component or modifer
.element-heading { ... }
```
gitextract_lhpf4avt/ ├── .editorconfig ├── .gitignore ├── CNAME ├── LICENSE.md ├── README.md ├── _config.yml ├── _includes/ │ ├── footer.html │ ├── header.html │ ├── js.html │ └── tweet-button.html ├── _layouts/ │ └── default.html ├── _sass/ │ ├── _ads.scss │ ├── _og.scss │ └── _syntax.scss ├── cg.scss └── index.md
Condensed preview — 16 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (43K chars).
[
{
"path": ".editorconfig",
"chars": 208,
"preview": "# editorconfig.org\n\nroot = true\n\n[*]\ncharset = utf-8\nend_of_line = lf\nindent_size = 2\nindent_style = space\ninsert_final_"
},
{
"path": ".gitignore",
"chars": 477,
"preview": "# Ignore docs files\n_gh_pages\n_site\n.jekyll-cache\n.ruby-version\n.sass-cache\n\n# Numerous always-ignore extensions\n*.diff\n"
},
{
"path": "CNAME",
"chars": 13,
"preview": "codeguide.co\n"
},
{
"path": "LICENSE.md",
"chars": 1072,
"preview": "The MIT License (MIT)\n\nCopyright (c) Mark Otto.\n\nPermission is hereby granted, free of charge, to any person obtaining a"
},
{
"path": "README.md",
"chars": 2817,
"preview": "<h3 align=\"center\">\n <a href=\"https://codeguide.co/\">\n <img src=\"code-guide-logo.png\" alt=\"Code Guide logo\" width=\"1"
},
{
"path": "_config.yml",
"chars": 290,
"preview": "name: Code Guide by @mdo\ndescription: Standards for developing consistent, flexible, and sustainable HTML and CSS.\nurl: "
},
{
"path": "_includes/footer.html",
"chars": 1062,
"preview": "<footer class=\"footer\">\n <p>\n Open sourced by <a href=\"https://twitter.com/mdo\">@mdo</a> under MIT license. Copyrig"
},
{
"path": "_includes/header.html",
"chars": 584,
"preview": "<header class=\"cg-masthead\">\n <div class=\"container\">\n <img class=\"cg-icon\" src=\"code-guide-logo.png\" alt=\"Code Guid"
},
{
"path": "_includes/js.html",
"chars": 64,
"preview": "<script src=\"https://platform.twitter.com/widgets.js\"></script>\n"
},
{
"path": "_includes/tweet-button.html",
"chars": 223,
"preview": "<div class=\"tweet-button\">\n <a href=\"https://twitter.com/share\" class=\"twitter-share-button\" data-size=\"large\" data-cou"
},
{
"path": "_layouts/default.html",
"chars": 2190,
"preview": "<!doctype html>\n<html lang=\"en\">\n <head>\n <meta charset=\"utf-8\">\n <meta name=\"viewport\" content=\"width=device-wid"
},
{
"path": "_sass/_ads.scss",
"chars": 622,
"preview": "// stylelint-disable declaration-no-important, selector-max-id\n\n//\n// Carbon ads\n//\n\n#carbonads {\n position: static;\n "
},
{
"path": "_sass/_og.scss",
"chars": 4479,
"preview": "//\n// Global\n//\n\n:root {\n --blue: #0d6efd;\n --indigo: #6610f2;\n --purple: #6f42c1;\n --pink: #d63384;\n --red: #dc354"
},
{
"path": "_sass/_syntax.scss",
"chars": 3472,
"preview": "// GitHub\n:root {\n --base00: #ffffff;\n --base01: #f5f5f5;\n --base02: #c8c8fa;\n --base03: #969896;\n --base"
},
{
"path": "cg.scss",
"chars": 187,
"preview": "---\n# Front matter comment to ensure Jekyll properly reads file.\n---\n\n/*\n * Code Guide by @mdo\n * Licensed MIT\n * https:"
},
{
"path": "index.md",
"chars": 22462,
"preview": "---\nlayout: default\n---\n\n<h2 id=\"toc\">Table of contents</h2>\n\n<div markdown=\"1\">\n### [HTML](#html)\n\n- [HTML syntax](#htm"
}
]
About this extraction
This page contains the full source code of the mdo/code-guide GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 16 files (39.3 KB), approximately 11.5k tokens. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.