CSS ALIGNMENT

Text inside the block-level elements can aligned by setting the text-align properly.

EXAMPLE

1.	h1 {
2.	    text-align: center;
3.	}
4.	p {
5.	    text-align: left;
6.	}

Center Alignment Using the margin Property

Center alignment of a block-level element is one of the most important implications of the CSS margin property. For example, the <div> container can be aligned horizontally center by setting the left and right margins to auto.

EXAMPLE

1.	div {
2.	    width: 50%;
3.	    margin: 0 auto;
4.	}
The style rules in the above example center align the <div> element horizontally.

Aligning Elements Using the position Property

The CSS position in conjunction with the leftrighttop and bottom property can be used to align elements with respect to the document’s viewport or containing parent element.

EXAMPLE

1.	.up {
2.	    position: absolute;
3.	    top: 0;
4.	}
5.	.down {
6.	    position: absolute;
7.	    bottom: 0;
8.	}

Left and Right Alignment Using the float Property

The float CSS property can be used to align an element to the left or right of its containing block in such a way that other content may flow along its side.

Hence, if an element is floated to the left, content will flow along its right side. Conversely, if the element is floated to the right, content will flow along its left side.

EXAMPLE

1.	div {
2.	    width: 200px;
3.	    float: left;
4.	}

Clearing Floats

One of the most confusing things about working with the float-based layouts is the collapsing parent. The parent element doesn’t stretch up automatically to accommodate the floated elements. Though, this isn’t always obvious if the parent doesn’t contain any visually noticeable background or borders, but it is important to be aware of and must dealt with to prevent strange layout and cross-browser problem. See the illustration below:

You can see the <div> element doesn’t stretch up automatically to accommodate the floated images. This problem can be fixed by clearing the float after the floated elements in the container but before the closing tag of the container element.

Fixing the Collapsed Parent

There are several ways to fix the CSS collapsing parent issue. The following section will describe you these solutions along with the live examples.

Solution 1: Float the Container

The easiest way to fix this problem is to float the containing parent element.

EXAMPLE

1.	.container {
2.	    float: left;
3.	    background: #f2f2f2;
4.	}

Solution 2: Using the Empty Div

This is an old fashioned way but is an easy solution and works across every browser.

EXAMPLE

1.	.clearfix {
2.	    clear: both;
3.	}
4.	/* html code snippet */
5.	<div class="clearfix">&nbsp;</div>

You could also do this by means of a <br> tag. But this method is not recommended since it adds nonsemantic code to your markup.

Solution 3: Using the :after Pseudo-Element

The :after pseudo-element in conjunction with the content property has been used quite extensively to resolve float-clearing issues.

EXAMPLE

1.	.clearfix:after {
2.	    content: ".";
3.	    display: block;
4.	    height: 0;
5.	    clear: both;
6.	    visibility: hidden;
7.	}

The class .clearfix is applied to any container that has floating children.

Categories: CSS