Centering pages with css

Centering in CSS: A Complete Guide

Centering things in CSS is the poster child of CSS complaining. Why does it have to be so hard? They jeer. I think the issue isn’t that it’s difficult to do, but in that there so many different ways of doing it, depending on the situation, it’s hard to know which to reach for. So let’s make it a decision tree and hopefully make it easier. I need to center…

Is it inline or inline-* elements (like text or links)? You can center inline elements horizontally, within a block-level parent element, with just:

This will work for inline, inline-block, inline-table, inline-flex, etc. Is it a block level element? You can center a block-level element by giving it margin-left and margin-right of auto (and it has a set width , otherwise it would be full width and wouldn’t need centering). That’s often done with shorthand like this:

This will work no matter what the width of the block level element you’re centering, or the parent. Note that you can’t float an element to the center. There is a trick though. Is there more than one block level element? If you have two or more block-level elements that need to be centered horizontally in a row, chances are you’d be better served making them a different display type. Here’s an example of making them inline-block and an example of flexbox:

Unless you mean you have multiple block level elements stacked on top of each other, in which case the auto margin technique is still fine:

Vertical centering is a bit trickier in CSS. Is it inline or inline-* elements (like text or links)? Is it a single line? Sometimes inline / text elements can appear vertically centered, just because there is equal padding above and below them.

If padding isn’t an option for some reason, and you’re trying to center some text that you know will not wrap, there is a trick were making the line-height equal to the height will center the text.

Is it multiple lines? Equal padding on top and bottom can give the centered effect for multiple lines of text too, but if that isn’t going to work, perhaps the element the text is in can be a table cell, either literally or made to behave like one with CSS. The vertical-align property handles this, in this case, unlike what it normally does which is handle the alignment of elements aligned on a row. (More on that.)

Читайте также:  Python write all lines to file

If something table-like is out, perhaps you could use flexbox? A single flex-child can be made to center in a flex-parent pretty easily.

Remember that it’s only really relevant if the parent container has a fixed height (px, %, etc), which is why the container here has a height. If both of these techniques are out, you could employ the “ghost element” technique, in which a full-height pseudo-element is placed inside the container and the text is vertically aligned with that.

.ghost-center < position: relative; >.ghost-center::before < content: " "; display: inline-block; height: 100%; width: 1%; vertical-align: middle; >.ghost-center p

Is it a block-level element? Do you know the height of the element? It’s fairly common to not know the height in web page layout, for lots of reasons: if the width changes, text reflow can change the height. Variance in the styling of text can change the height. Variance in the amount of text can change the height. Elements with a fixed aspect ratio, like images, can change height when resized. Etc. But if you do know the height, you can center vertically like:

Is the element of unknown height? It’s still possible to center it by nudging it up half of it’s height after bumping it down halfway:

Do you care if the element stretches the height of the container? If you don’t, you just need the content inside vertically centered, using tables or CSS display to make elements into tables can do the trick.

Both Horizontally & Vertically

You can combine the techniques above in any fashion to get perfectly centered elements. But I find this generally falls into three camps: Is the element of fixed width and height? Using negative margins equal to half of that width and height, after you’ve absolutely positioned it at 50% / 50% will center it with great cross-browser support:

Is the element of unknown width and height? If you don’t know the width or height, you can use the transform property and a negative translate of 50% in both directions (it is based on the current width/height of the element) to center:

Can you use flexbox? To center in both directions with flexbox, you need to use two centering properties:

Can you use grid? This is just a little trick (sent in by Lance Janssen) that will pretty much work for one element:

Psst! Create a DigitalOcean account and get $200 in free credit for cloud-based hosting and services.

Comments

Good idea! I like the concept of this article and also the show/hide structure. I applaud you, sir. However, I think you’re missing the spirit behind the classic “centering is hard” complaint in a couple of places, which, at least for me, always comes back to not knowing the height of the elements. 1) Your display: table-cell fix relies on knowing the height of the child element. 2) In your “is it block level” -> “is the element of unknown height” you proceed to give the parent an explicit height. To me, that defeats the purpose of trying to handle the unknown-height scenario. If I don’t know the height of the child, it’s quite common for me to also not know the height of the parent. 3) In your “both hor & vert example” where the height is unknown, it’s a little weird to have the child be pos: absolute and imply that this is no big deal. I think pos: absolute is a major caveat when laying things out, since it can have the unintended consequence of having elements layer over one another. 4) Also, in that same pen, the element fails to stay vertically centered if it has a sibling that stretches the vertical height of the parent. Regardless, I still really like the idea of this –it’s sorely needed. I just think it would be improved if you acknowledged some of the caveats that I think are at the root of the complaint you’re trying to dismiss.

Читайте также:  line-height

