将文字框放在图片上

时间:2018-07-25 06:30:25

标签: html css twitter-bootstrap

我需要制作一个类似于下图的横幅,并可以将框在右侧对齐:

enter image description here

我演示的示例到目前为止可以是seen here

我竭尽所能,但我的知识显然止于此。我无法使带有文本的覆盖框在左侧对齐-仅在底部对齐。因此,如何使文本覆盖框在左侧和右侧对齐。

当视口为< 768px时,横幅将变成一张Bootstrap卡。我想保持这种状态。

我删除了很多与问题不相关的HTML,但是保留了我所有的CSS,因此建议不覆盖任何现有的HTML  CSS。 Bootstrap版本是3.3.7:

  /* 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%;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <title>BMW TEMPLATE</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

</head>
<body>

<div class="container">
    <!-- Full width Banner -->
    <div class="row">
      <a href="#">
        <div class="col-md-12 small-padding top bottom">
            <div class="index-content">
                <div class="card">
                    <img src="http://placehold.it/1200x500"></img>
                    <div class="card-content">
                        <h4>BMW & Summer 2018</h4>
                        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
                        <a href="#" class="blue-button">Læs Mere</a>
                    </div>
                </div>
            </div>
        </div>
      </a>
    </div>
</div>
</body>
</html>

3 个答案:

答案 0 :(得分:2)

这是我的解决方案,以全屏模式打开代码段

将此添加到您的.card content CSS

position: absolute;
right: 10px; /*change this to left for left alignment*/
width: 230px;
background: rgba(0, 0, 0, 0.5);
top: 10px;
bottom: 10px;

将此添加到您的媒体查询(@media (min-width: 768px) {)中,将按钮放在底部

.blue-button {
    position: absolute;
    bottom: 15px;
    left: 50%;
    transform: translateX(-50%);
    display: block;
    margin: 0!important;
  }

/* 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;
    right: 10px;
    width: 230px;
    background: rgba(0, 0, 0, 0.5);
    top: 10px;
    bottom: 10px;
  }
  .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;
  }
  .blue-button {
    position: absolute;
    bottom: 15px;
    left: 50%;
    transform: translateX(-50%);
    display: block;
    margin: 0!important;
  }
}

.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%;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <title>BMW TEMPLATE</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

</head>

<body>

  <div class="container">
    <!-- Full width Banner -->
    <div class="row">
      <a href="#">
        <div class="col-md-12 small-padding top bottom">
          <div class="index-content">
            <div class="card">
              <img src="http://placehold.it/1200x500"></img>
              <div class="card-content">
                <h4>BMW Summer 2018</h4>
                <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
                <a href="#" class="blue-button">Læs Mere</a>
              </div>
            </div>
          </div>
        </div>
      </a>
    </div>
  </div>
</body>

</html>

答案 1 :(得分:1)

当视口高于768时,您可以将.card-content定位为绝对位置。

@media (min-width: 768px)
.card-content {
    position: absolute;
    top: 0;
    left: 0;
    /*right: 0;*//* top position on right*/
    margin: 15px;
    max-width: 200px;
}

/* 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 {
    position: absolute;
    top: 0;
    left: 0;
    margin: 15px;
    max-width: 200px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}
  .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%;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <title>BMW TEMPLATE</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

</head>
<body>

<div class="container">
    <!-- Full width Banner -->
    <div class="row">
      <a href="#">
        <div class="col-md-12 small-padding top bottom">
            <div class="index-content">
                <div class="card">
                    <img src="http://placehold.it/1200x500"></img>
                    <div class="card-content">
                        <h4>BMW & KOED 2018</h4>
                        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
                        <a href="#" class="blue-button">Læs Mere</a>
                    </div>
                </div>
            </div>
        </div>
      </a>
    </div>
</div>
</body>
</html>

答案 2 :(得分:0)

如果要保持CSS文件不变,则可以使用嵌入式CSS叠加效果,将“ top”属性设置为0,将“ left”属性设置为0(如果要对齐,则设置为right到0右边)应该可以解决此问题:

<body>

<div class="container">
    <!-- Full width Banner -->
    <div class="row">
      <a href="#">
        <div class="col-md-12 small-padding top bottom">
            <div class="index-content">
                <div class="card">
                    <img src="http://placehold.it/1200x500"></img>
                    <div class="card-content" style="top:0; left:0; max-width:40%; max-height: 80%">
                        <h4>BMW & KOED 2018</h4>
                        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
                        <a href="#" class="blue-button">Læs Mere</a>
                    </div>
                </div>
            </div>
        </div>
      </a>
    </div>
</div>
</body>

max-height和width-height属性将使您的内容保持响应状态。