如何在引导程序中将文本和链接放在图像上?

时间:2018-03-07 15:40:27

标签: css

我花了很多时间操纵位置,z-index等来尝试让bootstrap在图像上工作。这是我最干净的代码尝试。



img {
    display: block;
    max-width: 100%;
    width: 100%;
    height: 100%;
}
.link a {
    border: 0;
    border-radius: 0;
    padding: 20px 35px;
    background-color: black;
    color: white;
    font-size: 14px;
    text-transform: uppercase;
}

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<div class="container-fluid">
    <div class="row wrapper">
        <img class="img-responsive something-img" src="http://placehold.it/900x100"/>
        <div class="container">
            <div class="row overlay">
                <div class="col-sm-9">
                    <h2>here are some words about stuff</h2>
                </div>
                <div class="col-sm-3 link">
                    <a href="/place">view this thing</a>
                </div>
            </div>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;

我已尝试将位置相对置于叠加和绝对位置。尝试使用叠加上的z-index为2和图像上的z-index为1。我甚至在图像上放置了绝对位置,但它导致它覆盖了下一段代码。不知所措。有什么提示吗?

1 个答案:

答案 0 :(得分:2)

您需要提供包装器position:relative和覆盖行pposition:absolute

img {
  display: block;
  max-width: 100%;
  height: auto;
}

.link a {
  border: 0;
  border-radius: 0;
  padding: 20px 35px;
  background-color: black;
  color: white;
  font-size: 14px;
  text-transform: uppercase;
}

.wrapper {
  position: relative;
}

.wrapper .container {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container-fluid">
  <div class="row wrapper">
    <img class="img-responsive something-img" src="http://placehold.it/900x400" />
    <div class="container">
      <div class="row overlay">
        <div class="col-sm-9">
          <h2>here are some words about stuff</h2>
        </div>
        <div class="col-sm-3 link">
          <a href="/place">view this thing</a>
        </div>
      </div>
    </div>
  </div>