html - 在 Bootstrap 按钮上放置 H 标签

标签 html css twitter-bootstrap

我正在使用 Bootstrap 3.3.7。我想在小型和大型设备上放置这样的标题。我尝试使用默认的 Bootstrap 类来定位按钮。但我不能让这项工作成为头条新闻。我想尽可能多地使用 Bootstrap 类,所以我不必自定义和覆盖 Bootstrap 类。

如何定位如下图的标题?

enter image description here

There is a demo of the grid here .

  /* Card and Button */
body {
  background-color: #f5f5f5;
}
div {
  background-color: #fff;
}
.index-content a:hover {
  color: black;
  text-decoration: none;
}
.index-content .row {
  margin-top: 20px;
}
.index-content a {
  color: black;
}
.index-content .card {
  background-color: #FFFFFF;
  padding: 0;
  -webkit-border-radius: 4px;
  -moz-border-radius: 4px;
  border-radius: 4px;
  box-shadow: 0 4px 5px 0 rgba(0, 0, 0, 0.14), 0 1px 10px 0 rgba(0, 0, 0, 0.12), 0 2px 4px -1px rgba(0, 0, 0, 0.3);
}
.index-content .card:hover {
  box-shadow: 0 16px 24px 2px rgba(0, 0, 0, 0.14), 0 6px 30px 5px rgba(0, 0, 0, 0.12), 0 8px 10px -5px rgba(0, 0, 0, 0.3);
  color: black;
}
.index-content .card img {
  width: 100%;
  border-top-left-radius: 4px;
  border-top-right-radius: 4px;
  height: 350px;
}
.index-content .card h4 {
  margin: 20px;
}
.index-content .card p {
  margin: 20px;
  opacity: 0.65;
}
.index-content .blue-button {
  width: 100px;
  -webkit-transition: background-color 1s, color 1s;
  /* For Safari 3.1 to 6.0 */
  transition: background-color 0.5s, color 0.5s;
  min-height: 20px;
  background-color: #4CAF50;
  color: #ffffff;
  border-radius: 4px;
  text-align: center;
  font-weight: lighter;
  margin: 0px 20px 15px 20px;
  padding: 5px 0px;
  display: inline-block;
}
.index-content .blue-button:hover {
  background-color: #dadada;
  color: #002E5B;
}
/* Set width between block elements */
    .small-padding.top {
       padding-top:10px;
    }

    .small-padding.bottom {
        padding-bottom:10px;
    } 
    .small-padding.left {
        padding-left:5px;
    }

    .small-padding.right {
        padding-right:5px;
    }

    .row [class*="col-"] {
      padding-left: 5px;
      padding-right: 5px;
    }

/* Set full width on columns */
@media (max-width: 768px) {
    .img-responsive {
    width: 100%;
    }
}

