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.

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

  1. CSS布局之div交叉排布与底部对齐--flex实现

    最近在用wordpress写页面时,设计师给出了一种网页排布图样,之前从未遇到过,其在电脑上(分辨率大于768px)的效果图如下: 而在手机(分辨率小于等于768px)上要求这样排列: 我想到了两种方 ...

  2. 水平垂直居中div(css3)

    一.在需要居中的元素加上如下C3属性即可: <!doctype html><html lang="en"><head> <meta cha ...

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

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  4. DIV CSS 网页兼容全搞定 (IE6 IE7 IE8 IE9 火狐 谷歌)

    CSS兼容常用技巧 请尽量用xhtml格式写代码,而且DOCTYPE影响 CSS 处理,作为W3C标准,一定要加DOCTYPE声明. 1.div的垂直居中问题 vertical-align:middl ...

  5. div img居中的方式

    想让div中的img水平和垂直都居中,可以将img放在div中,img的样式:height:100%;width:100%; 外部定义div的宽度和高度,然后定义line-height行高,div外部 ...

  6. HTML CSS + DIV实现局部布局

    HTML CSS + DIV实现局部布局 HTML CSS + DIV实现局部布局 1.本章教大家掌握2种布局方式: 1)顶部导航菜单布局,效果图: 2)购物版块布局,效果图: 2.技术目标: 使用d ...

  7. js 弹出div窗口 可移动 可关闭 (转)

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  8. DIV 布局 左中右

    <style type="text/css">body{ margin:0; padding:0;}.Header{ height:100px; background: ...

  9. 转载:DIV+CSS有可能遇到的问题

    [总结]DIV+CSS有可能遇到的问题 一.超链接访问过后hover样式就不出现的问题? 被点击访问过的超链接样式不在具有hover和active了,解决方法是改变CSS属性的排列顺序: L-V-H- ...

随机推荐

  1. ZOJ 3469 Food Delivery 区间DP

    这道题我不会,看了网上的题解才会的,涨了姿势,现阶段还是感觉区间DP比较难,主要是太弱...QAQ 思路中其实有贪心的意思,n个住户加一个商店,分布在一维直线上,应该是从商店开始,先向两边距离近的送, ...

  2. [King.yue]Ext.net 页面布局Flex

    此属性仅当父布局HboxLayout或使用VboxLayout.此配置选项被应用到子项目容器管理的布局.每个子项目以Flex属性被弯曲的水平根据每个项目的相对弹性值比较,都带有一个弯曲值指定项目金额. ...

  3. class0513(html基础加强)

    内容:HTML.CSS 目标:掌握手写HTML实现一般难度的Web页面的能力(如网站注册表单),为ASP.Net学习打基础.坚持手写HTML,可视化设计只是一种自学的手段. 参考书:张孝祥<Ja ...

  4. 题目1023:EXCEL排序(多关键字+快排+尚未解决)

    http://ac.jobdu.com/problem.php?pid=1023 题目描述: Excel可以对一组纪录按任意指定列排序.现请你编写程序实现类似功能. 对每个测试用例,首先输出1行“Ca ...

  5. 【Kafka入门】Kafka基础结构和知识

    基本概念的总结 在基本的Kafka架构中,producer将消息发布到Kafka话题中,一个Kafka话题通常指消息的类别或者名称,Kafka话题被集群中一个充当Kafka server角色的 bro ...

  6. 【转】hive简介安装 配置常见问题和例子

    原文来自:  http://blog.csdn.net/zhumin726/article/details/8027802 1 HIVE概述 Hive是基于Hadoop的一个数据仓库工具,可以将结构化 ...

  7. SQL2008-c:\PROGRA~1\COMMON~1\System\OLEDB~1\oledb32.dll出错找不到指定的模块

    MSSQL2000企业管理器里无法查询数据 SQL server无法执行查询,因为一些文件丢失或未注册等问题的解决直接在企业管理器里无法查询数据,但是用查询分析器可以查看数据,重装了SqlServer ...

  8. A Tour of Go Maps

    A map maps keys to values. Maps must be created with make (not new) before use; the nil map is empty ...

  9. charindex的用法

    declare @str nvarchar(50);set @str='462,464,2';select @str as '字符串'select len(@str) as '字符长度' select ...

  10. GridView控件显示图片

    与图片的二进制数据库存储和显示 1.将图片以二进制存入数据库 2.读取二进制图片在页面显示 3.设置Image控件显示从数据库中读出的二进制图片 4.GridView中ImageField以URL方式 ...