The CSS box model is fundamental to layout. It consists of content, padding, border, and margin.
.box {
width: 200px;
height: 100px;
padding: 20px;
border: 2px solid black;
margin: 10px;
}
Explanation:
width and height set the dimensions of the content areapadding creates space inside the elementborder adds a border around the paddingmargin creates space outside the elementThe box-sizing property changes how the total width and height of elements are calculated.
* {
box-sizing: border-box;
}
This makes padding and border included in the element’s total width and height, which is often more intuitive for layout purposes.
The display property determines how an element is treated in the layout flow.
div {
display: block;
}
Block-level elements:
span {
display: inline;
}
Inline elements:
.inline-block {
display: inline-block;
}
Inline-block elements:
The position property specifies the type of positioning method used for an element.
.static {
position: static;
}
This is the default. Elements are positioned according to the normal flow of the document.
.relative {
position: relative;
top: 10px;
left: 20px;
}
The element is positioned relative to its normal position. Other elements are not affected.
.absolute {
position: absolute;
top: 50px;
right: 30px;
}
The element is positioned relative to its nearest positioned ancestor or the initial containing block.
.fixed {
position: fixed;
bottom: 0;
right: 0;
}
The element is positioned relative to the browser window and doesn’t move when scrolled.
Flexbox is a one-dimensional layout method for arranging items in rows or columns.
.container {
display: flex;
justify-content: space-between;
align-items: center;
}
.item {
flex: 1;
}
Explanation:
display: flex creates a flex containerjustify-content aligns items along the main axisalign-items aligns items along the cross axisflex: 1 on items makes them grow and shrink equallyCSS Grid is a two-dimensional layout system, allowing you to layout items in rows and columns simultaneously.
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 20px;
}
.grid-item {
grid-column: span 2;
}
Explanation:
display: grid creates a grid containergrid-template-columns defines the columns of the gridgrid-gap sets the gap between grid itemsgrid-column: span 2 makes an item span two columnsWhile less common in modern layouts, float is still used for wrapping text around images.
.float-left {
float: left;
margin-right: 15px;
}
.clear {
clear: both;
}
Explanation:
float: left moves the element to the left, allowing content to wrap around itclear: both on a subsequent element ensures it appears below the floated elementsFor text-heavy content, multi-column layout can improve readability.
.multi-column {
column-count: 3;
column-gap: 40px;
column-rule: 1px solid #ccc;
}
Explanation:
column-count specifies the number of columnscolumn-gap sets the gap between columnscolumn-rule adds a line between columnsMedia queries allow you to apply different styles for different screen sizes.
.container {
width: 1200px;
margin: 0 auto;
}
@media screen and (max-width: 1200px) {
.container {
width: 100%;
padding: 0 20px;
}
}
@media screen and (max-width: 768px) {
.grid-container {
grid-template-columns: 1fr;
}
}
Explanation:
Custom properties (CSS variables) can be particularly useful in responsive design.
:root {
--main-width: 1200px;
}
.container {
width: var(--main-width);
margin: 0 auto;
}
@media screen and (max-width: 1200px) {
:root {
--main-width: 100%;
}
}
This approach allows you to define key layout values in one place and easily adjust them for different screen sizes.
next -> Responsive web design