CSS Align
CSS has several properties for aligning elements.
CSS Layout - Horizontal & Vertical Align
CSS has several properties for aligning elements.
Example 1: Center Align Elements
To horizontally center a block element (like <div>), use margin: auto;.
Setting the width of the element will prevent it from stretching out to the edges of its container.
This div is centered.
Example 2: Center Align Text
To just center the text inside an element, use text-align: center;.
This text is centered.
Example 3: Center an Image
To center an image, make it into a block element and apply margin: auto;.
Example 4: Left and Right Align - Using Position
One method for aligning elements is to use position: absolute;.
This is right aligned.
Example 5: Left and Right Align - Using Float
Another method for aligning elements is to use the float property.
This is right aligned.
Example 6: Center Vertically - Using Padding
There are many ways to center an element vertically in CSS. A simple solution is to use top and bottom padding.
I am vertically centered.
Example 7: Center Vertically - Using Flexbox
You can also use flexbox to center things.
I am vertically and horizontally centered.