How to center a div in bootstrap3

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-4col-X-6col-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.

时间: 2024-10-27 04:09:20

How to center a div in bootstrap3的相关文章

DIV三列同行

<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus?"> <meta name="Author" content=""> <meta name="

div如何居中

整理一点关于div居中的方法.哪里有不妥欢迎大家拍砖~ div里的内容如何居中: 水平居中(非块级元素): text-align:center; 垂直居中: vertical-align:middle; 内容是文字的话,可以将height和line-height设置成一样的值: 简单方法(但不是全部兼容): 1 display: -webkit-flex; 2 display: flex; 3 -webkit-align-items: center; 4 align-items: center;

div 水平垂直居中

已知布局为如下: <div id="container"> <p>test</p> <div id="center">12345</div> </div> 不知道宽和高的情况下: 方法一: #container{ position:relative; background:blue; } #center{ background:red; position:absolute; left:50%;

【转载】CSS + DIV 实现局部布局

HTML CSS + DIV实现局部布局 1.本章教大家掌握2种布局方式: 1)顶部导航菜单布局,效果图: 2)购物版块布局,效果图: 2.技术目标:使用div + ul-li实现导航菜单布局    使用div + dl-dt-dd实现购物版块布局 3.一般的局部布局无非采用如下的技术:    1)div + ul(ol)-li:用于分类导航或菜单等场合    2)div + dl-dt-dd:用于图文混编场合    3)table-tr-td:用于图文布局或显示数据的场合    4)form

DIV+CSS 网页兼容性问题(IE6 IE7 IE8 IE9 火狐 chorm)

本文收集整理了IE7,6与Fireofx的CSS兼容性处理技巧,供大家参考. CSS兼容常用技巧 请尽量用xhtml格式写代码,而且DOCTYPE影响 CSS 处理,作为W3C标准,一定要加DOCTYPE声明. 1.div的垂直居中问题 vertical-align:middle; 将行距增加到和整个DIV一样高 line-height:200px; 然后插入文字,就垂直居中了.缺点是要控制内容不要换行 http://www.php100.com 2. margin加倍的问题 设置为float的

用div和css样式控制页面布局

1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 5 <title>网页布局练习</title> 6 7 <link href=&qu

让DIV垂直居中的几种办法

1.使用CSS3 的伸缩盒布局 <!doctype html> <html> <head> <meta charset="utf-8"> <style> #container { height: 400px; width: 100%; background-color: gray; display:-webkit-flex; display: flex; flex-direction: row; -webkit-flex-di

如何自动在html页面加载时动态改变div等元素的高度和宽度

这里需要用到jquery + css.原理是在页面加载时用javascript去动态改变一个class的高度和宽度.这样结合javascript能动态获取浏览器/页面的高度和宽度,从而使得div能动态的跟随浏览页面的大小变化而变化并且不影响高宽比.下面的代码创建一个手机页面,每一行三个图片分占33%,每个图片div的高和宽会随着浏览器的大小变化而自适应. <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"

Js无刷新添加新层,拖动DIV层可互换位置的JavaScript实现

<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>Js无刷新添加新层</title><style>body {margin:0px;padding:0px;font-size:12px;text-align:center;}body > div {text-ali