1) It doesn’t actually. I updated the example to put the height on the table instead. 2) Vertical centering is only relevant if the parent has a set height. If the parent doesn’t have a set height, what are you centering within? Even if the answer is “the entire page”, then you need to set the height of (probably) both html, body 3) That’s fair. It’s just one example. In my experience, if you’re trying to center something both ways like that, it’s probably a modal, in which the absolute (or fixed) positioning is going to be used. If it’s not, you can combine any of the other techniques as needed. And there is always flexbox which I covered a bunch. 4) Demo me on this one? I’m having a hard time picturing/reproducing.

Источник

How to Center Anything with CSS — Align a Div, Text, and More

Kris Koishigawa

Kris Koishigawa

How to Center Anything with CSS - Align a Div, Text, and More

Centering things is one of the most difficult aspects of CSS.

The methods themselves usually aren’t difficult to understand. Instead, it’s more due to the fact that there are so many ways to center things.

The method you use can vary depending on the HTML element you’re trying to center, or whether you’re centering it horizontally or vertically.

In this tutorial, we’ll go over how to center different elements horizontally, vertically, and both vertically and horizontally.

Here’s an Interactive Scrim Showing How to Center Anything with CSS

How to Center Horizontally

Centering elements horizontally is generally easier than centering them vertically. Here are some common elements you may want to center horizontally and different ways to do it.

How to Center Text with the CSS Text-Align Center Property

To center text or links horizontally, just use the text-align property with the value center :

image-15

How to Center a Div with CSS Margin Auto

Use the shorthand margin property with the value 0 auto to center block-level elements like a div horizontally:

box-centered-horizontally

How to Center a Div Horizontally with Flexbox

Flexbox is the most modern way to center things on the page, and makes designing responsive layouts much easier than it used to be. However, it’s not fully supported in some legacy browsers like Internet Explorer.

To center an element horizontally with Flexbox, just apply display: flex and justify-content: center to the parent element:

box-centered-horizontally-1

How to Center Vertically

Centering elements vertically without modern methods like Flexbox can be a real chore. Here we’ll go over some of the older methods to center things vertically first, then show you how to do it with Flexbox.

Читайте также:  Oracle java class file

How to Center a Div Vertically with CSS Absolute Positioning and Negative Margins

For a long time this was the go-to way to center things vertically. For this method you must know the height of the element you want to center.

First, set the position property of the parent element to relative .

Then for the child element, set the position property to absolute and top to 50% :

box-centered-vertically-1

But that really just vertically centers the top edge of the child element.

To truly center the child element, set the margin-top property to -(half the child element’s height) :

box-centered-vertically-final

How to Center a Div Vertically with Transform and Translate

If you don’t know the height of the element you want to center (or even if you do), this method is a nifty trick.

This method is very similar to the negative margins method above. Set the position property of the parent element to relative .

For the child element, set the position property to absolute and set top to 50% . Now instead of using a negative margin to truly center the child element, just use transform: translate(0, -50%) :

box-centered-vertically-final-1

Note that translate(0, -50%) is shorthand for translateX(0) and translateY(-50%) . You could also write transform: translateY(-50%) to center the child element vertically.

How to Center a Div Vertically with Flexbox

Like centering things horizontally, Flexbox makes it super easy to center things vertically.

To center an element vertically, apply display: flex and align-items: center to the parent element:

box-centered-vertically-final-2

How to Center Both Vertically and Horizontally

How to Center a Div Vertically and Horizontally with CSS Absolute Positioning and Negative Margins

This is very similar to the method above to center an element vertically. Like last time, you must know the width and height of the element you want to center.

Set the position property of the parent element to relative .

Then set the child’s position property to absolute , top to 50% , and left to 50% . This just centers the top left corner of the child element vertically and horizontally.

To truly center the child element, apply a negative top margin set to half the child element’s height, and a negative left margin set to half the child element’s width:

box-centered-vertically-and-horizontally

How to Center a Div Vertically and Horizontally with Transform and Translate

Use this method to center an element vertically and horizontally if you don’t know its exact dimensions and can’t use Flexbox.

First, set the position property of the parent element to relative .

Next, set the child element’s position property to absolute , top to 50% , and left to 50% .

Finally, use transform: translate(-50%, -50%) to truly center the child element:

box-centered-vertically-and-horizontally-1

How to Center a Div Vertically and Horizontally with Flexbox

Flexbox is the easiest way to center an element both vertically and horizontally.

This is really just a combination of the two previous Flexbox methods. To center the child element(s) horizontally and vertically, apply justify-content: center and align-items: center to the parent element:

box-centered-vertically-and-horizontally-2

That’s everything you need to know to center with the best of ’em. Now go forth and center all the things.

Источник

Оцените статью