Diese Lektion behandelt das CSS-Box-Modell, Abstände, Ränder, Größen und Positionierung von Elementen. Das Verständnis des Box-Modells ist essentiell für korrektes Layout und Design.
div {
width: 200px; /* Content-Breite */
padding: 10px; /* Innenabstand */
border: 5px solid black; /* Rahmen */
margin: 20px; /* Außenabstand */
}
p {
padding: 15px;
}
div {
border: 2px dashed #2b7cff;
}
section {
margin: 20px auto; /* zentriert horizontal */
}
box-sizing lässt sich festlegen, wie Breite und Höhe berechnet werden.
content-box – Standard, Breite = Contentborder-box – Breite = Content + Padding + Border*, *::before, *::after {
box-sizing: border-box;
}
static – Standardpositionrelative – relativ zur eigenen Positionabsolute – relativ zum nächstgelegenen positionierten Vorfahrenfixed – relativ zum Viewportsticky – scrollt, bleibt aber an einer Position klebenheader {
position: sticky;
top: 0;
}
img {
float: left;
margin-right: 10px;
}
.clearfix::after {
content: "";
display: table;
clear: both;
}
nav {
display: flex;
justify-content: space-between;
align-items: center;
}