Skip to content

Instantly share code, notes, and snippets.

@nicco88
Created February 15, 2017 07:30
Show Gist options
  • Save nicco88/4c87af9283705308f409da66cc05af86 to your computer and use it in GitHub Desktop.
Save nicco88/4c87af9283705308f409da66cc05af86 to your computer and use it in GitHub Desktop.
Centering in CSS: A Complete Guide
1. Horizontally
- Is is inline or inline-* elements (like text or links)?
-> You can center inline elements horizontally, within a block-level parent element, with just:
.center-children {
text-align: center;
}
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:
.center-me {
margin: 0 auto;
}
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: https://css-tricks.com/float-center/).
- 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.
2. Vertically
- Is is 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.
.link {
padding-top: 30px;
padding-bottom: 30px;
}
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.
.center-text-trick {
height: 100px;
line-height: 100px;
white-space: nowrap;
}
- 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.
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.
.flex-center-vertically {
display: flex;
justify-content: center;
flex-direction: column;
height: 400px;
}
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 {
display: inline-block;
vertical-align: middle;
}
- Is it a block-level element?
/************/
There are two approaches to centering a column <div> in Bootstrap 3:
Approach 1 (offsets):
The first approach uses Bootstrap's own offset classes so it requires no change in markup and no extra CSS. The key is to set an offset equal to half of the remaining size of the row. So for example, a column of size 2 would be centered by adding an offset of 5, that's (12-2)/2.
In markup this would look like:
<div class="row">
<div class="col-md-2 col-md-offset-5"></div>
</div>
Now, there's an obvious drawback for this method, it only works for even column sizes, so only .col-X-2, .col-X-4, col-X-6, col-X-8 and col-X-10 are supported.
Approach 2 (the old margin:auto)
You can center any column size by using the proven margin: 0 auto; technique, you just need to take care of the floating that is added by Bootstrap's grid system. I recommend defining a custom CSS class like the following:
.col-centered{
float: none;
margin: 0 auto;
}
Now you can add it to any column size at any screen size and it will work seamlessly with Bootstrap's responsive layout:
<div class="row">
<div class="col-lg-1 col-centered"></div>
</div>
Note: With both techniques you could skip the .row element and have the column centered inside a .container but you would notice a minimal difference in the actual column size because of the padding in the container class.
Update:
Since v3.0.1 Bootstrap has a built-in class named center-block that uses margin: 0 auto but is missing float:none. You can add that to your CSS to make it work with the grid system.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment