Absolute positioning removes the HTML element from the normal document flow, and the document will reserve no space for that element in the document. The absolutely positioned element will be arranged according to the parent.
The absolutely positioned element can be horizontally centered using the following CSS properties and values.
margin-left: auto;
margin-right: auto;
left: 0;
right: 0;
Applying these CSS properties to the image will provide a centered image. The margin-left
and margin-right
are set to auto, indicating that the browser will select the suitable margin. The left
and right
properties will place the positioned element in HTML horizontally.
In the CSS tab below:
Let's use the example above to center align the Educative image vertically. The absolutely positioned element will be vertically and horizontally centered using the following CSS properties and values.
margin: auto
left: 0
right: 0
top: 0
bottom: 0
Note: The code above will center the block horizontally and vertically. To only vertically center the block
- Set
margin-top
andmargin-bottom
toauto.
- Set
top
andbottom
to0
.
We used margin
property as it indicates margin to all the directions. The top
and bottom
properties will place the positioned element in HTML vertically.
In the CSS tab below: