Bootstrap 3 栏居中 responsive centered columns

本文提供了一种解决Bootstrap3中居中多个列的方法,包括响应式设计,并且适用于移动设备。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

An easy solution to center multiple columns in Bootstrap 3, also on mobile

 

The Problem

Sometimes you want to center Bootstrap columns, usually when you have an uneven number of them, or when you give them a max-width and they don't fill up the containing row.

 

The Solution

 

The Markup

Just add the class .row-centered to the row and .col-centered to the columns.

<div class="container">
    <div class="row row-centered">
        <div class="col-xs-6 col-centered"></div>
        <div class="col-xs-6 col-centered"></div>
        <div class="col-xs-3 col-centered"></div>
        <div class="col-xs-3 col-centered"></div>
        <div class="col-xs-3 col-centered"></div>
    </div>
</div>

 

The Css

This simple css code centers the columns (.col-centered) inside the row (.row-centered).

/* 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;
}

 

You can also set a min-width, a max-width or a fixed width to the columns.

.col-fixed {
    /* custom width */
    width:320px;
}
.col-min {
    /* custom min width */
    min-width:320px;
}
.col-max {
    /* custom max width */
    max-width:320px;
}

 

IE8 Support

The code works also on IE8 only if you make Bootstrap 3 work on IE8. To make it work you need to use Respond.js and follow the respond.js guidelines:

  • Change the Bootstrap 3 Css netdna.bootstrapcdn.com to a local file: Respond.js only works with local Css files unless additional steps are taken.
  • Change the Css from a style element to an external local Css with link rel="stylesheet": Respond.js doesn't parse Css referenced via @import, nor does it work with media queries within style elements.
  • Reference the Respond.js script after all of your Css.

 

原文:http://www.minimit.com/articles/solutions-tutorials/bootstrap-3-responsive-centered-columns

转自:Bootstrap 3 栏居中 responsive centered columns

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值