如何在bootstrap 3中创建响应按钮?

时间:2014-02-11 18:19:54

标签: html5 css3 twitter-bootstrap twitter-bootstrap-3

我试图为768px屏幕显示btn-sm,但它一直向我显示btn-xs。

这是我的HTML代码

<div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3 col-sm-offset-3">
    <button name="magnet" class="btn btn-success btn-sm btn-xs btn-lg">
        <em class="glyphicon glyphicon-home"></em>
        Home
    </button>

    <button name="magnet" class="btn btn-danger btn-sm btn-xs">
        <em class="glyphicon glyphicon-download"></em>
        Download
    </button>
</div>

是否可以在不使用媒体查询的情况下完成此任务?

1 个答案:

答案 0 :(得分:8)

您的按钮不会仅使用HTML进行神奇缩放。所以你有两个选择。

选项#1:重复

这是最丑陋的一个,但它不符合您的需求,因为它不需要额外的媒体查询:

<div class="container">
  <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3 col-sm-offset-3">
    <button name="magnet" class="btn btn-success btn-xs visible-xs"><em class="glyphicon glyphicon-home"></em> Home</button>
    <button name="magnet" class="btn btn-success btn-sm visible-sm"><em class="glyphicon glyphicon-home"></em> Home</button>
    <button name="magnet" class="btn btn-success btn-lg visible-md visible-lg"><em class="glyphicon glyphicon-home"></em> Home</button>

    <button name="magnet" class="btn btn-danger btn-xs visible-xs"><em class="glyphicon glyphicon-download"></em> Download</button>
    <button name="magnet" class="btn btn-danger btn-sm visible-sm"><em class="glyphicon glyphicon-download"></em> Download</button>
    <button name="magnet" class="btn btn-danger btn-lg visible-md visible-lg"><em class="glyphicon glyphicon-download"></em> Download</button>
  </div>
</div>

Bootply

选项#2:媒体查询

而且......最后。这是执行此操作的正确方法。

<div class="container">
  <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3 col-sm-offset-3">
    <button name="magnet" class="btn btn-success btn-autosize">
      <em class="glyphicon glyphicon-home"></em> 
      Home
    </button>

    <button name="magnet" class="btn btn-danger btn-autosize">
      <em class="glyphicon glyphicon-download"></em> 
      Download
    </button>
  </div>
</div>
.btn-autosize {
  padding: 1px 5px;
  font-size: 12px;
  line-height: 1.5;
  border-radius: 3px;
}
@media screen and (min-width: 768px) {
  .btn-autosize {
    padding: 5px 10px;
    font-size: 12px;
    line-height: 1.5;
    border-radius: 3px;
  }
}
@media screen and (min-width: 992px) {
  .btn-autosize {
    padding: 10px 16px;
    font-size: 18px;
    line-height: 1.33;
    border-radius: 6px;
  }
}

Bootply