Style language of the web: defines layout and style of HTML pages
<h1 style="color: grey; font-size: 30px">Hello</h1>
usually inside the head
:
<link rel="stylesheet" href="style.css" />
/* comment */
h1 {
font-family: sans-serif;
color: grey;
}
#home-button {
color: white;
}
*
h1
.important
#home-button
If two CSS properties are at odds with each other, the property that is applied is:
h1 {
color: red; /* text color */
background-color: blue; /* background color */
}
grey
, blue
, lightblue
, ...rgb(255, 128, 128)
#ff8080
hsl(180, 60%, 70%)
p {
font-family: Arial, sans-serif;
font-size: 16px;
line-height: 1.5;
font-style: italic;
font-weight: bold;
text-decoration: underline;
text-align: center;
}
we can specify multiple font families; the browser uses the first one that is available
p {
font-family: Arial, sans-serif;
}
Three generic font families that are available in every browser:
serif
, sans-serif
, monospace
possible units:
px
: "pixel"%
: percentage relative to the surrounding textem
: relative to the surrounding textrem
: relative to the font size of the html
elementcommon default font size in browsers: 16px
common default line height in browsers: 1.2
is often set to bigger values, e.g. 1.5
only used for italic font
h1 {
font-style: italic;
}
possible values:
100
200
300
(or: light
)400
(or: regular
)500
600
(or: semibold
)700
(or: bold
)800
900
h1 {
text-decoration: underline, overline, line-through;
}
center
justify
left
right
start
end
px, em, rem, %, vh, vw
formerly:
1px = one pixel on the screen
today:
e.g. on iPhone 4: 1px = two pixels on the screen (device pixel ratio = 2)
can be queried via the JavaScript variable devicePixelRatio
rem = font size of the html
element
default size in browsers: 1rem = 16px
1vh = 1% of the viewport height
1vw = 1% of the viewport width
reset: stylesheets which unify basic styles across browsers:
common CSS libraries:
simple, basic CSS library:
defining CSS variables:
:root {
--primary-color: #1565c0;
}
using variables:
h1 {
color: var(--primary-color);
}
button {
background-color: var(--primary-color);
color: white;
}
inline elements:
a
, em
, strong
, (img
)block elements:
h1
, ul
, li
, p
span
= generic inline element
div
= generic block element
"layers" of block elements from innermost to outermost:
example: two boxes
sizing the content:
height
/ width
min-height
/ min-width
max-height
/ max-width
if we specify a percentage amount (e.g. width: 50%
) the size is relative to the parent element
By default the attributes height
, width
, ... apply to the content (without padding or border)
In modern CSS we often set:
* {
box-sizing: border-box;
}
This means these attributes will refer to the total size (including padding and border, not including margin)
padding: 10px;
padding: 10px 20px 30px 40px
(top - right - bottom - left)padding: 10px 20px;
(top and bottom - left and right)margin: 10px auto;
padding: 10px; padding-left: 20px;
border (example): border: 1px solid blue
rounded corners: border-radius: 5px
Note: vertical margin and padding will not influence the positioning of inline elements (and may cause overlaps)
div {
width: 800px;
margin: auto;
}
div {
max-width: 800px;
margin: auto;
}
By default the document body will be ass tall as its content
If the body should take up the entire screen height:
html {
height: 100%;
}
body {
height: 100%;
}
By default every cell in a table has its own border
"collapsing" the borders of adjacent cells:
table {
border-collapse: collapse;
}
default behavior if a child element is taller or wider than its parent:
the child will flow out of the parent element
to display scroll bars if needed:
#parent {
overflow: auto;
}
example: display a background for the entire page by making the body take the full height, but display scrollbars if its content is more than that
body {
height: 100vh;
overflow: auto;
background-color: lightgrey;
}
special case: the body element always behaves like overflow: auto;
* {
box-sizing: border-box;
}
Der Body margin (Standard: 8px) wird oft auf 0 gesetzt - z.B. damit eine Navigationsleiste ganz oben angezeigt werden kann
body {
margin: 0;
}
simple sans-serif font declaration:
body {
font-family: sans-serif;
}
default sans-serif font declaration in Bootstrap / Reboot - includes native fonts for various platforms:
body {
font-family: system-ui, -apple-system, 'Segoe UI', Roboto,
'Helvetica Neue', Arial, 'Noto Sans', 'Liberation Sans',
sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol',
'Noto Color Emoji' !default;
}
make the body cover the whole screen (e.g. for setting the background):
html {
height: 100%;
}
body {
height: 100%;
padding: 0;
}
example styled button:
button {
background-color: #0074d9;
color: #ffffff;
padding: 0.5em 1em;
border: none;
border-radius: 4px;
cursor: pointer;
}
fluid container similar to that of bootstrap:
.container-fluid {
padding-left: 12px;
padding-right: 12px;
}
container similar to that of bootstrap:
.container {
padding-left: 12px;
padding-right: 12px;
margin-left: auto;
margin-right: auto;
}
@media (min-width: 576px) {
.container {
max-width: 540px;
}
}
@media (min-width: 768px) {
.container {
width: 720px;
}
}
/* ... (more media queries) ... */
default viewport widths and container sizes in bootstrap:
simple example from Picnic CSS:
.flex {
display: flex;
flex-wrap: wrap;
}
possibilities:
display: block
display: inline
display: none
display: flex
Simple possibility to arrange elements underneath each other or next to each other
container:
flex-direction
flex-wrap
justify-content
align-content
align-items
items:
flex-basis
flex-grow
#container {
display: flex;
flex-direction: row;
}
#container {
display: flex;
flex-direction: column;
}
Example: page layout with navbar on the side and main area
body {
display: flex;
flex-direction: row;
}
body > nav {
flex-basis: 5em;
}
body > main {
flex-grow: 1;
}
Example: article element with header and scrollable main area
article {
height: 240px;
display: flex;
flex-direction: column;
}
article > header {
height: 48px;
}
article > .main-content {
flex-grow: 1;
overflow-y: auto;
}
possibility to query the screen size and orientation (amongst others)
.menu {
display: flex;
flex-direction: column;
}
@media (min-width: 800px) {
.menu {
flex-direction: row;
}
}
@media (orientation: landscape) {
.layout {
flex-direction: row;
}
}
@media (min-width: 576px) {
/* small screen */
}
@media (min-width: 768px) {
/* medium screen */
}
/* ... */
common breakpoints (roughly every 200px):
size | min (bootstrap) | min (tailwind) |
---|---|---|
xs | N/A | N/A |
sm | 576px | 640px |
md | 768px | 768px |
lg | 992px | 1024px |
xl | 1200px | 1280px |
xxl | 1400px | 1536px |
input[type='checkbox'] {
/* ... */
}
style links occuring somewhere in the header
:
header a {
/* ... */
}
style a nav
which is a direct child of body
:
body > nav {
/* ... */
}
Pseudo classes start with a colon:
a:hover {
text-decoration: underline;
}
button:disabled {
background-color: lightgrey;
}
tr:nth-child(2n) {
background-color: grey;
}
tr:hover:nth-child(n) {
background-color: lightgreen;
}
:checked
:empty
:valid
:invalid
:required
:hover
:visited
:active
:first-child
:last-child
:nth-child(2n)
Pseudo elements allow for adding additional HTML elements via CSS:
.todo-item.completed::before {
content: '✓';
}
nav button[aria-haspopup='true']::after {
content: 'â–¾';
}
shadow example:
.shadow {
box-shadow: rgba(0, 0, 0, 0.15) 0px 8px 16px;
}
0px
8px
16px
rgba(0, 0, 0, 0.15)
(transparent black)Example: a div
should be positioned 10px
from the bottom right corner of its parent
<div id="outer">
<div id="inner"></div>
</div>
#outer {
position: relative;
}
#inner {
position: absolute;
bottom: 10px;
right: 10px;
}
position
: absolute
position
: relative
)position
: fixed
)position
: static
: default value)Example for position: relative
: superscript or subscript
Facebook clone with chat
example: layout with header and sidebar
#layout {
display: grid;
height: 100vh;
grid-template-areas: "header header" "sidebar main";
grid-template-rows: auto 1fr;
grid-template-columns: 200px 1fr;
}
placing contents:
#header {
grid-area: header;
}
#main {
grid-area: main;
}
/* ... */
#element1 {
transform: translsate(100px, 0);
}
#element2 {
transform: translate(100px, 0) rotate(45deg);
transform-origin: 0 0;
}
element will be rotated by 45 degrees first, then translated to the right by 100 pixels
center of rotation: top left corner of the element
the change of some CSS properties can be animated:
#animated {
transition: background-color: 3s, margin-top: 1s;
}
div.box {
width: 40px;
height: 40px;
background-color: blue;
transform: rotate(0);
transition: transform 9s, background-color 9s;
}
div.box:hover {
background-color: red;
transform: rotate(360deg) scale(2);
transition: transform 0.5s, background-color 0.5s;
}
div {
width: 40px;
height: 40px;
background-color: blue;
transform: translate(0, 0) rotate(0);
transition: transform 9s, background-color 9s;
}
div:hover {
background-color: red;
transform: translate(200px, 0) rotate(360deg);
transition: transform 3s, background-color 3s;
}
Dropdown that becomes active on button click
HTML template:
<div id="dropdown">dropdown</div>
<button
id="dropdown-button"
onclick="dropdown.className = 'active'"
>
menu
</button>
Note: we are using CSS transformations, not SVG transformations; we must set transform-origin
separately