Understanding CSS alignment properties is essential for creating well-structured and visually appealing web layouts. This tutorial will cover text alignment, vertical alignment, flexbox alignment, grid alignment, and positional alignment with detailed explanations, best practices, and practical examples.
Text alignment controls the horizontal alignment of text within an element.
Left Aligntext-align: left; aligns the text to the left within its containing element. This is the default behavior for most elements.
.text-left { text-align: left; }
text-align: center; centers the text within its containing element. This is useful for headings or any content that needs emphasis.
.text-center { text-align: center; }
text-align: right; aligns the text to the right within its containing element.
.text-right { text-align: right; }
text-align: justify; spaces the text so that the left and right sides are aligned.
.text-justify { text-align: justify; }
Vertical alignment controls the vertical alignment of an element within its container. This is typically used with inline or table-cell elements.
Top Alignvertical-align: top; aligns the element to the top of its containing element.
.align-top {
vertical-align: top;
}
.vertical-align-container {
display: table;
height: 200px;
border: 1px solid black;
}
.vertical-align-example {
display: table-cell;
border: 1px solid red;
padding: 10px;
}
vertical-align: middle; aligns the element to the middle of its containing element.
.align-middle {
vertical-align: middle;
}
.vertical-align-container {
display: table;
height: 200px;
border: 1px solid black;
}
.vertical-align-example {
display: table-cell;
border: 1px solid red;
padding: 10px;
}
vertical-align: bottom; aligns the element to the bottom of its containing element.
.align-bottom {
vertical-align: bottom;
}
.vertical-align-container {
display: table;
height: 200px;
border: 1px solid black;
}
.vertical-align-example {
display: table-cell;
border: 1px solid red;
padding: 10px;
}
For vertical alignment to work correctly with table-cell elements, the parent container should have a defined height.
Flexbox alignment provides more advanced alignment options for flex containers and their children.
Align Itemsalign-items aligns flex items along the cross axis (vertical axis in a row layout).
.flexbox-container {
display: flex;
border: 1px solid black;
margin-bottom: 10px;
height: 200px;
}
.flex-item {
background-color: lightgray;
margin: 5px;
padding: 10px;
border: 1px solid red;
}
.align-items-start {
align-items: flex-start;
}
.align-items-center {
align-items: center;
}
.align-items-end {
align-items: flex-end;
}
align-self allows individual flex items to have different alignment than the rest of the flex container.
.flexbox-align-self-container {
display: flex;
border: 1px solid black;
margin-bottom: 10px;
height: 200px;
}
.flex-item {
background-color: lightgray;
margin: 5px;
padding: 10px;
border: 1px solid red;
}
.align-self-start {
align-self: flex-start;
}
.align-self-center {
align-self: center;
}
.align-self-end {
align-self: flex-end;
}
justify-content aligns flex items along the main axis (horizontal axis in a row layout).
.flexbox-justify-content-container {
display: flex;
border: 1px solid black;
margin-bottom: 10px;
height: 200px;
}
.flex-item {
background-color: lightgray;
margin: 5px;
padding: 10px;
border: 1px solid red;
}
.justify-content-start {
justify-content: flex-start;
}
.justify-content-center {
justify-content: center;
}
.justify-content-end {
justify-content: flex-end;
}
.justify-content-space-between {
justify-content: space-between;
}
.justify-content-space-around {
justify-content: space-around;
}
Grid alignment provides alignment options for grid containers and their children.
Align Itemsalign-items aligns grid items along the cross axis (vertical axis in a row layout).
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
border: 1px solid black;
margin-bottom: 10px;
height: 200px;
}
.grid-item {
background-color: lightgray;
margin: 5px;
padding: 10px;
border: 1px solid red;
}
.align-items-start {
align-items: start;
}
.align-items-center {
align-items: center;
}
.align-items-end {
align-items: end;
}
justify-items aligns grid items along the main axis (horizontal axis in a row layout).
.grid-justify-items-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
border: 1px solid black;
margin-bottom: 10px;
height: 200px;
}
.grid-item {
background-color: lightgray;
margin: 5px;
padding: 10px;
border: 1px solid red;
}
.justify-items-start {
justify-items: start;
}
.justify-items-center {
justify-items: center;
}
.justify-items-end {
justify-items: end;
}
Positional alignment uses the properties top, right, bottom, and left to position elements precisely within their containing element.
Using Positional AlignmentPositional alignment can be used to align elements in relation to their containing element or to other elements. It works when the element is positioned relatively, absolutely, or fixed.
// first example
.position-example {
position: relative;
width: 400px;
height: 400px;
background-color: lightblue;
border: 1px solid black;
}
.position-example::before {
content: "Top: 10px; Left: 0;";
position: absolute;
top: 10px;
left: 0;
background-color: #7FFFD4;
padding: 5px;
}
.position-example::after {
content: "Bottom: 10px; Right: 8px;";
position: absolute;
bottom: 10px;
right: 8px;
background-color: #7FFFD4;
padding: 5px;
}
// second example
.position-example-2 {
position: relative;
width: 400px;
height: 400px;
background-color: green;
border: 1px solid black;
}
.div1 {
position: absolute;
top: 0;
right: 0;
background-color: yellow;
padding: 5px;
}
.div2 {
position: absolute;
bottom: 0;
left: 0;
background-color: yellow;
padding: 5px;
}
The position property needs to be set for top, right, bottom, and left to work. It can be set to relative, absolute, or fixed.
Understanding and utilizing these alignment properties will enhance your ability to create well-structured and visually appealing web layouts.