Jake
Jake

Reputation: 1370

How to put text and link, in bootstrap, on top of an image?

I have spent a lot of time manipulating positions, z-index, etc. to try and get bootstrap to work on top of an image. Here is the cleanest code attempt that I have.

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>

I've tried putting position relative on overlay and absolute. Tried using z-index of 2 on the overlay and z-index of 1 on the image. I even put a position absolute on the image but it caused it cover the next section of code. At a loss. Any tips?

Upvotes: 0

Views: 1702

Answers (1)

Paulie_D
Paulie_D

Reputation: 115108

You need to give the wrapper position:relative and the overlay row 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>

Upvotes: 2

Related Questions