The reason is that HTML elements used for content can either be inline-level elements or block-level elements. Both of these types of elements behave differently. Just as the browser has built-in styles for these elements, we can apply our own styles so that we override built-in styling.
Centering Text
Centering text within an element is a very easy task. You can simply use the CSS text-align styling property to center the text. Previous, the HTML
The HTML
The CSS
Horizontally Center an Element
In this example, we will center a block-level element such as a div. We want the div to remain in the center (horizontally) of the window, no matter what the size of the window is. Because the window size may change, we can not use absolute positioning to place it at a specific point because the element will not be centered for different window sizes. All we need to do is apply a set width and auto margin settings. As long as your block has a width set, it will be centered inside the containing element, assuming non-absolute positioning. However, the text that is contained inside of the block element will not be centered.
The HTML
The CSS
Keep in mind for this approach to work, you must declare a width as well as left and right margins. Declaring the height of the element is not needed. In addition, this technique will not help you with regard to vertical centering on the page.
Horizontally Center An Absolute Positioned Element
In this example, we will center an element with an absolute position within another element that has a relative position.
The HTML
The CSS
In this example, we placed an element with an absolute positioned item inside of a relatively positioned item. To center it horizontally, we need to set work with the left position as well as apply a negative left margin property. This approach allows you to move the absolute positioned element to the middle of its parent element, then move it to the left 1/2 of its width. This causes the element to be placed exactly in the horizontal center of its parent element.
How About Fluid Widths?
With more and more responsive web designs, static widths are being used less and less. In the following example, rather than applying a static width, percentages are used.
Horizontally Center an Image
Centering an image or images can be a little bit trickier. One approach is to place the image or images within a block-level element such as a div and apply the text-align: center styling. While most browsers will display the image or images centered within the element, using the text-align property, it is not recommended by the W3C. The preferred approach is to instruct the browser that the image is a block element. As a block element, you can center it as you would in the previous example.
The HTML
The CSS
Conclusion
So as you can see, it is important to get a grasp on the behavior of the elements you are trying to manipulate. With a little bit of practice, you should be able to handle just about almost any centering situation using CSS.