Center a column using Twitter Bootstrap 3(使用 Twitter Bootstrap 3 居中列)
问题描述
How do I center a div of one column size within the container (12 columns) in Twitter Bootstrap 3?
.centered {
background-color: red;
}
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<body class="container">
<div class="col-lg-1 col-offset-6 centered">
<img data-src="aG9sZGVyLmpzLzEwMHgxMDA=" alt="IiAvJmd0OwogICZsdDsvZGl2Jmd0OwombHQ7L2JvZHkmZ3Q7PC9jb2RlPjwvcHJlPjxwPg0KPC9kaXY+PHA+DQo8L2Rpdj48cD4NCjwvcD4KPHA+SSB3YW50IGEgPGNvZGU+ZGl2PC9jb2RlPiwgd2l0aCBhIGNsYXNzIDxjb2RlPi5jZW50ZXJlZDwvY29kZT4gdG8gYmUgY2VudGVyZWQgd2l0aGluIHRoZSBjb250YWluZXIuIEkgbWF5IHVzZSBhIHJvdyBpZiB0aGVyZSBhcmUgbXVsdGlwbGUgPGNvZGU+ZGl2PC9jb2RlPnMsIGJ1dCBmb3Igbm93IEkganVzdCB3YW50IGEgPGNvZGU+ZGl2PC9jb2RlPiB3aXRoIHRoZSBzaXplIG9mIG9uZSBjb2x1bW4gY2VudGVyZWQgd2l0aGluIHRoZSBjb250YWluZXIgKDEyIGNvbHVtbnMpLjwvcD4KPHA+SSBhbSBhbHNvIG5vdCBzdXJlIHRoZSBhYm92ZSBhcHByb2FjaCBpcyBnb29kIGVub3VnaCBhcyB0aGUgaW50ZW50aW9uIGlzIG5vdCB0byBvZmZzZXQgdGhlIDxjb2RlPmRpdjwvY29kZT4gYnkgaGFsZi4gSSBkbyBub3QgbmVlZCBmcmVlIHNwYWNlcyBvdXRzaWRlIHRoZSA8Y29kZT5kaXY8L2NvZGU+IGFuZCB0aGUgY29udGVudHMgb2YgdGhlIDxjb2RlPmRpdjwvY29kZT4gc2hyaW5rIGluIHByb3BvcnRpb24uIEkgd2FudCB0byA8c3Ryb25nPmVtcHR5IHNwYWNlIG91dHNpZGUgdGhlIGRpdiB0byBiZSBldmVubHkgZGlzdHJpYnV0ZWQ8L3N0cm9uZz4gKHNocmluayB0aWxsIHRoZSBjb250YWluZXIgd2lkdGggaXMgZXF1YWwgdG8gb25lIGNvbHVtbikuPC9wPjxkaXYgY2xhc3M9"h2_lin"> 解决方案
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.
这篇关于使用 Twitter Bootstrap 3 居中列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Twitter Bootstrap 3 居中列


- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01