I use bootstrap and am trying to center a div(span7) like that:
Lorem ips
-
Update
As of BS3 there's a .center-block helper class. From the docs:
// Classes
.center-block {
display: block;
margin-left: auto;
margin-right: auto;
}
// Usage as mixins
.element {
.center-block();
}
There is hidden complexity in this seemingly simple problem. All the answers given have some issues.
1. Create a Custom Class (Major Gotcha)
Create .col-centred class, but there is a major gotcha.
.col-centred {
float: none !important;
margin: 0 auto;
}
Centred content.
Centred content.
The Gotcha
Bootstrap requires columns add up to 12. If they do not they will overlap, which is a problem. In this case the centred column will overlap the column above it. Visually the page may look the same, but mouse events will not work on the column being overlapped (you can't hover or click links, for example). This is because mouse events are registering on the centred column that's overlapping the elements you try to click.
The Fixes
You can resolve this issue by using a clearfix element. Using z-index to bring the centred column to the bottom will not work because it will be overlapped itself, and consequently mouse events will work on it.
I get overlapped by `col-lg-7 centered` unless there's a clearfix.
Or you can isolate the centred column in its own row.
Look I am in my own row.
2. Use col-lg-offset-x or spanx-offset (Major Gotcha)
Centred content.
Centred content.
The first problem is that your centred column must be an even number because the offset value must divide evenly by 2 for the layout to be centered (left/right).
Secondly, as some have commented, using offsets is a bad idea. This is because when the browser resizes the offset will turn into blank space, pushing the actual content down the page.
3. Create an Inner Centred Column
This is the best solution in my opinion. No hacking required and you don't mess around with the grid, which could cause unintended consequences, as per solutions 1 and 2.
.col-centred {
margin: 0 auto;
}
Look I am in my own row.