/* GRID ELEMENTS MEDIA QUERIES */
@media (min-width: 768px) {
  .card {
    position: relative;
  }
  .card-content {
    position: absolute;
    bottom: 0;
    width: 100%;
    background: rgba(0, 0, 0, 0.5);
  }
  .card-content h4,
  .card-content p {
    color: white;
    width: 100%;
    float: left;
    margin: 0 0 5px;
  }
  .card-content a {
    float: right;
  }
  .index-content .card h4,
  .index-content .card p {
    padding: 15px 20px;
    margin: 0;
  }
  .index-content .card p {
    padding: 0 20px 15px;
    margin: 0;
  }
}
.margin_bottom {
  margin-bottom: 10px;
}
.row [class*="col-"] {
  padding-right: 5px;
  padding-left: 5px;
}
.row {
  margin-left: -5px;
  margin-right: -5px;
}
.card-img-bottom {
  color: #fff;
  height: 20rem;
  background: url(images/img1.jpg) center no-repeat;
  background-size: cover;
}
.img-responsive { 
    height: 100%;
}
/* Button Position */
.centered {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

.bottom-right {
    position: absolute;
    bottom: 8px;
    right: 16px;
}

.bottom-left {
    position: absolute;
    bottom: 8px;
    left: 16px;
}
<div class="container">
    
        
   
    <!-- Col 1 - 1 - 2 -->
    <div class="row">
        <div class="col-sm-3 margin_bottom">
            <img src="http://placehold.it/300x410" alt="5" class="img-responsive"></img>
            <h3 class="centered">HEADLINE</h3>
            <button class="btn btn-default centered"style="background-color:transparent;border:2px solid black;">See Offer</button>
        </div>
        <div class="col-sm-6 margin_bottom">
            <img src="http://placehold.it/600x410" alt="5" class="img-responsive" />
            <h3 class="bottom-left">HEADLINE</h3>
            <button class="btn btn-success bottom-right">See Offer</button>
            <!--<i class="fa fa-long-arrow-right"></a>-->
        </div>
        <div class="col-sm-3">
            <div class="row">
                <div class="col-xs-6 col-sm-12 margin_bottom">
                    <img src="http://placehold.it/300x200" alt="5" class="img-responsive" />
                    <h3 class="centered">HEADLINE</h3>
                    <button class="btn btn-success centered">See Offer</button>
                    <!--<i class="fa fa-long-arrow-right"></a>-->
                </div>
                <div class="col-xs-6 col-sm-12 margin_bottom">
                    <img src="http://placehold.it/300x200" alt="5" class="img-responsive" />
                    <h3 class="centered">HEADLINE</h3>
                    <button class="btn btn-success centered">See Offer</button>
                    <!--<i class="fa fa-long-arrow-right"></a>-->
                </div>
            </div>
        </div>
    </div>
    
    
</div>

最佳答案

我会将每个 h3 及其随附的 button 包装在一个 div 中,并使用它同时定位这两个元素。

例如。

<h3 class="centered">HEADLINE</h3>
<button class="btn btn-default centered">See Offer</button>

会变成

<div class="centered">
    <h3>HEADLINE</h3>
    <button class="btn btn-default">See Offer</button>
</div>

/* Card and Button */
body {
  background-color: #f5f5f5;
}
div {
  background-color: #fff;
}
.index-content a:hover {
  color: black;
  text-decoration: none;
}
.index-content .row {
  margin-top: 20px;
}
.index-content a {
  color: black;
}
.index-content .card {
  background-color: #FFFFFF;
  padding: 0;
  -webkit-border-radius: 4px;
  -moz-border-radius: 4px;
  border-radius: 4px;
  box-shadow: 0 4px 5px 0 rgba(0, 0, 0, 0.14), 0 1px 10px 0 rgba(0, 0, 0, 0.12), 0 2px 4px -1px rgba(0, 0, 0, 0.3);
}
.index-content .card:hover {
  box-shadow: 0 16px 24px 2px rgba(0, 0, 0, 0.14), 0 6px 30px 5px rgba(0, 0, 0, 0.12), 0 8px 10px -5px rgba(0, 0, 0, 0.3);
  color: black;
}
.index-content .card img {
  width: 100%;
  border-top-left-radius: 4px;
  border-top-right-radius: 4px;
  height: 350px;
}
.index-content .card h4 {
  margin: 20px;
}
.index-content .card p {
  margin: 20px;
  opacity: 0.65;
}
.index-content .blue-button {
  width: 100px;
  -webkit-transition: background-color 1s, color 1s;
  /* For Safari 3.1 to 6.0 */
  transition: background-color 0.5s, color 0.5s;
  min-height: 20px;
  background-color: #4CAF50;
  color: #ffffff;
  border-radius: 4px;
  text-align: center;
  font-weight: lighter;
  margin: 0px 20px 15px 20px;
  padding: 5px 0px;
  display: inline-block;
}
.index-content .blue-button:hover {
  background-color: #dadada;
  color: #002E5B;
}
/* Set width between block elements */
    .small-padding.top {
       padding-top:10px;
    }

    .small-padding.bottom {
        padding-bottom:10px;
    } 
    .small-padding.left {
        padding-left:5px;
    }

    .small-padding.right {
        padding-right:5px;
    }

    .row [class*="col-"] {
      padding-left: 5px;
      padding-right: 5px;
    }

/* Set full width on columns */
@media (max-width: 768px) {
    .img-responsive {
    width: 100%;
    }
}

/* GRID ELEMENTS MEDIA QUERIES */
@media (min-width: 768px) {
  .card {
    position: relative;
  }
  .card-content {
    position: absolute;
    bottom: 0;
    width: 100%;
    background: rgba(0, 0, 0, 0.5);
  }
  .card-content h4,
  .card-content p {
    color: white;
    width: 100%;
    float: left;
    margin: 0 0 5px;
  }
  .card-content a {
    float: right;
  }
  .index-content .card h4,
  .index-content .card p {
    padding: 15px 20px;
    margin: 0;
  }
  .index-content .card p {
    padding: 0 20px 15px;
    margin: 0;
  }
}
.margin_bottom {
  margin-bottom: 10px;
}
.row [class*="col-"] {
  padding-right: 5px;
  padding-left: 5px;
}
.row {
  margin-left: -5px;
  margin-right: -5px;
}
.card-img-bottom {
  color: #fff;
  height: 20rem;
  background: url(images/img1.jpg) center no-repeat;
  background-size: cover;
}
.img-responsive { 
    height: 100%;
}
/* Button Position */
.centered {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

.bottom-right {
    position: absolute;
    bottom: 8px;
    right: 16px;
}

.bottom-left {
    position: absolute;
    bottom: 8px;
    left: 16px;
}

.wrap {
  text-align: center;
  background: none;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">    
   
    <!-- Col 1 - 1 - 2 -->
    <div class="row">
        <div class="col-sm-3 margin_bottom">
            <img src="http://placehold.it/300x410" alt="5" class="img-responsive"></img>
            <div class="wrap centered">
              <h3>HEADLINE</h3>
              <button class="btn btn-default "style="background-color:transparent;border:2px solid black;">See Offer</button>
            </div>
        </div>
        <div class="col-sm-6 margin_bottom">
            <img src="http://placehold.it/600x410" alt="5" class="img-responsive" />
            <div class="wrap bottom-left">
              <h3>HEADLINE</h3>
              <button class="btn btn-success">See Offer</button>
            </div>
        </div>
        <div class="col-sm-3">
            <div class="row">
                <div class="col-xs-6 col-sm-12 margin_bottom">
                    <img src="http://placehold.it/300x200" alt="5" class="img-responsive" />      
                    <div class="wrap centered">
                      <h3>HEADLINE</h3>
                      <button class="btn btn-success">See Offer</button>
                    </div>
                </div>
                <div class="col-xs-6 col-sm-12 margin_bottom">
                    <img src="http://placehold.it/300x200" alt="5" class="img-responsive" />
                    <div class="wrap centered">
                      <h3>HEADLINE</h3>
                      <button class="btn btn-success">See Offer</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    
</div>

关于html - 在 Bootstrap 按钮上放置 H 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51444126/

相关文章:

javascript - 在 Android 设备上滚动 div

javascript - Tab 选择下一个元素

html - overflow -x visible 不显示ul中溢出的内容

html - 红色框没有出现,而是一条线

html - div 的背景图像不触及页面底部

html - 可以::在媒体查询中删除伪元素之后吗?

css - 不确定如何从 css 中的图像中删除下划线

css - 如何格式化 Bootstrap 表单标签以匹配表单输入的宽度?

twitter-bootstrap - 如何使用 Grunt.js 和 Compass 为 Sass 使用 Bootstrap?

php - 如何在导航栏 CSS 和 HTML 上方显示图像