
This is where we apply a new property called transform. But as in the previous example, we don’t want the edges to be lined up with the center, we want the center of our square to fit right on top of the center of our page. Though this method also uses absolute positioning and “left: 50%”, I’ve also applied two more properties to the element.īy setting the top property to 50% as well, I’m telling the browser to line up the top edge of our red square with the middle of the page vertically.

Let’s center our red square both horizontally and vertically. Up until now we’ve only dealt with centering things horizontally, but what if we want to put something right in the middle of the page? In our case it’s 50px (regardless of the width of the element, it will always be half).Ĭentering with Transform/Translate Transform/Translate Method In order to line things up and offset the extra space, we apply a margin-left of half of the green square’s width. This tells the browser to move the left edge 50% to the right.īut if you’re recreating this example, we don’t want the left edge to be in the middle, we want the middle of the square to line up with the middle of the page. In this example, we use a green square (what a beautiful green!) It’s the same size as the other examples, so our width is still 100px.Īs you can see, I’ve given “position: absolute” and applied “left: 50%” to our green square. Set a margin-left of half of the element’s width.Set the element’s position property to absolute.If we we want to simply center an element horizontally on the page, like we’ve been doing in the first two methods, there are three steps we need to remember: Well, it’s important because this can cause overlapping of elements if used incorrectly. The “0” portion can be set to any number of pixels you want for the top and bottom margins.Īnother cool trick is just setting either margin-left to auto or margin-right to auto, which allows us to push our div to either the right or left side of the page, respectively - give this a try! Absolute Positioning MethodĪbsolute positioning an element allows us to essentially place the element wherever we want it on the page…with one drawback.Ībsolute positioning removes the element from the flow of the page. By setting the width, the browser will automatically distribute the right amount of margin on either side of the yellow box. This is important, because without the 100px width I have defined, the browser will not be able to render the left and right margins needed to center the yellow box. Margin: 0 auto is shorthand for setting the top and bottom margins to zero, and the left and right margins to auto. We can simply apply margin: 0 auto to our yellow box, as long as we have a defined width. Using this method, we don’t need a parent element. Margin Auto MethodĪnother common way of centering is using the margin auto method. By setting the display property of my blue square to inline-block, we ensure that the blue square will only span the width that I have set, which is 100px.Īdding multiple child elements within the parent element (blue squares in this example), will center all of them. This is because, by default, a div’s display property is set to block, meaning it will span the whole width of the page. In order to center my blue square, I had to create a parent element and set the display property of my blue square to inline-block. In my example with the blue square, I enclose it with another div called blue-square-container. then set the inside div to display: inline-block.set text-align: center to parent element.enclose the div that you want to center with a parent element (commonly known as a wrapper or container).It’s used mostly for centering text on your HTML page, but it can be used to center divs as well. The “text-align: center” method is perhaps the most common one you’ll see for centering.

Centering with Text-Align, Margin Auto, and Absolute Positioning Methods Text-Align Method
