CSS Positioning and Layout
CSS Positioning and Layout
usage:
position: static;
Relative Positioning
usage:
position: relative;
Absolute Positioning
Removes the element from the normal document flow and positions it relative to
its nearest positioned ancestor.
If no positioned ancestor is found, the element is positioned relative to the initial
containing block (usually the viewport).
Usage:
position: absolute;
Fixed Positioning
Positions an element relative to the viewport, regardless of its containing element.
The element remains fixed in its position even when the page is scrolled.
Usage:
position: fixed;
Sticky Positioning
Acts like a hybrid of relative and fixed positioning.
The element is treated as relatively positioned until it reaches a specified
threshold, after which it becomes fixed.
Useful for creating elements that stick to the top or bottom of the viewport as the
user scrolls.
Usage:
position: sticky;
Example on Positioning
.container {
position: relative;
.absolute {
position: absolute;
top: 20px;
left: 20px;
}
Example on Positioning
.fixed {
position: fixed;
top: 20px;
right: 20px;
.sticky {
position: sticky;
top: 20px;
}
Flexbox Layout
Flexbox, short for Flexible Box, is a layout model in CSS designed to provide a
more efficient way to layout, align, and distribute space among items in a
container, even when their size is unknown or dynamic.
Key Concepts:
1. Flex Container:
● An element whose display property is set to flex or inline-flex.
● Acts as a parent container for flex items.
1. Flex Items:
● Children of a flex container.
● Automatically laid out within the flex container according to the flexbox layout
rules.
Key Concepts:
3. Main Axis and Cross Axis:
● Flexbox layout operates along two axes: the main axis and the cross axis.
● The main axis is determined by the flex-direction property and is the primary
axis along which flex items are laid out.
● The cross axis is perpendicular to the main axis.
flex-direction: Defines the direction in which flex items are placed in the flex
container (row, row-reverse, column, column-reverse).
justify-content: Aligns flex items along the main axis of the flex container.
align-items: Aligns flex items along the cross axis of the flex container.
align-content: Aligns flex lines within the flex container when there is extra space
in the cross axis.
Flex Item Properties
flex: Specifies the flexibility of a flex item.
flex-grow: Determines how much a flex item should grow relative to the other flex
items.
flex-shrink: Determines how much a flex item should shrink relative to the other
flex items.
flex-basis: Specifies the initial size of a flex item before it stretches or shrinks.
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
.item {
flex: 1;