![]() |
VOOZH | about |
CSS alignment techniques are essential for positioning items and distributing space between content. These techniques include horizontal and vertical alignment using various properties such as margin: auto, position: absolute, text-align, and padding. This article covers multiple methods to achieve alignment in CSS.
The margin: auto property centers a block element horizontally.
Note: Using margin: auto will not work in IE8 unless a !DOCTYPE is declared.
Example 1: This example describes the CSS align using the margin: auto property.
Output:
The position: absolute property aligns elements relative to their containing block.
Note: The position properties top, bottom, left, and right will not work unless the position property is set first.
Example 2: This example describes the CSS align using the position: absolute; property.
Output:
The text-align: center property centers text within its container
Example 3: This example describes the CSS align using the text-align: center; property.
Output:
The padding property can be used to center elements vertically.
Example 4: This example describes the CSS align using the padding property.
Output:
Combining padding and text-align: center to center elements both vertically and horizontally.
Example 5: This example describes the CSS align using the padding & text-align properties.
Output:
CSS alignment techniques provide a range of options for positioning elements and managing the space between them. Understanding and using properties like margin, position, text-align, and padding effectively can significantly enhance the layout and design of web pages.