title: How to overlay text on an image in a container--and keep the text in the container
body: <p>I'm following the solution given in a bagillion places for how to overlay text on an image, using <code>relative</code> and <code>absolute</code>positioning. The problem is that the text with <code>position: absolute</code> pops out of its container and goes to the utmost <code>top</code>, <code>right</code>, etc.</p>
  
  <p>I'd be happy to use a background image, but then I need tricks to get the container to match the size of the image, and I don't know of a way to make it fluid.</p>
  
  <p>Seems like a pretty simple problem that folks are always looking for a solution to. Also the thing of overlaying text on an opaque image, and needing to use <code>:after</code>. Wish there were straightforward options for these situations.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-css lang-css prettyprint-override"><code>.container {
    margin: 0 10%;
  }
  .container img {
    position: relative;
    width: 100%;
  }
  
  #div1 .text {
    position: absolute;
    bottom: 0;
    right: 0;
  }
  
  #div2 .text {
    position: absolute;
    top: 0;
    left: 0;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div id="div1" class="container"&gt;
    &lt;img src="http://placehold.it/800x200" /&gt;
    &lt;div class="text"&gt;
      &lt;h3&gt;Top Image&lt;/h3&gt;
      &lt;p&gt;This text should be in the bottom right of the top image.&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div&gt;&lt;p&gt;A bunch of miscellaneous text here.&lt;/p&gt;&lt;/div&gt;
  
  &lt;div id="div2" class="container"&gt;
    &lt;img src="http://placehold.it/800x200" /&gt;
    &lt;div class="text"&gt;
      &lt;h3&gt;Lower Image&lt;/h3&gt;
      &lt;p&gt;This should be in the top left of the lower image.&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>