Center a column using Twitter Bootstrap 3
How do I center a div of one column size within the container (12 columns) in Twitter Bootstrap 3.
Please see the starter fiddle.
<body class="container">
<div class="col-lg-1 col-offset-6 centered">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
So, I want a div
, with a class centered
to be centered within the container. I may use a row if there are multiple divs, but for now I just want a div with the size of one column centered within the container (12 columns).
I am also not sure the above approach is good enough as the intention is not to offset the div
by half. I do not need free spaces outside the div
and the contents of the div
shrink in proportion. I want to empty space outside the div to be evenly distributed (shrink till the container width == one column).
css twitter-bootstrap twitter-bootstrap-3 centering
add a comment |
How do I center a div of one column size within the container (12 columns) in Twitter Bootstrap 3.
Please see the starter fiddle.
<body class="container">
<div class="col-lg-1 col-offset-6 centered">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
So, I want a div
, with a class centered
to be centered within the container. I may use a row if there are multiple divs, but for now I just want a div with the size of one column centered within the container (12 columns).
I am also not sure the above approach is good enough as the intention is not to offset the div
by half. I do not need free spaces outside the div
and the contents of the div
shrink in proportion. I want to empty space outside the div to be evenly distributed (shrink till the container width == one column).
css twitter-bootstrap twitter-bootstrap-3 centering
add a comment |
How do I center a div of one column size within the container (12 columns) in Twitter Bootstrap 3.
Please see the starter fiddle.
<body class="container">
<div class="col-lg-1 col-offset-6 centered">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
So, I want a div
, with a class centered
to be centered within the container. I may use a row if there are multiple divs, but for now I just want a div with the size of one column centered within the container (12 columns).
I am also not sure the above approach is good enough as the intention is not to offset the div
by half. I do not need free spaces outside the div
and the contents of the div
shrink in proportion. I want to empty space outside the div to be evenly distributed (shrink till the container width == one column).
css twitter-bootstrap twitter-bootstrap-3 centering
How do I center a div of one column size within the container (12 columns) in Twitter Bootstrap 3.
Please see the starter fiddle.
<body class="container">
<div class="col-lg-1 col-offset-6 centered">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
So, I want a div
, with a class centered
to be centered within the container. I may use a row if there are multiple divs, but for now I just want a div with the size of one column centered within the container (12 columns).
I am also not sure the above approach is good enough as the intention is not to offset the div
by half. I do not need free spaces outside the div
and the contents of the div
shrink in proportion. I want to empty space outside the div to be evenly distributed (shrink till the container width == one column).
css twitter-bootstrap twitter-bootstrap-3 centering
css twitter-bootstrap twitter-bootstrap-3 centering
edited Jan 31 at 16:12
Gianfranco P.
4,5192842
4,5192842
asked Aug 9 '13 at 18:24
bsrbsr
21.4k65181273
21.4k65181273
add a comment |
add a comment |
31 Answers
31
active
oldest
votes
1 2
next
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.
6
@DiegoFernandoMurilloValenci.clearfix
wouldn't work in this case because it doesn't remove the float property on the elements (which is needed to center usingmargin: 0 auto
), it just makes a container wrap any floated elements inside
– koala_dev
Jul 8 '14 at 23:10
4
can't use this on a column, because nofloat:none;
– airtonix
Feb 28 '15 at 3:50
10
Responsive Centered Bootstrap Columns minimit.com/articles/solutions-tutorials/…
– Gothburz
Oct 24 '15 at 23:44
2
If you want to center more then one column in a single line, see this answer stackoverflow.com/questions/28683329/…
– Conor Svensson
May 17 '16 at 11:19
3
The syntax for offset changed in Bootstrap4 (as of latest version). Using this response as an example: the class col-md-offset-5 should now be offset-md-5.
– lgants
Sep 10 '17 at 23:46
|
show 17 more comments
The preferred method of centering columns is to use "offsets" (ie: col-md-offset-3
)
Bootstrap 3.x centering examples
For centering elements, there is a center-block
helper class.
You can also use text-center
to center text (and inline elements).
Demo: http://bootply.com/91632
EDIT - As mentioned in the comments, center-block
works on column contents and display:block
elements, but won't work on the column itself (col-*
divs) because Bootstrap uses float
.
Update 2018
Now with Bootstrap 4, the centering methods have changed..
text-center
is still used fordisplay:inline
elements
mx-auto
replacescenter-block
to centerdisplay:block
elements
offset-*
ormx-auto
can be used to center grid columns
mx-auto
(auto x-axis margins) will center display:block
or display:flex
elements that have a defined width, (%
, vw
, px
, etc..). Flexbox is used by default on grid columns, so there are also various flexbox centering methods.
Demo Bootstrap 4 Horizontal Centering
For vertical centering in BS4 see https://stackoverflow.com/a/41464397/171456
29
Unfortunately this helper class doesn't work with the column system as it doesn't take care of the float property. See this demo for example.
– koala_dev
Dec 5 '13 at 18:03
13
That only requires a center-block { float: none; }, and it works. bootply.com/122268
– Matias Vad
Mar 16 '14 at 11:33
5
not sure about the OP, but I got here looking for thetext-center
detail - which none of the other answers provided. This one gets my vote.
– domoarigato
Feb 2 '15 at 19:41
1
can't use this on a column, because nofloat:none;
– airtonix
Feb 28 '15 at 3:50
1
I edited the answer regarding offsets.
– Zim
Feb 10 '16 at 13:41
|
show 1 more comment
Now Bootstrap 3.1.1 is working with .center-block
, and this helper class works with the column system.
Bootstrap 3 Helper Class Center.
Please check this jsfiddle DEMO:
<div class="container">
<div class="row">
<div class="center-block">row center-block</div>
</div>
<div class="row">
<div class="col-md-6 brd">
<div class="center-block">1 center-block</div>
</div>
<div class="col-md-6 brd">
<div class="center-block">2 center-block</div>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-2 col-center-block">row col-xs-2 col-center-block</div>
</div>
Row column center using col-center-block
helper class.
.col-center-block {
float: none;
display: block;
margin: 0 auto;
/* margin-left: auto; margin-right: auto; */
}
I got this working without addingfloat:none
by making sure the div centre-block has thestyle="width : ?px"
style applied to it. For example an image width. Works with 3.2.2
– ppumkin
Mar 4 '15 at 21:31
margin: 0 auto; - enough instead margin-left + margin-right
– mmike
Aug 29 '16 at 10:06
add a comment |
Simply add this to your custom CSS file, editing Bootstrap CSS files directly is not recommended and cancells your ability to use a cdn.
.center-block {
float: none !important
}
Why?
Bootstrap CSS (Ver 3.7 and lower) uses: margin: 0 auto;, but it gets overridden by the float property of the size container.
PS:
After you add this class don't forget to set classes by the right order.
<div class="col-md-6 center-block">Example</div>
2
Only made .center-block to work by doing like this answer.
– João Martins
Mar 26 '15 at 14:06
1
I'm using Bootstrap3.3.7
and this is the only method working for me!
– illagrenan
Sep 6 '16 at 14:52
1
this works. but don't think!important
is necessary
– Gianfranco P.
Feb 23 '17 at 13:51
@GianfrancoP. P. Your right, but... that depends on the load order and i wanted to provide a foolproof method.
– Sagive SEO
Sep 13 '17 at 21:37
add a comment |
Bootstrap 3 now has a built-in class for this .center-block
.center-block {
display: block;
margin-left: auto;
margin-right: auto;
}
If you are still using 2.X then just add this to your CSS.
this would probably give you the desired effect the easiest. Also throw width on the .centered class or add a second class if you are going for a more modular approach
– Matt Lambert
Aug 9 '13 at 21:46
2
Adding float: none; to .centered lets you move the class up into container. keeps the code cleaner
– Adam Patterson
Jan 29 '14 at 3:50
7
can't use this on a column, because nofloat:none;
– airtonix
Feb 28 '15 at 3:49
add a comment |
My approach to center columns is to use display: inline-block
for columns and text-align: center
for the container parent.
You just have to add the CSS class 'centered' to the row
.
HTML:
<div class="container-fluid">
<div class="row centered">
<div class="col-sm-4 col-md-4">
Col 1
</div>
<div class="col-sm-4 col-md-4">
Col 2
</div>
<div class="col-sm-4 col-md-4">
Col 3
</div>
</div>
</div>
CSS:
.centered {
text-align: center;
font-size: 0;
}
.centered > div {
float: none;
display: inline-block;
text-align: left;
font-size: 13px;
}
JSfiddle:
http://jsfiddle.net/steyffi/ug4fzcjd/
2
This is the only answer which worked 100% of the time - The others worked, but not when bootstrap switched to its mobile view (the items were moved to the left side).
– Levi Fuller
Jul 6 '16 at 16:10
This was the only one which worked me as well, .center-block only works for one column and I need to center all columns independent of how many it is.
– cjohansson
Dec 8 '16 at 12:13
Only solution that worked for me too. Other ones worked only when the browser was a window, (not fullscreen).
– kiwicomb123
Jun 29 '17 at 3:30
add a comment |
Bootstrap version 3 has a .text-center class.
Just add this class:
text-center
It will simply load this style:
.text-center {
text-align: center;
}
Example:
<div class="container-fluid">
<div class="row text-center">
<div class="col-md-12">
Bootstrap 4 is coming....
</div>
</div>
</div>
Clarify where this class will be added?
– aksu
Sep 20 '16 at 16:34
Hi @aksu, you don't have to add any .css. Look at the example provided.
– JoshYates1980
Oct 28 '16 at 15:42
add a comment |
With Bootstrap v4, this can be accomplished just by adding .justify-content-center
to the .row
<div>
<div class="row justify-content-center">
<div class="col-1">centered 1 column</div>
</div>
https://getbootstrap.com/docs/4.0/utilities/flex/#justify-content
add a comment |
This works. A hackish way probably, but it works nicely. It was tested for responsive (Y).
.centered {
background-color: teal;
text-align: center;
}
works only because the element has diplay inline. Animg
may be set as a block.
– ProfileTwist
Aug 12 '13 at 18:34
2
But it works and I do not see a reason to down vote.
– Ali Gajani
Aug 12 '13 at 18:34
Perhaps in your isolated testing case. I would add.centered img { display: inline; }
to your answer and then it would be worthy of an up vote instead.
– ProfileTwist
Aug 13 '13 at 15:02
No problem. I am sure the OP will find his/her answer :)
– Ali Gajani
Aug 13 '13 at 15:11
in bootstrap 3, the .text-center class does the same thing natively, as I learned above.
– domoarigato
Feb 2 '15 at 19:43
add a comment |
You can use text-center
for the row, and make sure the internal divs have display:inline-block
( with not float
)
as:
<div class="container">
<div class="row text-center" style="background-color : black;">
<div class="redBlock">A red block</div>
<div class="whiteBlock">A white block</div>
<div class="yellowBlock">A yellow block</div>
</div>
</div>
and css:
.redBlock {
width: 100px;
height: 100px;
background-color: aqua;
display: inline-block
}
.whiteBlock {
width: 100px;
height: 100px;
background-color: white;
display: inline-block
}
.yellowBlock {
width: 100px;
height: 100px;
background-color: yellow;
display: inline-block
}
The fiddle:
http://jsfiddle.net/DTcHh/3177/
add a comment |
Just set your one column that displays content to col-xs-12 (mobile-first ;-) and configure the container only to control how wide you want your centred content to be, so:
.container {
background-color: blue;
}
.centered {
background-color: red;
}
<body class="container col-xs-offset-3 col-xs-6">
<div class="col-xs-12 centered">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
<body class="container col-xs-offset-1 col-xs-10">
<div class="col-xs-12 centered">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
For a demo, see http://codepen.io/Kebten/pen/gpRNMe :-)
add a comment |
To center the col- we need to use the below code. cols are floater elements besides margin auto. We will also set it to float none,
<body class="container">
<div class="col-lg-1 col-md-4 centered">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
To center the above col-lg-1 with class of centered, we will write:
.centered {
float: none;
margin-left: auto;
margin-right: auto;
}
To center the content inside the div, use text-align:center
,
.centered {
text-align: center;
}
If you want to center it only on the desktop and larger screen, not on mobile, then use the following media query.
@media (min-width: 768px) {
.centered {
float: none;
margin-left: auto;
margin-right: auto;
}
}
And to center the div only on mobile version, use the below code.
@media (max-width: 768px) {
.centered {
float: none;
margin-left: auto;
margin-right: auto;
}
}
add a comment |
This is probably not the best answer, but there is one more creative solution to this. As pointed out by koala_dev the column offsetting works only for even column sizes. However, by nesting rows you can achieve centering uneven columns as well.
To stick with the original question where you want to center a column of 1 inside a grid of 12.
- Center a column of 2 by offsetting it 5
- Make a nested row, so you get a new 12 columns inside your 2 columns.
- Since you want to center a column of 1, and 1 is "half" of 2 (what we centered in step 1), you now need to center a column of 6 in your nested row, which is easily done by offsetting it 3.
For example:
<div class="container">
<div class="row">
<div class="col-md-offset-5 col-md-2">
<div class="row">
<div class="col-md-offset-3 col-md-6">
centered column with that has an "original width" of 1 col
</div>
</div>
</div>
</div>
</div>
See this fiddle, please note that you have to increase the size of the output window in order too see the result, otherwise the columns will wrap.
add a comment |
Another approach of offsetting is to have two empty rows, for example:
<div class="col-md-4"></div>
<div class="col-md-4">Centered Content</div>
<div class="col-md-4"></div>
1
2 useless divs, col-md-offset-* is better
– ymakux
Sep 19 '15 at 18:56
1
I agree. I used that method because it actually works well and tested.
– LeRoy
Sep 21 '15 at 7:07
add a comment |
<div class="container-fluid">
<div class="row">
<div class="col-lg-4 col-lg-offset-4">
<img src="some.jpg">
</div>
</div>
</div>
add a comment |
We can achieve this by using the table layout mechanism:
The mechanism is:
- Wrap all columns in one div.
- Make that div as a table with a fixed layout.
- Make each column as a table cell.
- Use vertical-align property to control content position.
The sample demo is here
add a comment |
As koala_dev used in his approach 1, I would prefer the offset method instead of center-block or margins which has limited usage, but as he mentioned:
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.
This can be solved using the following approach for odd columns.
<div class="col-xs-offset-5 col-xs-2">
<div class="col-xs-offset-3">
// Your content here
</div>
</div>
add a comment |
This is not my code, but it works perfectly (tested on Bootstrap 3) and I don't have to mess around with col-offset.
Demo:
/* centered columns styles */
.row-centered {
text-align: center;
}
.col-centered {
display: inline-block;
float: none;
/* reset the text-align */
text-align: left;
/* inline-block space fix */
margin-right: -4px;
text-align: center;
background-color: #ccc;
border: 1px solid #ddd;
}
<div class="container">
<div class="row row-centered">
<div class="col-xs-6 col-centered">Column 6</div>
<div class="col-xs-6 col-centered">Column 6</div>
<div class="col-xs-3 col-centered">Column 3</div>
<div class="col-xs-3 col-centered">Column 3</div>
<div class="col-xs-3 col-centered">Column 3</div>
</div>
</div>
1
that is the solution from codepen.io/makrjean22/pen/xEdKAK
– Serge
Dec 20 '18 at 15:19
add a comment |
You can use the very flexible solution flexbox to your Bootstrap.
justify-content: center;
can center your column.
Check out flex.
add a comment |
Because I never have the need to center only a single .col-
within a .row
, I set the following class on the wrapping .row
of my target columns.
.col-center > [class*="col-"] {
float: none;
margin-left: auto;
margin-right: auto;
}
Example
<div class="full-container">
<div class="row col-center">
<div class="col-xs-11">
Foo
</div>
<div class="col-xs-11">
Bar
</div>
</div>
</div>
add a comment |
For those looking to center the column elements on the screen when you don't have the exact number to fill your grid, I have written a little piece of JavaScript to return the class names:
function colCalculator(totalNumberOfElements, elementsPerRow, screenSize) {
var arrayFill = function (size, content) {
return Array.apply(null, Array(size)).map(String.prototype.valueOf, content);
};
var elementSize = parseInt(12 / elementsPerRow, 10);
var normalClassName = 'col-' + screenSize + '-' + elementSize;
var numberOfFittingElements = parseInt(totalNumberOfElements / elementsPerRow, 10) * elementsPerRow;
var numberOfRemainingElements = totalNumberOfElements - numberOfFittingElements;
var ret = arrayFill(numberOfFittingElements, normalClassName);
var remainingSize = 12 - numberOfRemainingElements * elementSize;
var remainingLeftSize = parseInt(remainingSize / 2, 10);
return ret.concat(arrayFill(numberOfRemainingElements, normalClassName + ' col-' + screenSize + '-push-' + remainingLeftSize));
}
If you have 5 elements and you want to have 3 per row on a md
screen, you do this:
colCalculator(5, 3, 'md')
>> ["col-md-4", "col-md-4", "col-md-4", "col-md-4 col-md-push-2", "col-md-4 col-md-push-2"]
Keep in mind, the second argument must be dividable by 12.
add a comment |
To center more than one column in a Bootstrap row - and the number of cols are odd, simply add this css
class to all the columns in that row:
.many-cols-centered { // To horizontally center bootstrap odd cols, eg col-lg-9, col-md-3, works well in lg
display:inline-block;
float:none;
}
So in your HTML you have something like:
<div class="row text-center"> <!-- text-center centers all text in that row -->
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12 many-cols-centered">
<img src='assets/image1.jpg'>
<h3>You See</h3>
<p>I love coding.</p>
</div>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12 many-cols-centered">
<img src='assets/image2.jpg'>
<h3>You See</h3>
<p>I love coding.</p>
</div>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12 many-cols-centered">
<img src='assets/image3.jpg'>
<h3>You See</h3>
<p>I love coding.</p>
</div>
</div>
add a comment |
Try this code.
<body class="container">
<div class="col-lg-1 col-lg-offset-10">
<img data-src="holder.js/100x100" alt="" />
</div>
Here i have used col-lg-1, and the offset should be 10 for properly centered the div on large devices, if you need it to center on mediam to large device then just change the lg to md and so on, let me know if any issue.
add a comment |
If you put this on your row, all of the columns inside will be centered:
.row-centered {
display: flex;
justify-content: space-between;
}
made the element non responsive.
– gfivehost
Feb 17 '17 at 14:22
add a comment |
Try this
<div class="row">
<div class="col-xs-2 col-xs-offset-5"></div>
</div>
You can use other col
as well like col-md-2
, etc.
add a comment |
I suggest simply to use the class text-center
:
<body class="container">
<div class="col-md-12 text-center">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
add a comment |
To be more precise Bootstrap's grid system contains 12 columns and to center any content let’s say, for instance, the content takes up one column. One will need to occupy two columns of Bootstrap's grid and place the content on half of the two occupied columns.
<div class="row">
<div class="col-xs-2 col-xs-offset-5 centered">
... your content / data will come here ...
</div>
</div>
'col-xs-offset-5' is telling the grid system where to start placing the content.
'col-xs-2' is telling the grid system how many of the left over columns should the content occupy.
'centered' will be a defined class that will center the content.
Here is how this example looks like in Bootstrap's grid system.
Columns:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12
.......offset....... .data. .......not used........
add a comment |
Append this snippet inside your .row or your .col, this is for bootstrap 4*
d-flex justify-content-center
add a comment |
Don't forget to add !important
. Then you can be sure that element really will be in the center:
.col-centered{
float: none !important;
margin: 0 auto !important;
}
add a comment |
Method 1:
<div class="container">
<div class="row">
<div class="col-md-2 col-md-offset-5">
YOUR CONTENT
</div>
</div>
</div>
Method 2:
CSS
.float-center{float: none;}
<div class="container">
<div id="mydev" class="center-block float-center" style="width:75%;">
YOUR CONTENT
</div>
</div>
Bootstrap Tips, examples and tools: OnAirCode
add a comment |
1 2
next
protected by Josh Crozier Sep 5 '14 at 14:05
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
31 Answers
31
active
oldest
votes
31 Answers
31
active
oldest
votes
active
oldest
votes
active
oldest
votes
1 2
next
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.
6
@DiegoFernandoMurilloValenci.clearfix
wouldn't work in this case because it doesn't remove the float property on the elements (which is needed to center usingmargin: 0 auto
), it just makes a container wrap any floated elements inside
– koala_dev
Jul 8 '14 at 23:10
4
can't use this on a column, because nofloat:none;
– airtonix
Feb 28 '15 at 3:50
10
Responsive Centered Bootstrap Columns minimit.com/articles/solutions-tutorials/…
– Gothburz
Oct 24 '15 at 23:44
2
If you want to center more then one column in a single line, see this answer stackoverflow.com/questions/28683329/…
– Conor Svensson
May 17 '16 at 11:19
3
The syntax for offset changed in Bootstrap4 (as of latest version). Using this response as an example: the class col-md-offset-5 should now be offset-md-5.
– lgants
Sep 10 '17 at 23:46
|
show 17 more comments
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.
6
@DiegoFernandoMurilloValenci.clearfix
wouldn't work in this case because it doesn't remove the float property on the elements (which is needed to center usingmargin: 0 auto
), it just makes a container wrap any floated elements inside
– koala_dev
Jul 8 '14 at 23:10
4
can't use this on a column, because nofloat:none;
– airtonix
Feb 28 '15 at 3:50
10
Responsive Centered Bootstrap Columns minimit.com/articles/solutions-tutorials/…
– Gothburz
Oct 24 '15 at 23:44
2
If you want to center more then one column in a single line, see this answer stackoverflow.com/questions/28683329/…
– Conor Svensson
May 17 '16 at 11:19
3
The syntax for offset changed in Bootstrap4 (as of latest version). Using this response as an example: the class col-md-offset-5 should now be offset-md-5.
– lgants
Sep 10 '17 at 23:46
|
show 17 more comments
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.
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.
edited Mar 7 '16 at 2:58
TylerH
15.6k105267
15.6k105267
answered Aug 9 '13 at 18:45
koala_devkoala_dev
46.1k64964
46.1k64964
6
@DiegoFernandoMurilloValenci.clearfix
wouldn't work in this case because it doesn't remove the float property on the elements (which is needed to center usingmargin: 0 auto
), it just makes a container wrap any floated elements inside
– koala_dev
Jul 8 '14 at 23:10
4
can't use this on a column, because nofloat:none;
– airtonix
Feb 28 '15 at 3:50
10
Responsive Centered Bootstrap Columns minimit.com/articles/solutions-tutorials/…
– Gothburz
Oct 24 '15 at 23:44
2
If you want to center more then one column in a single line, see this answer stackoverflow.com/questions/28683329/…
– Conor Svensson
May 17 '16 at 11:19
3
The syntax for offset changed in Bootstrap4 (as of latest version). Using this response as an example: the class col-md-offset-5 should now be offset-md-5.
– lgants
Sep 10 '17 at 23:46
|
show 17 more comments
6
@DiegoFernandoMurilloValenci.clearfix
wouldn't work in this case because it doesn't remove the float property on the elements (which is needed to center usingmargin: 0 auto
), it just makes a container wrap any floated elements inside
– koala_dev
Jul 8 '14 at 23:10
4
can't use this on a column, because nofloat:none;
– airtonix
Feb 28 '15 at 3:50
10
Responsive Centered Bootstrap Columns minimit.com/articles/solutions-tutorials/…
– Gothburz
Oct 24 '15 at 23:44
2
If you want to center more then one column in a single line, see this answer stackoverflow.com/questions/28683329/…
– Conor Svensson
May 17 '16 at 11:19
3
The syntax for offset changed in Bootstrap4 (as of latest version). Using this response as an example: the class col-md-offset-5 should now be offset-md-5.
– lgants
Sep 10 '17 at 23:46
6
6
@DiegoFernandoMurilloValenci
.clearfix
wouldn't work in this case because it doesn't remove the float property on the elements (which is needed to center using margin: 0 auto
), it just makes a container wrap any floated elements inside– koala_dev
Jul 8 '14 at 23:10
@DiegoFernandoMurilloValenci
.clearfix
wouldn't work in this case because it doesn't remove the float property on the elements (which is needed to center using margin: 0 auto
), it just makes a container wrap any floated elements inside– koala_dev
Jul 8 '14 at 23:10
4
4
can't use this on a column, because no
float:none;
– airtonix
Feb 28 '15 at 3:50
can't use this on a column, because no
float:none;
– airtonix
Feb 28 '15 at 3:50
10
10
Responsive Centered Bootstrap Columns minimit.com/articles/solutions-tutorials/…
– Gothburz
Oct 24 '15 at 23:44
Responsive Centered Bootstrap Columns minimit.com/articles/solutions-tutorials/…
– Gothburz
Oct 24 '15 at 23:44
2
2
If you want to center more then one column in a single line, see this answer stackoverflow.com/questions/28683329/…
– Conor Svensson
May 17 '16 at 11:19
If you want to center more then one column in a single line, see this answer stackoverflow.com/questions/28683329/…
– Conor Svensson
May 17 '16 at 11:19
3
3
The syntax for offset changed in Bootstrap4 (as of latest version). Using this response as an example: the class col-md-offset-5 should now be offset-md-5.
– lgants
Sep 10 '17 at 23:46
The syntax for offset changed in Bootstrap4 (as of latest version). Using this response as an example: the class col-md-offset-5 should now be offset-md-5.
– lgants
Sep 10 '17 at 23:46
|
show 17 more comments
The preferred method of centering columns is to use "offsets" (ie: col-md-offset-3
)
Bootstrap 3.x centering examples
For centering elements, there is a center-block
helper class.
You can also use text-center
to center text (and inline elements).
Demo: http://bootply.com/91632
EDIT - As mentioned in the comments, center-block
works on column contents and display:block
elements, but won't work on the column itself (col-*
divs) because Bootstrap uses float
.
Update 2018
Now with Bootstrap 4, the centering methods have changed..
text-center
is still used fordisplay:inline
elements
mx-auto
replacescenter-block
to centerdisplay:block
elements
offset-*
ormx-auto
can be used to center grid columns
mx-auto
(auto x-axis margins) will center display:block
or display:flex
elements that have a defined width, (%
, vw
, px
, etc..). Flexbox is used by default on grid columns, so there are also various flexbox centering methods.
Demo Bootstrap 4 Horizontal Centering
For vertical centering in BS4 see https://stackoverflow.com/a/41464397/171456
29
Unfortunately this helper class doesn't work with the column system as it doesn't take care of the float property. See this demo for example.
– koala_dev
Dec 5 '13 at 18:03
13
That only requires a center-block { float: none; }, and it works. bootply.com/122268
– Matias Vad
Mar 16 '14 at 11:33
5
not sure about the OP, but I got here looking for thetext-center
detail - which none of the other answers provided. This one gets my vote.
– domoarigato
Feb 2 '15 at 19:41
1
can't use this on a column, because nofloat:none;
– airtonix
Feb 28 '15 at 3:50
1
I edited the answer regarding offsets.
– Zim
Feb 10 '16 at 13:41
|
show 1 more comment
The preferred method of centering columns is to use "offsets" (ie: col-md-offset-3
)
Bootstrap 3.x centering examples
For centering elements, there is a center-block
helper class.
You can also use text-center
to center text (and inline elements).
Demo: http://bootply.com/91632
EDIT - As mentioned in the comments, center-block
works on column contents and display:block
elements, but won't work on the column itself (col-*
divs) because Bootstrap uses float
.
Update 2018
Now with Bootstrap 4, the centering methods have changed..
text-center
is still used fordisplay:inline
elements
mx-auto
replacescenter-block
to centerdisplay:block
elements
offset-*
ormx-auto
can be used to center grid columns
mx-auto
(auto x-axis margins) will center display:block
or display:flex
elements that have a defined width, (%
, vw
, px
, etc..). Flexbox is used by default on grid columns, so there are also various flexbox centering methods.
Demo Bootstrap 4 Horizontal Centering
For vertical centering in BS4 see https://stackoverflow.com/a/41464397/171456
29
Unfortunately this helper class doesn't work with the column system as it doesn't take care of the float property. See this demo for example.
– koala_dev
Dec 5 '13 at 18:03
13
That only requires a center-block { float: none; }, and it works. bootply.com/122268
– Matias Vad
Mar 16 '14 at 11:33
5
not sure about the OP, but I got here looking for thetext-center
detail - which none of the other answers provided. This one gets my vote.
– domoarigato
Feb 2 '15 at 19:41
1
can't use this on a column, because nofloat:none;
– airtonix
Feb 28 '15 at 3:50
1
I edited the answer regarding offsets.
– Zim
Feb 10 '16 at 13:41
|
show 1 more comment
The preferred method of centering columns is to use "offsets" (ie: col-md-offset-3
)
Bootstrap 3.x centering examples
For centering elements, there is a center-block
helper class.
You can also use text-center
to center text (and inline elements).
Demo: http://bootply.com/91632
EDIT - As mentioned in the comments, center-block
works on column contents and display:block
elements, but won't work on the column itself (col-*
divs) because Bootstrap uses float
.
Update 2018
Now with Bootstrap 4, the centering methods have changed..
text-center
is still used fordisplay:inline
elements
mx-auto
replacescenter-block
to centerdisplay:block
elements
offset-*
ormx-auto
can be used to center grid columns
mx-auto
(auto x-axis margins) will center display:block
or display:flex
elements that have a defined width, (%
, vw
, px
, etc..). Flexbox is used by default on grid columns, so there are also various flexbox centering methods.
Demo Bootstrap 4 Horizontal Centering
For vertical centering in BS4 see https://stackoverflow.com/a/41464397/171456
The preferred method of centering columns is to use "offsets" (ie: col-md-offset-3
)
Bootstrap 3.x centering examples
For centering elements, there is a center-block
helper class.
You can also use text-center
to center text (and inline elements).
Demo: http://bootply.com/91632
EDIT - As mentioned in the comments, center-block
works on column contents and display:block
elements, but won't work on the column itself (col-*
divs) because Bootstrap uses float
.
Update 2018
Now with Bootstrap 4, the centering methods have changed..
text-center
is still used fordisplay:inline
elements
mx-auto
replacescenter-block
to centerdisplay:block
elements
offset-*
ormx-auto
can be used to center grid columns
mx-auto
(auto x-axis margins) will center display:block
or display:flex
elements that have a defined width, (%
, vw
, px
, etc..). Flexbox is used by default on grid columns, so there are also various flexbox centering methods.
Demo Bootstrap 4 Horizontal Centering
For vertical centering in BS4 see https://stackoverflow.com/a/41464397/171456
edited Feb 22 '18 at 20:44
answered Dec 3 '13 at 18:12
ZimZim
188k48396379
188k48396379
29
Unfortunately this helper class doesn't work with the column system as it doesn't take care of the float property. See this demo for example.
– koala_dev
Dec 5 '13 at 18:03
13
That only requires a center-block { float: none; }, and it works. bootply.com/122268
– Matias Vad
Mar 16 '14 at 11:33
5
not sure about the OP, but I got here looking for thetext-center
detail - which none of the other answers provided. This one gets my vote.
– domoarigato
Feb 2 '15 at 19:41
1
can't use this on a column, because nofloat:none;
– airtonix
Feb 28 '15 at 3:50
1
I edited the answer regarding offsets.
– Zim
Feb 10 '16 at 13:41
|
show 1 more comment
29
Unfortunately this helper class doesn't work with the column system as it doesn't take care of the float property. See this demo for example.
– koala_dev
Dec 5 '13 at 18:03
13
That only requires a center-block { float: none; }, and it works. bootply.com/122268
– Matias Vad
Mar 16 '14 at 11:33
5
not sure about the OP, but I got here looking for thetext-center
detail - which none of the other answers provided. This one gets my vote.
– domoarigato
Feb 2 '15 at 19:41
1
can't use this on a column, because nofloat:none;
– airtonix
Feb 28 '15 at 3:50
1
I edited the answer regarding offsets.
– Zim
Feb 10 '16 at 13:41
29
29
Unfortunately this helper class doesn't work with the column system as it doesn't take care of the float property. See this demo for example.
– koala_dev
Dec 5 '13 at 18:03
Unfortunately this helper class doesn't work with the column system as it doesn't take care of the float property. See this demo for example.
– koala_dev
Dec 5 '13 at 18:03
13
13
That only requires a center-block { float: none; }, and it works. bootply.com/122268
– Matias Vad
Mar 16 '14 at 11:33
That only requires a center-block { float: none; }, and it works. bootply.com/122268
– Matias Vad
Mar 16 '14 at 11:33
5
5
not sure about the OP, but I got here looking for the
text-center
detail - which none of the other answers provided. This one gets my vote.– domoarigato
Feb 2 '15 at 19:41
not sure about the OP, but I got here looking for the
text-center
detail - which none of the other answers provided. This one gets my vote.– domoarigato
Feb 2 '15 at 19:41
1
1
can't use this on a column, because no
float:none;
– airtonix
Feb 28 '15 at 3:50
can't use this on a column, because no
float:none;
– airtonix
Feb 28 '15 at 3:50
1
1
I edited the answer regarding offsets.
– Zim
Feb 10 '16 at 13:41
I edited the answer regarding offsets.
– Zim
Feb 10 '16 at 13:41
|
show 1 more comment
Now Bootstrap 3.1.1 is working with .center-block
, and this helper class works with the column system.
Bootstrap 3 Helper Class Center.
Please check this jsfiddle DEMO:
<div class="container">
<div class="row">
<div class="center-block">row center-block</div>
</div>
<div class="row">
<div class="col-md-6 brd">
<div class="center-block">1 center-block</div>
</div>
<div class="col-md-6 brd">
<div class="center-block">2 center-block</div>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-2 col-center-block">row col-xs-2 col-center-block</div>
</div>
Row column center using col-center-block
helper class.
.col-center-block {
float: none;
display: block;
margin: 0 auto;
/* margin-left: auto; margin-right: auto; */
}
I got this working without addingfloat:none
by making sure the div centre-block has thestyle="width : ?px"
style applied to it. For example an image width. Works with 3.2.2
– ppumkin
Mar 4 '15 at 21:31
margin: 0 auto; - enough instead margin-left + margin-right
– mmike
Aug 29 '16 at 10:06
add a comment |
Now Bootstrap 3.1.1 is working with .center-block
, and this helper class works with the column system.
Bootstrap 3 Helper Class Center.
Please check this jsfiddle DEMO:
<div class="container">
<div class="row">
<div class="center-block">row center-block</div>
</div>
<div class="row">
<div class="col-md-6 brd">
<div class="center-block">1 center-block</div>
</div>
<div class="col-md-6 brd">
<div class="center-block">2 center-block</div>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-2 col-center-block">row col-xs-2 col-center-block</div>
</div>
Row column center using col-center-block
helper class.
.col-center-block {
float: none;
display: block;
margin: 0 auto;
/* margin-left: auto; margin-right: auto; */
}
I got this working without addingfloat:none
by making sure the div centre-block has thestyle="width : ?px"
style applied to it. For example an image width. Works with 3.2.2
– ppumkin
Mar 4 '15 at 21:31
margin: 0 auto; - enough instead margin-left + margin-right
– mmike
Aug 29 '16 at 10:06
add a comment |
Now Bootstrap 3.1.1 is working with .center-block
, and this helper class works with the column system.
Bootstrap 3 Helper Class Center.
Please check this jsfiddle DEMO:
<div class="container">
<div class="row">
<div class="center-block">row center-block</div>
</div>
<div class="row">
<div class="col-md-6 brd">
<div class="center-block">1 center-block</div>
</div>
<div class="col-md-6 brd">
<div class="center-block">2 center-block</div>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-2 col-center-block">row col-xs-2 col-center-block</div>
</div>
Row column center using col-center-block
helper class.
.col-center-block {
float: none;
display: block;
margin: 0 auto;
/* margin-left: auto; margin-right: auto; */
}
Now Bootstrap 3.1.1 is working with .center-block
, and this helper class works with the column system.
Bootstrap 3 Helper Class Center.
Please check this jsfiddle DEMO:
<div class="container">
<div class="row">
<div class="center-block">row center-block</div>
</div>
<div class="row">
<div class="col-md-6 brd">
<div class="center-block">1 center-block</div>
</div>
<div class="col-md-6 brd">
<div class="center-block">2 center-block</div>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-2 col-center-block">row col-xs-2 col-center-block</div>
</div>
Row column center using col-center-block
helper class.
.col-center-block {
float: none;
display: block;
margin: 0 auto;
/* margin-left: auto; margin-right: auto; */
}
edited Aug 29 '16 at 11:25
answered Mar 18 '14 at 6:31
SenderSender
4,32893649
4,32893649
I got this working without addingfloat:none
by making sure the div centre-block has thestyle="width : ?px"
style applied to it. For example an image width. Works with 3.2.2
– ppumkin
Mar 4 '15 at 21:31
margin: 0 auto; - enough instead margin-left + margin-right
– mmike
Aug 29 '16 at 10:06
add a comment |
I got this working without addingfloat:none
by making sure the div centre-block has thestyle="width : ?px"
style applied to it. For example an image width. Works with 3.2.2
– ppumkin
Mar 4 '15 at 21:31
margin: 0 auto; - enough instead margin-left + margin-right
– mmike
Aug 29 '16 at 10:06
I got this working without adding
float:none
by making sure the div centre-block has the style="width : ?px"
style applied to it. For example an image width. Works with 3.2.2– ppumkin
Mar 4 '15 at 21:31
I got this working without adding
float:none
by making sure the div centre-block has the style="width : ?px"
style applied to it. For example an image width. Works with 3.2.2– ppumkin
Mar 4 '15 at 21:31
margin: 0 auto; - enough instead margin-left + margin-right
– mmike
Aug 29 '16 at 10:06
margin: 0 auto; - enough instead margin-left + margin-right
– mmike
Aug 29 '16 at 10:06
add a comment |
Simply add this to your custom CSS file, editing Bootstrap CSS files directly is not recommended and cancells your ability to use a cdn.
.center-block {
float: none !important
}
Why?
Bootstrap CSS (Ver 3.7 and lower) uses: margin: 0 auto;, but it gets overridden by the float property of the size container.
PS:
After you add this class don't forget to set classes by the right order.
<div class="col-md-6 center-block">Example</div>
2
Only made .center-block to work by doing like this answer.
– João Martins
Mar 26 '15 at 14:06
1
I'm using Bootstrap3.3.7
and this is the only method working for me!
– illagrenan
Sep 6 '16 at 14:52
1
this works. but don't think!important
is necessary
– Gianfranco P.
Feb 23 '17 at 13:51
@GianfrancoP. P. Your right, but... that depends on the load order and i wanted to provide a foolproof method.
– Sagive SEO
Sep 13 '17 at 21:37
add a comment |
Simply add this to your custom CSS file, editing Bootstrap CSS files directly is not recommended and cancells your ability to use a cdn.
.center-block {
float: none !important
}
Why?
Bootstrap CSS (Ver 3.7 and lower) uses: margin: 0 auto;, but it gets overridden by the float property of the size container.
PS:
After you add this class don't forget to set classes by the right order.
<div class="col-md-6 center-block">Example</div>
2
Only made .center-block to work by doing like this answer.
– João Martins
Mar 26 '15 at 14:06
1
I'm using Bootstrap3.3.7
and this is the only method working for me!
– illagrenan
Sep 6 '16 at 14:52
1
this works. but don't think!important
is necessary
– Gianfranco P.
Feb 23 '17 at 13:51
@GianfrancoP. P. Your right, but... that depends on the load order and i wanted to provide a foolproof method.
– Sagive SEO
Sep 13 '17 at 21:37
add a comment |
Simply add this to your custom CSS file, editing Bootstrap CSS files directly is not recommended and cancells your ability to use a cdn.
.center-block {
float: none !important
}
Why?
Bootstrap CSS (Ver 3.7 and lower) uses: margin: 0 auto;, but it gets overridden by the float property of the size container.
PS:
After you add this class don't forget to set classes by the right order.
<div class="col-md-6 center-block">Example</div>
Simply add this to your custom CSS file, editing Bootstrap CSS files directly is not recommended and cancells your ability to use a cdn.
.center-block {
float: none !important
}
Why?
Bootstrap CSS (Ver 3.7 and lower) uses: margin: 0 auto;, but it gets overridden by the float property of the size container.
PS:
After you add this class don't forget to set classes by the right order.
<div class="col-md-6 center-block">Example</div>
edited Sep 13 '17 at 21:34
answered Dec 15 '13 at 3:12
Sagive SEOSagive SEO
1,1001523
1,1001523
2
Only made .center-block to work by doing like this answer.
– João Martins
Mar 26 '15 at 14:06
1
I'm using Bootstrap3.3.7
and this is the only method working for me!
– illagrenan
Sep 6 '16 at 14:52
1
this works. but don't think!important
is necessary
– Gianfranco P.
Feb 23 '17 at 13:51
@GianfrancoP. P. Your right, but... that depends on the load order and i wanted to provide a foolproof method.
– Sagive SEO
Sep 13 '17 at 21:37
add a comment |
2
Only made .center-block to work by doing like this answer.
– João Martins
Mar 26 '15 at 14:06
1
I'm using Bootstrap3.3.7
and this is the only method working for me!
– illagrenan
Sep 6 '16 at 14:52
1
this works. but don't think!important
is necessary
– Gianfranco P.
Feb 23 '17 at 13:51
@GianfrancoP. P. Your right, but... that depends on the load order and i wanted to provide a foolproof method.
– Sagive SEO
Sep 13 '17 at 21:37
2
2
Only made .center-block to work by doing like this answer.
– João Martins
Mar 26 '15 at 14:06
Only made .center-block to work by doing like this answer.
– João Martins
Mar 26 '15 at 14:06
1
1
I'm using Bootstrap
3.3.7
and this is the only method working for me!– illagrenan
Sep 6 '16 at 14:52
I'm using Bootstrap
3.3.7
and this is the only method working for me!– illagrenan
Sep 6 '16 at 14:52
1
1
this works. but don't think
!important
is necessary– Gianfranco P.
Feb 23 '17 at 13:51
this works. but don't think
!important
is necessary– Gianfranco P.
Feb 23 '17 at 13:51
@GianfrancoP. P. Your right, but... that depends on the load order and i wanted to provide a foolproof method.
– Sagive SEO
Sep 13 '17 at 21:37
@GianfrancoP. P. Your right, but... that depends on the load order and i wanted to provide a foolproof method.
– Sagive SEO
Sep 13 '17 at 21:37
add a comment |
Bootstrap 3 now has a built-in class for this .center-block
.center-block {
display: block;
margin-left: auto;
margin-right: auto;
}
If you are still using 2.X then just add this to your CSS.
this would probably give you the desired effect the easiest. Also throw width on the .centered class or add a second class if you are going for a more modular approach
– Matt Lambert
Aug 9 '13 at 21:46
2
Adding float: none; to .centered lets you move the class up into container. keeps the code cleaner
– Adam Patterson
Jan 29 '14 at 3:50
7
can't use this on a column, because nofloat:none;
– airtonix
Feb 28 '15 at 3:49
add a comment |
Bootstrap 3 now has a built-in class for this .center-block
.center-block {
display: block;
margin-left: auto;
margin-right: auto;
}
If you are still using 2.X then just add this to your CSS.
this would probably give you the desired effect the easiest. Also throw width on the .centered class or add a second class if you are going for a more modular approach
– Matt Lambert
Aug 9 '13 at 21:46
2
Adding float: none; to .centered lets you move the class up into container. keeps the code cleaner
– Adam Patterson
Jan 29 '14 at 3:50
7
can't use this on a column, because nofloat:none;
– airtonix
Feb 28 '15 at 3:49
add a comment |
Bootstrap 3 now has a built-in class for this .center-block
.center-block {
display: block;
margin-left: auto;
margin-right: auto;
}
If you are still using 2.X then just add this to your CSS.
Bootstrap 3 now has a built-in class for this .center-block
.center-block {
display: block;
margin-left: auto;
margin-right: auto;
}
If you are still using 2.X then just add this to your CSS.
edited Mar 24 '14 at 20:24
answered Aug 9 '13 at 18:39
SchmalzySchmalzy
13.8k63545
13.8k63545
this would probably give you the desired effect the easiest. Also throw width on the .centered class or add a second class if you are going for a more modular approach
– Matt Lambert
Aug 9 '13 at 21:46
2
Adding float: none; to .centered lets you move the class up into container. keeps the code cleaner
– Adam Patterson
Jan 29 '14 at 3:50
7
can't use this on a column, because nofloat:none;
– airtonix
Feb 28 '15 at 3:49
add a comment |
this would probably give you the desired effect the easiest. Also throw width on the .centered class or add a second class if you are going for a more modular approach
– Matt Lambert
Aug 9 '13 at 21:46
2
Adding float: none; to .centered lets you move the class up into container. keeps the code cleaner
– Adam Patterson
Jan 29 '14 at 3:50
7
can't use this on a column, because nofloat:none;
– airtonix
Feb 28 '15 at 3:49
this would probably give you the desired effect the easiest. Also throw width on the .centered class or add a second class if you are going for a more modular approach
– Matt Lambert
Aug 9 '13 at 21:46
this would probably give you the desired effect the easiest. Also throw width on the .centered class or add a second class if you are going for a more modular approach
– Matt Lambert
Aug 9 '13 at 21:46
2
2
Adding float: none; to .centered lets you move the class up into container. keeps the code cleaner
– Adam Patterson
Jan 29 '14 at 3:50
Adding float: none; to .centered lets you move the class up into container. keeps the code cleaner
– Adam Patterson
Jan 29 '14 at 3:50
7
7
can't use this on a column, because no
float:none;
– airtonix
Feb 28 '15 at 3:49
can't use this on a column, because no
float:none;
– airtonix
Feb 28 '15 at 3:49
add a comment |
My approach to center columns is to use display: inline-block
for columns and text-align: center
for the container parent.
You just have to add the CSS class 'centered' to the row
.
HTML:
<div class="container-fluid">
<div class="row centered">
<div class="col-sm-4 col-md-4">
Col 1
</div>
<div class="col-sm-4 col-md-4">
Col 2
</div>
<div class="col-sm-4 col-md-4">
Col 3
</div>
</div>
</div>
CSS:
.centered {
text-align: center;
font-size: 0;
}
.centered > div {
float: none;
display: inline-block;
text-align: left;
font-size: 13px;
}
JSfiddle:
http://jsfiddle.net/steyffi/ug4fzcjd/
2
This is the only answer which worked 100% of the time - The others worked, but not when bootstrap switched to its mobile view (the items were moved to the left side).
– Levi Fuller
Jul 6 '16 at 16:10
This was the only one which worked me as well, .center-block only works for one column and I need to center all columns independent of how many it is.
– cjohansson
Dec 8 '16 at 12:13
Only solution that worked for me too. Other ones worked only when the browser was a window, (not fullscreen).
– kiwicomb123
Jun 29 '17 at 3:30
add a comment |
My approach to center columns is to use display: inline-block
for columns and text-align: center
for the container parent.
You just have to add the CSS class 'centered' to the row
.
HTML:
<div class="container-fluid">
<div class="row centered">
<div class="col-sm-4 col-md-4">
Col 1
</div>
<div class="col-sm-4 col-md-4">
Col 2
</div>
<div class="col-sm-4 col-md-4">
Col 3
</div>
</div>
</div>
CSS:
.centered {
text-align: center;
font-size: 0;
}
.centered > div {
float: none;
display: inline-block;
text-align: left;
font-size: 13px;
}
JSfiddle:
http://jsfiddle.net/steyffi/ug4fzcjd/
2
This is the only answer which worked 100% of the time - The others worked, but not when bootstrap switched to its mobile view (the items were moved to the left side).
– Levi Fuller
Jul 6 '16 at 16:10
This was the only one which worked me as well, .center-block only works for one column and I need to center all columns independent of how many it is.
– cjohansson
Dec 8 '16 at 12:13
Only solution that worked for me too. Other ones worked only when the browser was a window, (not fullscreen).
– kiwicomb123
Jun 29 '17 at 3:30
add a comment |
My approach to center columns is to use display: inline-block
for columns and text-align: center
for the container parent.
You just have to add the CSS class 'centered' to the row
.
HTML:
<div class="container-fluid">
<div class="row centered">
<div class="col-sm-4 col-md-4">
Col 1
</div>
<div class="col-sm-4 col-md-4">
Col 2
</div>
<div class="col-sm-4 col-md-4">
Col 3
</div>
</div>
</div>
CSS:
.centered {
text-align: center;
font-size: 0;
}
.centered > div {
float: none;
display: inline-block;
text-align: left;
font-size: 13px;
}
JSfiddle:
http://jsfiddle.net/steyffi/ug4fzcjd/
My approach to center columns is to use display: inline-block
for columns and text-align: center
for the container parent.
You just have to add the CSS class 'centered' to the row
.
HTML:
<div class="container-fluid">
<div class="row centered">
<div class="col-sm-4 col-md-4">
Col 1
</div>
<div class="col-sm-4 col-md-4">
Col 2
</div>
<div class="col-sm-4 col-md-4">
Col 3
</div>
</div>
</div>
CSS:
.centered {
text-align: center;
font-size: 0;
}
.centered > div {
float: none;
display: inline-block;
text-align: left;
font-size: 13px;
}
JSfiddle:
http://jsfiddle.net/steyffi/ug4fzcjd/
edited Jun 30 '16 at 16:07
Peter Mortensen
13.6k1984111
13.6k1984111
answered Feb 16 '15 at 10:27
SteffiSteffi
3,086155994
3,086155994
2
This is the only answer which worked 100% of the time - The others worked, but not when bootstrap switched to its mobile view (the items were moved to the left side).
– Levi Fuller
Jul 6 '16 at 16:10
This was the only one which worked me as well, .center-block only works for one column and I need to center all columns independent of how many it is.
– cjohansson
Dec 8 '16 at 12:13
Only solution that worked for me too. Other ones worked only when the browser was a window, (not fullscreen).
– kiwicomb123
Jun 29 '17 at 3:30
add a comment |
2
This is the only answer which worked 100% of the time - The others worked, but not when bootstrap switched to its mobile view (the items were moved to the left side).
– Levi Fuller
Jul 6 '16 at 16:10
This was the only one which worked me as well, .center-block only works for one column and I need to center all columns independent of how many it is.
– cjohansson
Dec 8 '16 at 12:13
Only solution that worked for me too. Other ones worked only when the browser was a window, (not fullscreen).
– kiwicomb123
Jun 29 '17 at 3:30
2
2
This is the only answer which worked 100% of the time - The others worked, but not when bootstrap switched to its mobile view (the items were moved to the left side).
– Levi Fuller
Jul 6 '16 at 16:10
This is the only answer which worked 100% of the time - The others worked, but not when bootstrap switched to its mobile view (the items were moved to the left side).
– Levi Fuller
Jul 6 '16 at 16:10
This was the only one which worked me as well, .center-block only works for one column and I need to center all columns independent of how many it is.
– cjohansson
Dec 8 '16 at 12:13
This was the only one which worked me as well, .center-block only works for one column and I need to center all columns independent of how many it is.
– cjohansson
Dec 8 '16 at 12:13
Only solution that worked for me too. Other ones worked only when the browser was a window, (not fullscreen).
– kiwicomb123
Jun 29 '17 at 3:30
Only solution that worked for me too. Other ones worked only when the browser was a window, (not fullscreen).
– kiwicomb123
Jun 29 '17 at 3:30
add a comment |
Bootstrap version 3 has a .text-center class.
Just add this class:
text-center
It will simply load this style:
.text-center {
text-align: center;
}
Example:
<div class="container-fluid">
<div class="row text-center">
<div class="col-md-12">
Bootstrap 4 is coming....
</div>
</div>
</div>
Clarify where this class will be added?
– aksu
Sep 20 '16 at 16:34
Hi @aksu, you don't have to add any .css. Look at the example provided.
– JoshYates1980
Oct 28 '16 at 15:42
add a comment |
Bootstrap version 3 has a .text-center class.
Just add this class:
text-center
It will simply load this style:
.text-center {
text-align: center;
}
Example:
<div class="container-fluid">
<div class="row text-center">
<div class="col-md-12">
Bootstrap 4 is coming....
</div>
</div>
</div>
Clarify where this class will be added?
– aksu
Sep 20 '16 at 16:34
Hi @aksu, you don't have to add any .css. Look at the example provided.
– JoshYates1980
Oct 28 '16 at 15:42
add a comment |
Bootstrap version 3 has a .text-center class.
Just add this class:
text-center
It will simply load this style:
.text-center {
text-align: center;
}
Example:
<div class="container-fluid">
<div class="row text-center">
<div class="col-md-12">
Bootstrap 4 is coming....
</div>
</div>
</div>
Bootstrap version 3 has a .text-center class.
Just add this class:
text-center
It will simply load this style:
.text-center {
text-align: center;
}
Example:
<div class="container-fluid">
<div class="row text-center">
<div class="col-md-12">
Bootstrap 4 is coming....
</div>
</div>
</div>
edited Oct 28 '16 at 16:55
JoshYates1980
2,15522647
2,15522647
answered Jul 16 '15 at 14:20
RustyInglesRustyIngles
1,91111721
1,91111721
Clarify where this class will be added?
– aksu
Sep 20 '16 at 16:34
Hi @aksu, you don't have to add any .css. Look at the example provided.
– JoshYates1980
Oct 28 '16 at 15:42
add a comment |
Clarify where this class will be added?
– aksu
Sep 20 '16 at 16:34
Hi @aksu, you don't have to add any .css. Look at the example provided.
– JoshYates1980
Oct 28 '16 at 15:42
Clarify where this class will be added?
– aksu
Sep 20 '16 at 16:34
Clarify where this class will be added?
– aksu
Sep 20 '16 at 16:34
Hi @aksu, you don't have to add any .css. Look at the example provided.
– JoshYates1980
Oct 28 '16 at 15:42
Hi @aksu, you don't have to add any .css. Look at the example provided.
– JoshYates1980
Oct 28 '16 at 15:42
add a comment |
With Bootstrap v4, this can be accomplished just by adding .justify-content-center
to the .row
<div>
<div class="row justify-content-center">
<div class="col-1">centered 1 column</div>
</div>
https://getbootstrap.com/docs/4.0/utilities/flex/#justify-content
add a comment |
With Bootstrap v4, this can be accomplished just by adding .justify-content-center
to the .row
<div>
<div class="row justify-content-center">
<div class="col-1">centered 1 column</div>
</div>
https://getbootstrap.com/docs/4.0/utilities/flex/#justify-content
add a comment |
With Bootstrap v4, this can be accomplished just by adding .justify-content-center
to the .row
<div>
<div class="row justify-content-center">
<div class="col-1">centered 1 column</div>
</div>
https://getbootstrap.com/docs/4.0/utilities/flex/#justify-content
With Bootstrap v4, this can be accomplished just by adding .justify-content-center
to the .row
<div>
<div class="row justify-content-center">
<div class="col-1">centered 1 column</div>
</div>
https://getbootstrap.com/docs/4.0/utilities/flex/#justify-content
answered Jun 25 '18 at 2:57
joknawejoknawe
897513
897513
add a comment |
add a comment |
This works. A hackish way probably, but it works nicely. It was tested for responsive (Y).
.centered {
background-color: teal;
text-align: center;
}
works only because the element has diplay inline. Animg
may be set as a block.
– ProfileTwist
Aug 12 '13 at 18:34
2
But it works and I do not see a reason to down vote.
– Ali Gajani
Aug 12 '13 at 18:34
Perhaps in your isolated testing case. I would add.centered img { display: inline; }
to your answer and then it would be worthy of an up vote instead.
– ProfileTwist
Aug 13 '13 at 15:02
No problem. I am sure the OP will find his/her answer :)
– Ali Gajani
Aug 13 '13 at 15:11
in bootstrap 3, the .text-center class does the same thing natively, as I learned above.
– domoarigato
Feb 2 '15 at 19:43
add a comment |
This works. A hackish way probably, but it works nicely. It was tested for responsive (Y).
.centered {
background-color: teal;
text-align: center;
}
works only because the element has diplay inline. Animg
may be set as a block.
– ProfileTwist
Aug 12 '13 at 18:34
2
But it works and I do not see a reason to down vote.
– Ali Gajani
Aug 12 '13 at 18:34
Perhaps in your isolated testing case. I would add.centered img { display: inline; }
to your answer and then it would be worthy of an up vote instead.
– ProfileTwist
Aug 13 '13 at 15:02
No problem. I am sure the OP will find his/her answer :)
– Ali Gajani
Aug 13 '13 at 15:11
in bootstrap 3, the .text-center class does the same thing natively, as I learned above.
– domoarigato
Feb 2 '15 at 19:43
add a comment |
This works. A hackish way probably, but it works nicely. It was tested for responsive (Y).
.centered {
background-color: teal;
text-align: center;
}
This works. A hackish way probably, but it works nicely. It was tested for responsive (Y).
.centered {
background-color: teal;
text-align: center;
}
edited Jun 30 '16 at 16:01
Peter Mortensen
13.6k1984111
13.6k1984111
answered Aug 9 '13 at 21:47
Ali GajaniAli Gajani
11.1k53674
11.1k53674
works only because the element has diplay inline. Animg
may be set as a block.
– ProfileTwist
Aug 12 '13 at 18:34
2
But it works and I do not see a reason to down vote.
– Ali Gajani
Aug 12 '13 at 18:34
Perhaps in your isolated testing case. I would add.centered img { display: inline; }
to your answer and then it would be worthy of an up vote instead.
– ProfileTwist
Aug 13 '13 at 15:02
No problem. I am sure the OP will find his/her answer :)
– Ali Gajani
Aug 13 '13 at 15:11
in bootstrap 3, the .text-center class does the same thing natively, as I learned above.
– domoarigato
Feb 2 '15 at 19:43
add a comment |
works only because the element has diplay inline. Animg
may be set as a block.
– ProfileTwist
Aug 12 '13 at 18:34
2
But it works and I do not see a reason to down vote.
– Ali Gajani
Aug 12 '13 at 18:34
Perhaps in your isolated testing case. I would add.centered img { display: inline; }
to your answer and then it would be worthy of an up vote instead.
– ProfileTwist
Aug 13 '13 at 15:02
No problem. I am sure the OP will find his/her answer :)
– Ali Gajani
Aug 13 '13 at 15:11
in bootstrap 3, the .text-center class does the same thing natively, as I learned above.
– domoarigato
Feb 2 '15 at 19:43
works only because the element has diplay inline. An
img
may be set as a block.– ProfileTwist
Aug 12 '13 at 18:34
works only because the element has diplay inline. An
img
may be set as a block.– ProfileTwist
Aug 12 '13 at 18:34
2
2
But it works and I do not see a reason to down vote.
– Ali Gajani
Aug 12 '13 at 18:34
But it works and I do not see a reason to down vote.
– Ali Gajani
Aug 12 '13 at 18:34
Perhaps in your isolated testing case. I would add
.centered img { display: inline; }
to your answer and then it would be worthy of an up vote instead.– ProfileTwist
Aug 13 '13 at 15:02
Perhaps in your isolated testing case. I would add
.centered img { display: inline; }
to your answer and then it would be worthy of an up vote instead.– ProfileTwist
Aug 13 '13 at 15:02
No problem. I am sure the OP will find his/her answer :)
– Ali Gajani
Aug 13 '13 at 15:11
No problem. I am sure the OP will find his/her answer :)
– Ali Gajani
Aug 13 '13 at 15:11
in bootstrap 3, the .text-center class does the same thing natively, as I learned above.
– domoarigato
Feb 2 '15 at 19:43
in bootstrap 3, the .text-center class does the same thing natively, as I learned above.
– domoarigato
Feb 2 '15 at 19:43
add a comment |
You can use text-center
for the row, and make sure the internal divs have display:inline-block
( with not float
)
as:
<div class="container">
<div class="row text-center" style="background-color : black;">
<div class="redBlock">A red block</div>
<div class="whiteBlock">A white block</div>
<div class="yellowBlock">A yellow block</div>
</div>
</div>
and css:
.redBlock {
width: 100px;
height: 100px;
background-color: aqua;
display: inline-block
}
.whiteBlock {
width: 100px;
height: 100px;
background-color: white;
display: inline-block
}
.yellowBlock {
width: 100px;
height: 100px;
background-color: yellow;
display: inline-block
}
The fiddle:
http://jsfiddle.net/DTcHh/3177/
add a comment |
You can use text-center
for the row, and make sure the internal divs have display:inline-block
( with not float
)
as:
<div class="container">
<div class="row text-center" style="background-color : black;">
<div class="redBlock">A red block</div>
<div class="whiteBlock">A white block</div>
<div class="yellowBlock">A yellow block</div>
</div>
</div>
and css:
.redBlock {
width: 100px;
height: 100px;
background-color: aqua;
display: inline-block
}
.whiteBlock {
width: 100px;
height: 100px;
background-color: white;
display: inline-block
}
.yellowBlock {
width: 100px;
height: 100px;
background-color: yellow;
display: inline-block
}
The fiddle:
http://jsfiddle.net/DTcHh/3177/
add a comment |
You can use text-center
for the row, and make sure the internal divs have display:inline-block
( with not float
)
as:
<div class="container">
<div class="row text-center" style="background-color : black;">
<div class="redBlock">A red block</div>
<div class="whiteBlock">A white block</div>
<div class="yellowBlock">A yellow block</div>
</div>
</div>
and css:
.redBlock {
width: 100px;
height: 100px;
background-color: aqua;
display: inline-block
}
.whiteBlock {
width: 100px;
height: 100px;
background-color: white;
display: inline-block
}
.yellowBlock {
width: 100px;
height: 100px;
background-color: yellow;
display: inline-block
}
The fiddle:
http://jsfiddle.net/DTcHh/3177/
You can use text-center
for the row, and make sure the internal divs have display:inline-block
( with not float
)
as:
<div class="container">
<div class="row text-center" style="background-color : black;">
<div class="redBlock">A red block</div>
<div class="whiteBlock">A white block</div>
<div class="yellowBlock">A yellow block</div>
</div>
</div>
and css:
.redBlock {
width: 100px;
height: 100px;
background-color: aqua;
display: inline-block
}
.whiteBlock {
width: 100px;
height: 100px;
background-color: white;
display: inline-block
}
.yellowBlock {
width: 100px;
height: 100px;
background-color: yellow;
display: inline-block
}
The fiddle:
http://jsfiddle.net/DTcHh/3177/
answered Jan 13 '15 at 8:39
Alireza FattahiAlireza Fattahi
21.2k86891
21.2k86891
add a comment |
add a comment |
Just set your one column that displays content to col-xs-12 (mobile-first ;-) and configure the container only to control how wide you want your centred content to be, so:
.container {
background-color: blue;
}
.centered {
background-color: red;
}
<body class="container col-xs-offset-3 col-xs-6">
<div class="col-xs-12 centered">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
<body class="container col-xs-offset-1 col-xs-10">
<div class="col-xs-12 centered">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
For a demo, see http://codepen.io/Kebten/pen/gpRNMe :-)
add a comment |
Just set your one column that displays content to col-xs-12 (mobile-first ;-) and configure the container only to control how wide you want your centred content to be, so:
.container {
background-color: blue;
}
.centered {
background-color: red;
}
<body class="container col-xs-offset-3 col-xs-6">
<div class="col-xs-12 centered">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
<body class="container col-xs-offset-1 col-xs-10">
<div class="col-xs-12 centered">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
For a demo, see http://codepen.io/Kebten/pen/gpRNMe :-)
add a comment |
Just set your one column that displays content to col-xs-12 (mobile-first ;-) and configure the container only to control how wide you want your centred content to be, so:
.container {
background-color: blue;
}
.centered {
background-color: red;
}
<body class="container col-xs-offset-3 col-xs-6">
<div class="col-xs-12 centered">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
<body class="container col-xs-offset-1 col-xs-10">
<div class="col-xs-12 centered">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
For a demo, see http://codepen.io/Kebten/pen/gpRNMe :-)
Just set your one column that displays content to col-xs-12 (mobile-first ;-) and configure the container only to control how wide you want your centred content to be, so:
.container {
background-color: blue;
}
.centered {
background-color: red;
}
<body class="container col-xs-offset-3 col-xs-6">
<div class="col-xs-12 centered">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
<body class="container col-xs-offset-1 col-xs-10">
<div class="col-xs-12 centered">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
For a demo, see http://codepen.io/Kebten/pen/gpRNMe :-)
.container {
background-color: blue;
}
.centered {
background-color: red;
}
<body class="container col-xs-offset-3 col-xs-6">
<div class="col-xs-12 centered">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
.container {
background-color: blue;
}
.centered {
background-color: red;
}
<body class="container col-xs-offset-3 col-xs-6">
<div class="col-xs-12 centered">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
<body class="container col-xs-offset-1 col-xs-10">
<div class="col-xs-12 centered">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
<body class="container col-xs-offset-1 col-xs-10">
<div class="col-xs-12 centered">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
edited Jun 30 '16 at 16:04
Peter Mortensen
13.6k1984111
13.6k1984111
answered Jun 13 '15 at 13:14
Kieran RyanKieran Ryan
503515
503515
add a comment |
add a comment |
To center the col- we need to use the below code. cols are floater elements besides margin auto. We will also set it to float none,
<body class="container">
<div class="col-lg-1 col-md-4 centered">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
To center the above col-lg-1 with class of centered, we will write:
.centered {
float: none;
margin-left: auto;
margin-right: auto;
}
To center the content inside the div, use text-align:center
,
.centered {
text-align: center;
}
If you want to center it only on the desktop and larger screen, not on mobile, then use the following media query.
@media (min-width: 768px) {
.centered {
float: none;
margin-left: auto;
margin-right: auto;
}
}
And to center the div only on mobile version, use the below code.
@media (max-width: 768px) {
.centered {
float: none;
margin-left: auto;
margin-right: auto;
}
}
add a comment |
To center the col- we need to use the below code. cols are floater elements besides margin auto. We will also set it to float none,
<body class="container">
<div class="col-lg-1 col-md-4 centered">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
To center the above col-lg-1 with class of centered, we will write:
.centered {
float: none;
margin-left: auto;
margin-right: auto;
}
To center the content inside the div, use text-align:center
,
.centered {
text-align: center;
}
If you want to center it only on the desktop and larger screen, not on mobile, then use the following media query.
@media (min-width: 768px) {
.centered {
float: none;
margin-left: auto;
margin-right: auto;
}
}
And to center the div only on mobile version, use the below code.
@media (max-width: 768px) {
.centered {
float: none;
margin-left: auto;
margin-right: auto;
}
}
add a comment |
To center the col- we need to use the below code. cols are floater elements besides margin auto. We will also set it to float none,
<body class="container">
<div class="col-lg-1 col-md-4 centered">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
To center the above col-lg-1 with class of centered, we will write:
.centered {
float: none;
margin-left: auto;
margin-right: auto;
}
To center the content inside the div, use text-align:center
,
.centered {
text-align: center;
}
If you want to center it only on the desktop and larger screen, not on mobile, then use the following media query.
@media (min-width: 768px) {
.centered {
float: none;
margin-left: auto;
margin-right: auto;
}
}
And to center the div only on mobile version, use the below code.
@media (max-width: 768px) {
.centered {
float: none;
margin-left: auto;
margin-right: auto;
}
}
To center the col- we need to use the below code. cols are floater elements besides margin auto. We will also set it to float none,
<body class="container">
<div class="col-lg-1 col-md-4 centered">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
To center the above col-lg-1 with class of centered, we will write:
.centered {
float: none;
margin-left: auto;
margin-right: auto;
}
To center the content inside the div, use text-align:center
,
.centered {
text-align: center;
}
If you want to center it only on the desktop and larger screen, not on mobile, then use the following media query.
@media (min-width: 768px) {
.centered {
float: none;
margin-left: auto;
margin-right: auto;
}
}
And to center the div only on mobile version, use the below code.
@media (max-width: 768px) {
.centered {
float: none;
margin-left: auto;
margin-right: auto;
}
}
edited Jul 12 '14 at 22:41
Peter Mortensen
13.6k1984111
13.6k1984111
answered Apr 13 '14 at 4:02
Aamer ShahzadAamer Shahzad
8081014
8081014
add a comment |
add a comment |
This is probably not the best answer, but there is one more creative solution to this. As pointed out by koala_dev the column offsetting works only for even column sizes. However, by nesting rows you can achieve centering uneven columns as well.
To stick with the original question where you want to center a column of 1 inside a grid of 12.
- Center a column of 2 by offsetting it 5
- Make a nested row, so you get a new 12 columns inside your 2 columns.
- Since you want to center a column of 1, and 1 is "half" of 2 (what we centered in step 1), you now need to center a column of 6 in your nested row, which is easily done by offsetting it 3.
For example:
<div class="container">
<div class="row">
<div class="col-md-offset-5 col-md-2">
<div class="row">
<div class="col-md-offset-3 col-md-6">
centered column with that has an "original width" of 1 col
</div>
</div>
</div>
</div>
</div>
See this fiddle, please note that you have to increase the size of the output window in order too see the result, otherwise the columns will wrap.
add a comment |
This is probably not the best answer, but there is one more creative solution to this. As pointed out by koala_dev the column offsetting works only for even column sizes. However, by nesting rows you can achieve centering uneven columns as well.
To stick with the original question where you want to center a column of 1 inside a grid of 12.
- Center a column of 2 by offsetting it 5
- Make a nested row, so you get a new 12 columns inside your 2 columns.
- Since you want to center a column of 1, and 1 is "half" of 2 (what we centered in step 1), you now need to center a column of 6 in your nested row, which is easily done by offsetting it 3.
For example:
<div class="container">
<div class="row">
<div class="col-md-offset-5 col-md-2">
<div class="row">
<div class="col-md-offset-3 col-md-6">
centered column with that has an "original width" of 1 col
</div>
</div>
</div>
</div>
</div>
See this fiddle, please note that you have to increase the size of the output window in order too see the result, otherwise the columns will wrap.
add a comment |
This is probably not the best answer, but there is one more creative solution to this. As pointed out by koala_dev the column offsetting works only for even column sizes. However, by nesting rows you can achieve centering uneven columns as well.
To stick with the original question where you want to center a column of 1 inside a grid of 12.
- Center a column of 2 by offsetting it 5
- Make a nested row, so you get a new 12 columns inside your 2 columns.
- Since you want to center a column of 1, and 1 is "half" of 2 (what we centered in step 1), you now need to center a column of 6 in your nested row, which is easily done by offsetting it 3.
For example:
<div class="container">
<div class="row">
<div class="col-md-offset-5 col-md-2">
<div class="row">
<div class="col-md-offset-3 col-md-6">
centered column with that has an "original width" of 1 col
</div>
</div>
</div>
</div>
</div>
See this fiddle, please note that you have to increase the size of the output window in order too see the result, otherwise the columns will wrap.
This is probably not the best answer, but there is one more creative solution to this. As pointed out by koala_dev the column offsetting works only for even column sizes. However, by nesting rows you can achieve centering uneven columns as well.
To stick with the original question where you want to center a column of 1 inside a grid of 12.
- Center a column of 2 by offsetting it 5
- Make a nested row, so you get a new 12 columns inside your 2 columns.
- Since you want to center a column of 1, and 1 is "half" of 2 (what we centered in step 1), you now need to center a column of 6 in your nested row, which is easily done by offsetting it 3.
For example:
<div class="container">
<div class="row">
<div class="col-md-offset-5 col-md-2">
<div class="row">
<div class="col-md-offset-3 col-md-6">
centered column with that has an "original width" of 1 col
</div>
</div>
</div>
</div>
</div>
See this fiddle, please note that you have to increase the size of the output window in order too see the result, otherwise the columns will wrap.
answered Mar 2 '16 at 5:35
reinderreinder
2,19921838
2,19921838
add a comment |
add a comment |
Another approach of offsetting is to have two empty rows, for example:
<div class="col-md-4"></div>
<div class="col-md-4">Centered Content</div>
<div class="col-md-4"></div>
1
2 useless divs, col-md-offset-* is better
– ymakux
Sep 19 '15 at 18:56
1
I agree. I used that method because it actually works well and tested.
– LeRoy
Sep 21 '15 at 7:07
add a comment |
Another approach of offsetting is to have two empty rows, for example:
<div class="col-md-4"></div>
<div class="col-md-4">Centered Content</div>
<div class="col-md-4"></div>
1
2 useless divs, col-md-offset-* is better
– ymakux
Sep 19 '15 at 18:56
1
I agree. I used that method because it actually works well and tested.
– LeRoy
Sep 21 '15 at 7:07
add a comment |
Another approach of offsetting is to have two empty rows, for example:
<div class="col-md-4"></div>
<div class="col-md-4">Centered Content</div>
<div class="col-md-4"></div>
Another approach of offsetting is to have two empty rows, for example:
<div class="col-md-4"></div>
<div class="col-md-4">Centered Content</div>
<div class="col-md-4"></div>
edited Jun 30 '16 at 16:05
Peter Mortensen
13.6k1984111
13.6k1984111
answered Jul 2 '15 at 14:27
LeRoyLeRoy
2,49911937
2,49911937
1
2 useless divs, col-md-offset-* is better
– ymakux
Sep 19 '15 at 18:56
1
I agree. I used that method because it actually works well and tested.
– LeRoy
Sep 21 '15 at 7:07
add a comment |
1
2 useless divs, col-md-offset-* is better
– ymakux
Sep 19 '15 at 18:56
1
I agree. I used that method because it actually works well and tested.
– LeRoy
Sep 21 '15 at 7:07
1
1
2 useless divs, col-md-offset-* is better
– ymakux
Sep 19 '15 at 18:56
2 useless divs, col-md-offset-* is better
– ymakux
Sep 19 '15 at 18:56
1
1
I agree. I used that method because it actually works well and tested.
– LeRoy
Sep 21 '15 at 7:07
I agree. I used that method because it actually works well and tested.
– LeRoy
Sep 21 '15 at 7:07
add a comment |
<div class="container-fluid">
<div class="row">
<div class="col-lg-4 col-lg-offset-4">
<img src="some.jpg">
</div>
</div>
</div>
add a comment |
<div class="container-fluid">
<div class="row">
<div class="col-lg-4 col-lg-offset-4">
<img src="some.jpg">
</div>
</div>
</div>
add a comment |
<div class="container-fluid">
<div class="row">
<div class="col-lg-4 col-lg-offset-4">
<img src="some.jpg">
</div>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="col-lg-4 col-lg-offset-4">
<img src="some.jpg">
</div>
</div>
</div>
edited Sep 13 '17 at 17:46
JGallardo
6,58335567
6,58335567
answered Nov 3 '16 at 13:23
waliwali
463615
463615
add a comment |
add a comment |
We can achieve this by using the table layout mechanism:
The mechanism is:
- Wrap all columns in one div.
- Make that div as a table with a fixed layout.
- Make each column as a table cell.
- Use vertical-align property to control content position.
The sample demo is here
add a comment |
We can achieve this by using the table layout mechanism:
The mechanism is:
- Wrap all columns in one div.
- Make that div as a table with a fixed layout.
- Make each column as a table cell.
- Use vertical-align property to control content position.
The sample demo is here
add a comment |
We can achieve this by using the table layout mechanism:
The mechanism is:
- Wrap all columns in one div.
- Make that div as a table with a fixed layout.
- Make each column as a table cell.
- Use vertical-align property to control content position.
The sample demo is here
We can achieve this by using the table layout mechanism:
The mechanism is:
- Wrap all columns in one div.
- Make that div as a table with a fixed layout.
- Make each column as a table cell.
- Use vertical-align property to control content position.
The sample demo is here
edited May 19 '18 at 18:42
Peter Mortensen
13.6k1984111
13.6k1984111
answered Mar 30 '17 at 13:37
Mohan DereMohan Dere
2,49411014
2,49411014
add a comment |
add a comment |
As koala_dev used in his approach 1, I would prefer the offset method instead of center-block or margins which has limited usage, but as he mentioned:
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.
This can be solved using the following approach for odd columns.
<div class="col-xs-offset-5 col-xs-2">
<div class="col-xs-offset-3">
// Your content here
</div>
</div>
add a comment |
As koala_dev used in his approach 1, I would prefer the offset method instead of center-block or margins which has limited usage, but as he mentioned:
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.
This can be solved using the following approach for odd columns.
<div class="col-xs-offset-5 col-xs-2">
<div class="col-xs-offset-3">
// Your content here
</div>
</div>
add a comment |
As koala_dev used in his approach 1, I would prefer the offset method instead of center-block or margins which has limited usage, but as he mentioned:
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.
This can be solved using the following approach for odd columns.
<div class="col-xs-offset-5 col-xs-2">
<div class="col-xs-offset-3">
// Your content here
</div>
</div>
As koala_dev used in his approach 1, I would prefer the offset method instead of center-block or margins which has limited usage, but as he mentioned:
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.
This can be solved using the following approach for odd columns.
<div class="col-xs-offset-5 col-xs-2">
<div class="col-xs-offset-3">
// Your content here
</div>
</div>
edited Jun 30 '16 at 16:06
Peter Mortensen
13.6k1984111
13.6k1984111
answered Sep 16 '15 at 11:55
carelesslyChoosycarelesslyChoosy
5,08611527
5,08611527
add a comment |
add a comment |
This is not my code, but it works perfectly (tested on Bootstrap 3) and I don't have to mess around with col-offset.
Demo:
/* centered columns styles */
.row-centered {
text-align: center;
}
.col-centered {
display: inline-block;
float: none;
/* reset the text-align */
text-align: left;
/* inline-block space fix */
margin-right: -4px;
text-align: center;
background-color: #ccc;
border: 1px solid #ddd;
}
<div class="container">
<div class="row row-centered">
<div class="col-xs-6 col-centered">Column 6</div>
<div class="col-xs-6 col-centered">Column 6</div>
<div class="col-xs-3 col-centered">Column 3</div>
<div class="col-xs-3 col-centered">Column 3</div>
<div class="col-xs-3 col-centered">Column 3</div>
</div>
</div>
1
that is the solution from codepen.io/makrjean22/pen/xEdKAK
– Serge
Dec 20 '18 at 15:19
add a comment |
This is not my code, but it works perfectly (tested on Bootstrap 3) and I don't have to mess around with col-offset.
Demo:
/* centered columns styles */
.row-centered {
text-align: center;
}
.col-centered {
display: inline-block;
float: none;
/* reset the text-align */
text-align: left;
/* inline-block space fix */
margin-right: -4px;
text-align: center;
background-color: #ccc;
border: 1px solid #ddd;
}
<div class="container">
<div class="row row-centered">
<div class="col-xs-6 col-centered">Column 6</div>
<div class="col-xs-6 col-centered">Column 6</div>
<div class="col-xs-3 col-centered">Column 3</div>
<div class="col-xs-3 col-centered">Column 3</div>
<div class="col-xs-3 col-centered">Column 3</div>
</div>
</div>
1
that is the solution from codepen.io/makrjean22/pen/xEdKAK
– Serge
Dec 20 '18 at 15:19
add a comment |
This is not my code, but it works perfectly (tested on Bootstrap 3) and I don't have to mess around with col-offset.
Demo:
/* centered columns styles */
.row-centered {
text-align: center;
}
.col-centered {
display: inline-block;
float: none;
/* reset the text-align */
text-align: left;
/* inline-block space fix */
margin-right: -4px;
text-align: center;
background-color: #ccc;
border: 1px solid #ddd;
}
<div class="container">
<div class="row row-centered">
<div class="col-xs-6 col-centered">Column 6</div>
<div class="col-xs-6 col-centered">Column 6</div>
<div class="col-xs-3 col-centered">Column 3</div>
<div class="col-xs-3 col-centered">Column 3</div>
<div class="col-xs-3 col-centered">Column 3</div>
</div>
</div>
This is not my code, but it works perfectly (tested on Bootstrap 3) and I don't have to mess around with col-offset.
Demo:
/* centered columns styles */
.row-centered {
text-align: center;
}
.col-centered {
display: inline-block;
float: none;
/* reset the text-align */
text-align: left;
/* inline-block space fix */
margin-right: -4px;
text-align: center;
background-color: #ccc;
border: 1px solid #ddd;
}
<div class="container">
<div class="row row-centered">
<div class="col-xs-6 col-centered">Column 6</div>
<div class="col-xs-6 col-centered">Column 6</div>
<div class="col-xs-3 col-centered">Column 3</div>
<div class="col-xs-3 col-centered">Column 3</div>
<div class="col-xs-3 col-centered">Column 3</div>
</div>
</div>
/* centered columns styles */
.row-centered {
text-align: center;
}
.col-centered {
display: inline-block;
float: none;
/* reset the text-align */
text-align: left;
/* inline-block space fix */
margin-right: -4px;
text-align: center;
background-color: #ccc;
border: 1px solid #ddd;
}
<div class="container">
<div class="row row-centered">
<div class="col-xs-6 col-centered">Column 6</div>
<div class="col-xs-6 col-centered">Column 6</div>
<div class="col-xs-3 col-centered">Column 3</div>
<div class="col-xs-3 col-centered">Column 3</div>
<div class="col-xs-3 col-centered">Column 3</div>
</div>
</div>
/* centered columns styles */
.row-centered {
text-align: center;
}
.col-centered {
display: inline-block;
float: none;
/* reset the text-align */
text-align: left;
/* inline-block space fix */
margin-right: -4px;
text-align: center;
background-color: #ccc;
border: 1px solid #ddd;
}
<div class="container">
<div class="row row-centered">
<div class="col-xs-6 col-centered">Column 6</div>
<div class="col-xs-6 col-centered">Column 6</div>
<div class="col-xs-3 col-centered">Column 3</div>
<div class="col-xs-3 col-centered">Column 3</div>
<div class="col-xs-3 col-centered">Column 3</div>
</div>
</div>
edited May 9 '18 at 21:05
Ahmed Hussein
3081622
3081622
answered Apr 19 '18 at 9:22
Binar WebBinar Web
405117
405117
1
that is the solution from codepen.io/makrjean22/pen/xEdKAK
– Serge
Dec 20 '18 at 15:19
add a comment |
1
that is the solution from codepen.io/makrjean22/pen/xEdKAK
– Serge
Dec 20 '18 at 15:19
1
1
that is the solution from codepen.io/makrjean22/pen/xEdKAK
– Serge
Dec 20 '18 at 15:19
that is the solution from codepen.io/makrjean22/pen/xEdKAK
– Serge
Dec 20 '18 at 15:19
add a comment |
You can use the very flexible solution flexbox to your Bootstrap.
justify-content: center;
can center your column.
Check out flex.
add a comment |
You can use the very flexible solution flexbox to your Bootstrap.
justify-content: center;
can center your column.
Check out flex.
add a comment |
You can use the very flexible solution flexbox to your Bootstrap.
justify-content: center;
can center your column.
Check out flex.
You can use the very flexible solution flexbox to your Bootstrap.
justify-content: center;
can center your column.
Check out flex.
edited May 19 '18 at 18:38
Peter Mortensen
13.6k1984111
13.6k1984111
answered Nov 6 '17 at 8:45
HarshHarsh
1016
1016
add a comment |
add a comment |
Because I never have the need to center only a single .col-
within a .row
, I set the following class on the wrapping .row
of my target columns.
.col-center > [class*="col-"] {
float: none;
margin-left: auto;
margin-right: auto;
}
Example
<div class="full-container">
<div class="row col-center">
<div class="col-xs-11">
Foo
</div>
<div class="col-xs-11">
Bar
</div>
</div>
</div>
add a comment |
Because I never have the need to center only a single .col-
within a .row
, I set the following class on the wrapping .row
of my target columns.
.col-center > [class*="col-"] {
float: none;
margin-left: auto;
margin-right: auto;
}
Example
<div class="full-container">
<div class="row col-center">
<div class="col-xs-11">
Foo
</div>
<div class="col-xs-11">
Bar
</div>
</div>
</div>
add a comment |
Because I never have the need to center only a single .col-
within a .row
, I set the following class on the wrapping .row
of my target columns.
.col-center > [class*="col-"] {
float: none;
margin-left: auto;
margin-right: auto;
}
Example
<div class="full-container">
<div class="row col-center">
<div class="col-xs-11">
Foo
</div>
<div class="col-xs-11">
Bar
</div>
</div>
</div>
Because I never have the need to center only a single .col-
within a .row
, I set the following class on the wrapping .row
of my target columns.
.col-center > [class*="col-"] {
float: none;
margin-left: auto;
margin-right: auto;
}
Example
<div class="full-container">
<div class="row col-center">
<div class="col-xs-11">
Foo
</div>
<div class="col-xs-11">
Bar
</div>
</div>
</div>
answered Apr 27 '15 at 21:41
Walter RomanWalter Roman
3,44012533
3,44012533
add a comment |
add a comment |
For those looking to center the column elements on the screen when you don't have the exact number to fill your grid, I have written a little piece of JavaScript to return the class names:
function colCalculator(totalNumberOfElements, elementsPerRow, screenSize) {
var arrayFill = function (size, content) {
return Array.apply(null, Array(size)).map(String.prototype.valueOf, content);
};
var elementSize = parseInt(12 / elementsPerRow, 10);
var normalClassName = 'col-' + screenSize + '-' + elementSize;
var numberOfFittingElements = parseInt(totalNumberOfElements / elementsPerRow, 10) * elementsPerRow;
var numberOfRemainingElements = totalNumberOfElements - numberOfFittingElements;
var ret = arrayFill(numberOfFittingElements, normalClassName);
var remainingSize = 12 - numberOfRemainingElements * elementSize;
var remainingLeftSize = parseInt(remainingSize / 2, 10);
return ret.concat(arrayFill(numberOfRemainingElements, normalClassName + ' col-' + screenSize + '-push-' + remainingLeftSize));
}
If you have 5 elements and you want to have 3 per row on a md
screen, you do this:
colCalculator(5, 3, 'md')
>> ["col-md-4", "col-md-4", "col-md-4", "col-md-4 col-md-push-2", "col-md-4 col-md-push-2"]
Keep in mind, the second argument must be dividable by 12.
add a comment |
For those looking to center the column elements on the screen when you don't have the exact number to fill your grid, I have written a little piece of JavaScript to return the class names:
function colCalculator(totalNumberOfElements, elementsPerRow, screenSize) {
var arrayFill = function (size, content) {
return Array.apply(null, Array(size)).map(String.prototype.valueOf, content);
};
var elementSize = parseInt(12 / elementsPerRow, 10);
var normalClassName = 'col-' + screenSize + '-' + elementSize;
var numberOfFittingElements = parseInt(totalNumberOfElements / elementsPerRow, 10) * elementsPerRow;
var numberOfRemainingElements = totalNumberOfElements - numberOfFittingElements;
var ret = arrayFill(numberOfFittingElements, normalClassName);
var remainingSize = 12 - numberOfRemainingElements * elementSize;
var remainingLeftSize = parseInt(remainingSize / 2, 10);
return ret.concat(arrayFill(numberOfRemainingElements, normalClassName + ' col-' + screenSize + '-push-' + remainingLeftSize));
}
If you have 5 elements and you want to have 3 per row on a md
screen, you do this:
colCalculator(5, 3, 'md')
>> ["col-md-4", "col-md-4", "col-md-4", "col-md-4 col-md-push-2", "col-md-4 col-md-push-2"]
Keep in mind, the second argument must be dividable by 12.
add a comment |
For those looking to center the column elements on the screen when you don't have the exact number to fill your grid, I have written a little piece of JavaScript to return the class names:
function colCalculator(totalNumberOfElements, elementsPerRow, screenSize) {
var arrayFill = function (size, content) {
return Array.apply(null, Array(size)).map(String.prototype.valueOf, content);
};
var elementSize = parseInt(12 / elementsPerRow, 10);
var normalClassName = 'col-' + screenSize + '-' + elementSize;
var numberOfFittingElements = parseInt(totalNumberOfElements / elementsPerRow, 10) * elementsPerRow;
var numberOfRemainingElements = totalNumberOfElements - numberOfFittingElements;
var ret = arrayFill(numberOfFittingElements, normalClassName);
var remainingSize = 12 - numberOfRemainingElements * elementSize;
var remainingLeftSize = parseInt(remainingSize / 2, 10);
return ret.concat(arrayFill(numberOfRemainingElements, normalClassName + ' col-' + screenSize + '-push-' + remainingLeftSize));
}
If you have 5 elements and you want to have 3 per row on a md
screen, you do this:
colCalculator(5, 3, 'md')
>> ["col-md-4", "col-md-4", "col-md-4", "col-md-4 col-md-push-2", "col-md-4 col-md-push-2"]
Keep in mind, the second argument must be dividable by 12.
For those looking to center the column elements on the screen when you don't have the exact number to fill your grid, I have written a little piece of JavaScript to return the class names:
function colCalculator(totalNumberOfElements, elementsPerRow, screenSize) {
var arrayFill = function (size, content) {
return Array.apply(null, Array(size)).map(String.prototype.valueOf, content);
};
var elementSize = parseInt(12 / elementsPerRow, 10);
var normalClassName = 'col-' + screenSize + '-' + elementSize;
var numberOfFittingElements = parseInt(totalNumberOfElements / elementsPerRow, 10) * elementsPerRow;
var numberOfRemainingElements = totalNumberOfElements - numberOfFittingElements;
var ret = arrayFill(numberOfFittingElements, normalClassName);
var remainingSize = 12 - numberOfRemainingElements * elementSize;
var remainingLeftSize = parseInt(remainingSize / 2, 10);
return ret.concat(arrayFill(numberOfRemainingElements, normalClassName + ' col-' + screenSize + '-push-' + remainingLeftSize));
}
If you have 5 elements and you want to have 3 per row on a md
screen, you do this:
colCalculator(5, 3, 'md')
>> ["col-md-4", "col-md-4", "col-md-4", "col-md-4 col-md-push-2", "col-md-4 col-md-push-2"]
Keep in mind, the second argument must be dividable by 12.
answered Sep 6 '15 at 21:29
jsgoupiljsgoupil
2,11112236
2,11112236
add a comment |
add a comment |
To center more than one column in a Bootstrap row - and the number of cols are odd, simply add this css
class to all the columns in that row:
.many-cols-centered { // To horizontally center bootstrap odd cols, eg col-lg-9, col-md-3, works well in lg
display:inline-block;
float:none;
}
So in your HTML you have something like:
<div class="row text-center"> <!-- text-center centers all text in that row -->
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12 many-cols-centered">
<img src='assets/image1.jpg'>
<h3>You See</h3>
<p>I love coding.</p>
</div>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12 many-cols-centered">
<img src='assets/image2.jpg'>
<h3>You See</h3>
<p>I love coding.</p>
</div>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12 many-cols-centered">
<img src='assets/image3.jpg'>
<h3>You See</h3>
<p>I love coding.</p>
</div>
</div>
add a comment |
To center more than one column in a Bootstrap row - and the number of cols are odd, simply add this css
class to all the columns in that row:
.many-cols-centered { // To horizontally center bootstrap odd cols, eg col-lg-9, col-md-3, works well in lg
display:inline-block;
float:none;
}
So in your HTML you have something like:
<div class="row text-center"> <!-- text-center centers all text in that row -->
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12 many-cols-centered">
<img src='assets/image1.jpg'>
<h3>You See</h3>
<p>I love coding.</p>
</div>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12 many-cols-centered">
<img src='assets/image2.jpg'>
<h3>You See</h3>
<p>I love coding.</p>
</div>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12 many-cols-centered">
<img src='assets/image3.jpg'>
<h3>You See</h3>
<p>I love coding.</p>
</div>
</div>
add a comment |
To center more than one column in a Bootstrap row - and the number of cols are odd, simply add this css
class to all the columns in that row:
.many-cols-centered { // To horizontally center bootstrap odd cols, eg col-lg-9, col-md-3, works well in lg
display:inline-block;
float:none;
}
So in your HTML you have something like:
<div class="row text-center"> <!-- text-center centers all text in that row -->
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12 many-cols-centered">
<img src='assets/image1.jpg'>
<h3>You See</h3>
<p>I love coding.</p>
</div>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12 many-cols-centered">
<img src='assets/image2.jpg'>
<h3>You See</h3>
<p>I love coding.</p>
</div>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12 many-cols-centered">
<img src='assets/image3.jpg'>
<h3>You See</h3>
<p>I love coding.</p>
</div>
</div>
To center more than one column in a Bootstrap row - and the number of cols are odd, simply add this css
class to all the columns in that row:
.many-cols-centered { // To horizontally center bootstrap odd cols, eg col-lg-9, col-md-3, works well in lg
display:inline-block;
float:none;
}
So in your HTML you have something like:
<div class="row text-center"> <!-- text-center centers all text in that row -->
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12 many-cols-centered">
<img src='assets/image1.jpg'>
<h3>You See</h3>
<p>I love coding.</p>
</div>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12 many-cols-centered">
<img src='assets/image2.jpg'>
<h3>You See</h3>
<p>I love coding.</p>
</div>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12 many-cols-centered">
<img src='assets/image3.jpg'>
<h3>You See</h3>
<p>I love coding.</p>
</div>
</div>
answered Mar 29 '17 at 13:27
Ruto CollinsRuto Collins
1,111617
1,111617
add a comment |
add a comment |
Try this code.
<body class="container">
<div class="col-lg-1 col-lg-offset-10">
<img data-src="holder.js/100x100" alt="" />
</div>
Here i have used col-lg-1, and the offset should be 10 for properly centered the div on large devices, if you need it to center on mediam to large device then just change the lg to md and so on, let me know if any issue.
add a comment |
Try this code.
<body class="container">
<div class="col-lg-1 col-lg-offset-10">
<img data-src="holder.js/100x100" alt="" />
</div>
Here i have used col-lg-1, and the offset should be 10 for properly centered the div on large devices, if you need it to center on mediam to large device then just change the lg to md and so on, let me know if any issue.
add a comment |
Try this code.
<body class="container">
<div class="col-lg-1 col-lg-offset-10">
<img data-src="holder.js/100x100" alt="" />
</div>
Here i have used col-lg-1, and the offset should be 10 for properly centered the div on large devices, if you need it to center on mediam to large device then just change the lg to md and so on, let me know if any issue.
Try this code.
<body class="container">
<div class="col-lg-1 col-lg-offset-10">
<img data-src="holder.js/100x100" alt="" />
</div>
Here i have used col-lg-1, and the offset should be 10 for properly centered the div on large devices, if you need it to center on mediam to large device then just change the lg to md and so on, let me know if any issue.
edited May 21 '18 at 4:44
answered Aug 24 '17 at 16:43
BangashBangash
879
879
add a comment |
add a comment |
If you put this on your row, all of the columns inside will be centered:
.row-centered {
display: flex;
justify-content: space-between;
}
made the element non responsive.
– gfivehost
Feb 17 '17 at 14:22
add a comment |
If you put this on your row, all of the columns inside will be centered:
.row-centered {
display: flex;
justify-content: space-between;
}
made the element non responsive.
– gfivehost
Feb 17 '17 at 14:22
add a comment |
If you put this on your row, all of the columns inside will be centered:
.row-centered {
display: flex;
justify-content: space-between;
}
If you put this on your row, all of the columns inside will be centered:
.row-centered {
display: flex;
justify-content: space-between;
}
answered Aug 16 '16 at 19:59
Eran ShabiEran Shabi
5,74962040
5,74962040
made the element non responsive.
– gfivehost
Feb 17 '17 at 14:22
add a comment |
made the element non responsive.
– gfivehost
Feb 17 '17 at 14:22
made the element non responsive.
– gfivehost
Feb 17 '17 at 14:22
made the element non responsive.
– gfivehost
Feb 17 '17 at 14:22
add a comment |
Try this
<div class="row">
<div class="col-xs-2 col-xs-offset-5"></div>
</div>
You can use other col
as well like col-md-2
, etc.
add a comment |
Try this
<div class="row">
<div class="col-xs-2 col-xs-offset-5"></div>
</div>
You can use other col
as well like col-md-2
, etc.
add a comment |
Try this
<div class="row">
<div class="col-xs-2 col-xs-offset-5"></div>
</div>
You can use other col
as well like col-md-2
, etc.
Try this
<div class="row">
<div class="col-xs-2 col-xs-offset-5"></div>
</div>
You can use other col
as well like col-md-2
, etc.
edited Sep 13 '17 at 17:47
JGallardo
6,58335567
6,58335567
answered Mar 8 '17 at 6:46
UmarJamilUmarJamil
6111
6111
add a comment |
add a comment |
I suggest simply to use the class text-center
:
<body class="container">
<div class="col-md-12 text-center">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
add a comment |
I suggest simply to use the class text-center
:
<body class="container">
<div class="col-md-12 text-center">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
add a comment |
I suggest simply to use the class text-center
:
<body class="container">
<div class="col-md-12 text-center">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
I suggest simply to use the class text-center
:
<body class="container">
<div class="col-md-12 text-center">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
edited May 19 '18 at 18:32
Peter Mortensen
13.6k1984111
13.6k1984111
answered Mar 30 '18 at 6:53
Dadhich SouravDadhich Sourav
167216
167216
add a comment |
add a comment |
To be more precise Bootstrap's grid system contains 12 columns and to center any content let’s say, for instance, the content takes up one column. One will need to occupy two columns of Bootstrap's grid and place the content on half of the two occupied columns.
<div class="row">
<div class="col-xs-2 col-xs-offset-5 centered">
... your content / data will come here ...
</div>
</div>
'col-xs-offset-5' is telling the grid system where to start placing the content.
'col-xs-2' is telling the grid system how many of the left over columns should the content occupy.
'centered' will be a defined class that will center the content.
Here is how this example looks like in Bootstrap's grid system.
Columns:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12
.......offset....... .data. .......not used........
add a comment |
To be more precise Bootstrap's grid system contains 12 columns and to center any content let’s say, for instance, the content takes up one column. One will need to occupy two columns of Bootstrap's grid and place the content on half of the two occupied columns.
<div class="row">
<div class="col-xs-2 col-xs-offset-5 centered">
... your content / data will come here ...
</div>
</div>
'col-xs-offset-5' is telling the grid system where to start placing the content.
'col-xs-2' is telling the grid system how many of the left over columns should the content occupy.
'centered' will be a defined class that will center the content.
Here is how this example looks like in Bootstrap's grid system.
Columns:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12
.......offset....... .data. .......not used........
add a comment |
To be more precise Bootstrap's grid system contains 12 columns and to center any content let’s say, for instance, the content takes up one column. One will need to occupy two columns of Bootstrap's grid and place the content on half of the two occupied columns.
<div class="row">
<div class="col-xs-2 col-xs-offset-5 centered">
... your content / data will come here ...
</div>
</div>
'col-xs-offset-5' is telling the grid system where to start placing the content.
'col-xs-2' is telling the grid system how many of the left over columns should the content occupy.
'centered' will be a defined class that will center the content.
Here is how this example looks like in Bootstrap's grid system.
Columns:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12
.......offset....... .data. .......not used........
To be more precise Bootstrap's grid system contains 12 columns and to center any content let’s say, for instance, the content takes up one column. One will need to occupy two columns of Bootstrap's grid and place the content on half of the two occupied columns.
<div class="row">
<div class="col-xs-2 col-xs-offset-5 centered">
... your content / data will come here ...
</div>
</div>
'col-xs-offset-5' is telling the grid system where to start placing the content.
'col-xs-2' is telling the grid system how many of the left over columns should the content occupy.
'centered' will be a defined class that will center the content.
Here is how this example looks like in Bootstrap's grid system.
Columns:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12
.......offset....... .data. .......not used........
edited May 19 '18 at 18:34
Peter Mortensen
13.6k1984111
13.6k1984111
answered Dec 29 '17 at 12:24
TechTech
16219
16219
add a comment |
add a comment |
Append this snippet inside your .row or your .col, this is for bootstrap 4*
d-flex justify-content-center
add a comment |
Append this snippet inside your .row or your .col, this is for bootstrap 4*
d-flex justify-content-center
add a comment |
Append this snippet inside your .row or your .col, this is for bootstrap 4*
d-flex justify-content-center
Append this snippet inside your .row or your .col, this is for bootstrap 4*
d-flex justify-content-center
answered Dec 31 '18 at 6:44
Thomas ChirwaThomas Chirwa
609
609
add a comment |
add a comment |
Don't forget to add !important
. Then you can be sure that element really will be in the center:
.col-centered{
float: none !important;
margin: 0 auto !important;
}
add a comment |
Don't forget to add !important
. Then you can be sure that element really will be in the center:
.col-centered{
float: none !important;
margin: 0 auto !important;
}
add a comment |
Don't forget to add !important
. Then you can be sure that element really will be in the center:
.col-centered{
float: none !important;
margin: 0 auto !important;
}
Don't forget to add !important
. Then you can be sure that element really will be in the center:
.col-centered{
float: none !important;
margin: 0 auto !important;
}
edited Jun 30 '16 at 15:50
Peter Mortensen
13.6k1984111
13.6k1984111
answered Jun 24 '16 at 9:30
BoomerangeBoomerange
200414
200414
add a comment |
add a comment |
Method 1:
<div class="container">
<div class="row">
<div class="col-md-2 col-md-offset-5">
YOUR CONTENT
</div>
</div>
</div>
Method 2:
CSS
.float-center{float: none;}
<div class="container">
<div id="mydev" class="center-block float-center" style="width:75%;">
YOUR CONTENT
</div>
</div>
Bootstrap Tips, examples and tools: OnAirCode
add a comment |
Method 1:
<div class="container">
<div class="row">
<div class="col-md-2 col-md-offset-5">
YOUR CONTENT
</div>
</div>
</div>
Method 2:
CSS
.float-center{float: none;}
<div class="container">
<div id="mydev" class="center-block float-center" style="width:75%;">
YOUR CONTENT
</div>
</div>
Bootstrap Tips, examples and tools: OnAirCode
add a comment |
Method 1:
<div class="container">
<div class="row">
<div class="col-md-2 col-md-offset-5">
YOUR CONTENT
</div>
</div>
</div>
Method 2:
CSS
.float-center{float: none;}
<div class="container">
<div id="mydev" class="center-block float-center" style="width:75%;">
YOUR CONTENT
</div>
</div>
Bootstrap Tips, examples and tools: OnAirCode
Method 1:
<div class="container">
<div class="row">
<div class="col-md-2 col-md-offset-5">
YOUR CONTENT
</div>
</div>
</div>
Method 2:
CSS
.float-center{float: none;}
<div class="container">
<div id="mydev" class="center-block float-center" style="width:75%;">
YOUR CONTENT
</div>
</div>
Bootstrap Tips, examples and tools: OnAirCode
edited Nov 25 '17 at 8:36
answered Nov 25 '17 at 8:19
Pacific P. RegmiPacific P. Regmi
9871412
9871412
add a comment |
add a comment |
1 2
next
protected by Josh Crozier Sep 5 '14 at 14:05
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?