CSS cheatsheet

HTML

Basic HTML page:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>My website</title>
    </head>
    <body>
        <h1>My website</h1>
        <p>Hello world!</p>
    </body>
</html>

Elements

1
<a href="https://www.website.com">website</a>

Lists

1
2
3
4
5
<ul>
    <li>item 1</li>
    <li>item 2</li>
    <li>item 3</li>
</ul>

Images

1
<img src="file_path.jpg" alt="">

Layout elements

Types of element

Paths

Comments

1
2
3
4
<!-- comment -->

<!-- very very 
long comment -->

CSS

3 ways to use

Inline CSS

1
<p style="property: value; property: value">my paragraph</p>

Internal CSS

1
2
3
4
5
6
7
8
<head>
    <style>
        element { /* rule */
            property: value; /* declaration */
            property: value;
        }
    </style>
</head>

External CSS

1
2
3
<head>
    <link rel="stylesheet" href="style.css">
</head>

Selectors

1
2
3
element {

}
1
<p class="my-class"></p>
1
2
3
4
5
6
7
8
9
.my-class {

}

/* or */

element.my-class {

}
1
<p class="my-id"></p>
1
2
3
#my-id {

}

Properties

Colors

1
2
3
4
5
6
7
p {
    color: red;
    color: #FF00FF; /* RRGGBB */
    color: #F0F; /* RGB */
    color: rgb(255, 0, 255);
    color: hsl(100, 100%, 75%);
}

Size units

Comments

1
/* comment */

Mobile

1
<meta name="viewport" content="width=device-width, initial-scale=1">

Box-sizing

Include padding and border in box, make CSS much easier.

1
2
3
*, *::before, *::after {
    box-sizing: border-box;
}

Center elements horizontally

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
.my-class {
    /* align to the right */
    margin-left: auto /* fill left with empty space */

    /* align to center */
    width: 100px

    margin-left: auto
    margin-right: auto
        /* or */
    margin: 0 auto /* preferred way */
        /* or */
    margin: auto
}

Flexbox

1
2
3
4
5
<div class="container">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
</div>
1
2
3
.container {
    display: flex;
}

Debug tricks

1
2
3
* {
    border: 2px solid red; /* show all boxes */
}