2024-08-07

CSS cheatsheet


Cascading Style Sheets are used for styling HTML documents.

Box model

css
div {
    /* content box */
    box-sizing: content-box; /* default value */
    width: 400px;
    height: 400px;
    
    /* padding box (includes scroll) */
    padding: 20px;
    overflow: scroll;
    
    /* border box */
    border: solid 5px black;

    /* margin box (includes outline and box-shadow) */
    margin: 40px;
    outline: solid;
    box-shadow: 10px 5px 5px red;
}

Selectors

css
* { color: black; }
article { color: red; }
.text-gray { color: gray; }
#app { border: solid 2px black; }
[data-user-type='admin'] { background-color: red; }

main article h1 { font-weight: bold; }

Cascade

css
h1 { color: yellow; }

h1 { color: blue; }
/* text will be blue */

Specificity

css
 /* 0 points */
* { color: red; }

/* 1 point */
div { color: blue; }
::selection { color: blue }

/* 10 points */
.text-gray { color: gray; }
:hover { text-decoration: underline; }
[data-role='admin'] { color: yellow; }

/* 100 points */
#app { border: solid 10px #000; }
html
<!-- 1000 points --->
<article style="font-size: 200px;"></article>
css
/* 10000 points */
.p-5 { padding: 36px !important; }

Inheritance

css
html {
    color: black;
    font-size: 36px;
}

p {
    background-color: gray;
}

Pseudo-classes

css
a:hover {
    color: blue;
}

li:first-child {
    margin-top: 20px;
}

Pseudo-elements

css
h1::before {
    content: '# ';
}

p::selection {
    color: orange;
}

Color

css
h1 { color: green; }
h2 { color: #00ff00; }
h3 { color: rgb(255, 255, 255); }
h4 { color: rgba(255, 255, 255, 0.2); }
h5 { color: hsl(344, 79%, 40%); }

body { background-color: transparent; }

Units

css
body { font-size: 20px; }
h1 { font-size: 3.5rem; }

article { width: 50%; }

.viewport {
    width: 100vw;
    height: 100vh;
}

.icon {
    transform: rotate(45deg);
}

Text

css
p {
    font-family: "Georgia", serif;
    font-size: 24px;
    font-weight: 700;
    font-style: italic;
}
css
body {
    text-transform: capitalize;
    text-decoration: underline;
    text-align: end;
}

Borders

css
.link {
    border: solid 2px #000;
}
css
.link {
    border-top: solid 22px green;
    border-bottom: solid 2px #000;
    border-right: dashed 12px red;
    border-left: none;
}
css
.button {
    border-radius: 12px;
}

.circle {
    width: 200px;
    height: 200px;
    border-radius: 50%;
}

Shadows

css
h1 { box-shadow: 10px 5px 5px red; }

p { text-shadow: 1px 1px 2px pink; }

Layout

css
/*
 * use 100% width by default
 * you can set width, height, margin, padding
 */
.tile { display: block; }
css
/*
 * display as part of the text, auto width
 * you can set width, height, margin, padding
 */
.header { display: inline-block }
css
/*
 * display as part of the text
 * cannot set width or height
 */
p { display: inline; }

Flexbox

css
.container {
    display: flex;
    flex-direction: row; /* main axis, default value */
    flex-wrap: nowrap;
    justify-content: center; /* justify content along the main axis */
    align-items: center; /* align items along the cross axis */
    gap: 30px;
}
css
.item {
    flex-basis: 300px;
    flex-grow: 1;
    flex-shrink: 0;
    
    /* flex: 1 0 300px; */
}

.item:first-child {
    order: 3;
}

.item:last-child {
    align-self: end;
}

Grid

css
.container {
    display: grid;
    grid-template-columns: 200px 100px 10%;
    grid-template-rows: 300px auto;
    gap: 32px;
}
css
.another-container {
    display: grid;
    grid-template-columns: 1fr 3fr 1fr;
}
css
.yet-another-container {
    display: grid;
    grid-template-columns: repeat(12, minmax(0, 1fr));
}

Backgrounds

css
body { background-color: black; }

h1 { background: no-repeat url('bg.png'); }

article { background-image: linear-gradient(red, black); }

Z-index

css
.menu { z-index: 1000; }

.modal { z-index: 10000; }

Transitions

css
a {
    background-color: red;
    transition: background-color 200ms ease-in-out;
}

a:hover {
    background-color: brown;
}