16/08/2016

CSS Horizontal Align

In CSS, we can use different properties (like margin, position or float) to align elements horizontally.

Using the margin Property for Center Aligning

By setting the margin-left and margin-right property to "auto" for a block element, specifies the available margin equally.

The will define the element as a center aligned:

Example
#example-center 
{
 margin-left: auto;
 margin-right: auto;
 width: 50%;
 background-color: #336699;
}

Using the position Property to Align Left and Right

Using the absolute positioning also, we can achieve the left or right alignment of HTML element:
#example-right
{
    position: absolute;
    right: 0px;
    width: 150px;
    background-color: #336699;
}

Using the float Property to Align Left and Right

Another way to aligning elements is by using the float property:
#example-right
{
 float: right;
 width: 150px;
 background-color: #336699;
}