⇧
vadnica-logo
Editor My Blog Password Manager Finance My Services About the Project Telegram O meni O meni

CSS Box Model and Positioning

Every HTML element on a web page is a box. The CSS Box Model describes how this box is built: content, padding, border, and margin. Understanding the Box Model is crucial for controlling the size and distribution of elements.

The width and height properties define the size of the content, while box-sizing determines whether padding and border are included in those dimensions. The overflow property solves the problem when content exceeds the size of an element.

Positioning determines where an element is located on the page. There are five positioning methods: static (default), relative, absolute, fixed, and sticky. With the z-index property, we control which element is in the foreground when elements overlap.

EXAMPLE
RESULT
Property Example Description
Box Model: Size
widthwidth: 300px; Sets the width of the element content
heightheight: 200px; Sets the height of the element content
min-widthmin-width: 200px; Minimum allowed width
max-widthmax-width: 100%; Maximum allowed width
min-heightmin-height: 100px; Minimum allowed height
max-heightmax-height: 500px; Maximum allowed height
box-sizingbox-sizing: border-box; Includes padding and border in total width/height (recommended!)
Box Model: Overflow
overflowoverflow: hidden; What to do when content overflows the element (visible, hidden, scroll, auto)
overflow-xoverflow-x: scroll; Overflow in horizontal direction only
overflow-yoverflow-y: auto; Overflow in vertical direction only
Positioning
position: staticposition: static; Default – element follows the normal flow of the page
position: relativeposition: relative; top: 10px; Moves the element relative to its default position
position: absoluteposition: absolute; top: 0; right: 0; Positions the element relative to its nearest positioned ancestor
position: fixedposition: fixed; bottom: 0; Attaches the element to the browser window (does not move on scroll)
position: stickyposition: sticky; top: 0; Combination of relative and fixed – sticks when it reaches a specified position
top / right / bottom / lefttop: 20px; left: 50px; Sets offset from edges (only works with position: relative/absolute/fixed/sticky)
Stacking (Z-Index)
z-indexz-index: 100; Sets the stacking order (higher value = more in front)
Floating (Float)
floatfloat: left; Element floats left or right (left, right, none)
clearclear: both; Prevents element from floating next to a previous float element