CSS Konsepter
Konsepter som er unike for CSS (Cascading Style Sheets).
Selektorer
Bestemmer hvilke elementer som skal styles.
Tip
ID brukes kun én gang per side, klasser kan gjenbrukes.
Kombinerte selektorer
/* Descendants (all p inside article) */
article p {
line-height: 1.6;
}
/* Direct children */
article > p {
margin: 10px;
}
/* Multiple elements */
h1, h2, h3 {
font-family: Arial;
}
/* Element with class */
p.intro {
font-size: 18px;
}
Pseudo-klasser
/* Hover (mouse over) */
a:hover {
color: red;
}
/* First child */
li:first-child {
font-weight: bold;
}
/* Last child */
li:last-child {
border-bottom: none;
}
/* Every other element */
tr:nth-child(even) {
background: #f2f2f2;
}
/* Focus (on tab/click) */
input:focus {
border-color: blue;
}
Box Model
Alle HTML-elementer er bokser med fire lag.
┌─────────────────────────────────────┐
│ MARGIN │
│ ┌───────────────────────────────┐ │
│ │ BORDER │ │
│ │ ┌─────────────────────────┐ │ │
│ │ │ PADDING │ │ │
│ │ │ ┌───────────────────┐ │ │ │
│ │ │ │ CONTENT │ │ │ │
│ │ │ └───────────────────┘ │ │ │
│ │ └─────────────────────────┘ │ │
│ └───────────────────────────────┘ │
└─────────────────────────────────────┘
| Del | Beskrivelse |
|---|---|
| Content | Innholdet (tekst, bilde) |
| Padding | Avstand mellom innhold og border |
| Border | Rammen rundt elementet |
| Margin | Avstand til andre elementer |
.box {
width: 200px;
padding: 20px;
border: 2px solid black;
margin: 10px;
}
/* Total width: 200 + 20*2 + 2*2 + 10*2 = 264px */
box-sizing
/* Default: width = content only */
box-sizing: content-box;
/* Recommended: width = content + padding + border */
box-sizing: border-box;
/* Set for entire page */
* {
box-sizing: border-box;
}
Margin og Padding
Sentrering med margin
Margin collapse
Vertikale marginer kollapser - kun den største brukes. Mellom to <p> med margin: 20px blir det 20px, ikke 40px.
Border
/* Shorthand */
border: 2px solid black;
/* Individual properties */
border-width: 2px;
border-style: solid;
border-color: black;
/* One side */
border-top: 1px dashed gray;
border-bottom: 3px double blue;
Border-stiler
| Stil | Beskrivelse |
|---|---|
solid |
Heltrukket linje |
dashed |
Stiplet linje |
dotted |
Prikket linje |
double |
Dobbel linje |
none |
Ingen border |
Border-radius (avrunding)
border-radius: 10px; /* All corners */
border-radius: 50%; /* Circle */
border-radius: 10px 0 10px 0; /* Individual corners */
Farger
/* Color name */
color: red;
color: blue;
color: darkgreen;
/* Hex code */
color: #FF0000; /* Red */
color: #00F; /* Short: #0000FF */
/* RGB */
color: rgb(255, 0, 0);
/* RGBA (with transparency) */
color: rgba(255, 0, 0, 0.5); /* 50% transparent */
/* HSL */
color: hsl(0, 100%, 50%); /* Red */
Vanlige farger
| Farge | Navn | Hex |
|---|---|---|
| Svart | black |
#000000 |
| Hvit | white |
#FFFFFF |
| Rød | red |
#FF0000 |
| Grønn | green |
#008000 |
| Blå | blue |
#0000FF |
| Gul | yellow |
#FFFF00 |
| Grå | gray |
#808080 |
Typografi
p {
font-family: Arial, Helvetica, sans-serif;
font-size: 16px;
font-weight: bold; /* normal, bold, 100-900 */
font-style: italic; /* normal, italic */
line-height: 1.6; /* Line height */
text-align: center; /* left, right, center, justify */
text-decoration: underline; /* none, underline, line-through */
text-transform: uppercase; /* lowercase, capitalize */
letter-spacing: 2px;
}
Display
Bestemmer hvordan elementer oppfører seg.
display: block; /* Full width, new line */
display: inline; /* Flows in text, ignores width/height */
display: inline-block; /* Flows, but respects width/height */
display: none; /* Hidden completely */
display: flex; /* Flexbox container */
display: grid; /* Grid container */
Enheter
| Enhet | Type | Beskrivelse |
|---|---|---|
px |
Absolutt | Piksler |
% |
Relativ | Prosent av forelder |
em |
Relativ | Relativt til elementets font-size |
rem |
Relativ | Relativt til rot (html) font-size |
vw |
Relativ | Prosent av viewport bredde |
vh |
Relativ | Prosent av viewport høyde |
Posisjonering
position: static; /* Default (normal flow) */
position: relative; /* Relative to original position */
position: absolute; /* Relative to nearest positioned parent */
position: fixed; /* Relative to viewport (doesn't scroll) */
position: sticky; /* Hybrid: relative until it "sticks" */
Flexbox
For én-dimensjonal layout (rad eller kolonne).
.container {
display: flex;
flex-direction: row; /* row, column, row-reverse */
justify-content: center; /* Main axis */
align-items: center; /* Cross axis */
gap: 20px; /* Spacing */
flex-wrap: wrap; /* Allow wrapping */
}
.item {
flex: 1; /* Grow to fill */
}
justify-content (hovedakse)
| Verdi | Beskrivelse |
|---|---|
flex-start |
Start |
flex-end |
End |
center |
Center |
space-between |
Evenly distributed, no edge space |
space-around |
Even with half edge |
space-evenly |
Completely even |
align-items (kryssakse)
| Verdi | Beskrivelse |
|---|---|
stretch |
Stretch to full height |
flex-start |
Top |
flex-end |
Bottom |
center |
Center |
baseline |
Text baseline |
Grid
For to-dimensjonal layout.
.container {
display: grid;
grid-template-columns: 1fr 2fr 1fr; /* 3 columns */
grid-template-rows: 100px auto; /* 2 rows */
gap: 20px;
}
.item {
grid-column: 1 / 3; /* Span column 1-2 */
grid-row: 1 / 2; /* Row 1 */
}
Media Queries
Responsiv design for ulike skjermstørrelser.
/* Default (mobile first) */
.box {
width: 100%;
}
/* Larger screens */
@media (min-width: 768px) {
.box {
width: 50%;
}
}
@media (min-width: 1024px) {
.box {
width: 33.33%;
}
}
Transitions
Animerer endringer mellom tilstander.
.button {
background: blue;
transition: background 0.3s ease;
}
.button:hover {
background: darkblue;
}
Transform
Endre form, posisjon, rotasjon.