Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'd suggest using a pseudo element in place of the overlay element. Because pseudo elements can't be added on enclosed <code>img</code> elements, you would still need to wrap the <code>img</code> element though.</p> <p><a href="http://jsfiddle.net/9z26ky19/"><strong>LIVE EXAMPLE HERE</strong></a> -- <a href="http://jsfiddle.net/51csqs0b/"><strong>EXAMPLE WITH TEXT</strong></a></p> <pre><code>&lt;div class="image"&gt; &lt;img src="http://i.stack.imgur.com/Sjsbh.jpg" alt="" /&gt; &lt;/div&gt; </code></pre> <p>As for the CSS, set <strong>optional dimensions</strong> on the <code>.image</code> element, and relatively position it. If you are aiming for a responsive image, just omit the dimensions and this will still work <a href="http://jsfiddle.net/s6q288sw/"><strong>(example)</strong></a>. It's just worth noting that the dimensions must be on the parent element as opposed to the <code>img</code> element itself, <a href="http://jsfiddle.net/qhamuv32/">see</a>.</p> <pre><code>.image { position: relative; width: 400px; height: 400px; } </code></pre> <p>Give the child <code>img</code> element a width of <code>100%</code> of the parent and add <code>vertical-align:top</code> to fix the default baseline alignment issues.</p> <pre><code>.image img { width: 100%; vertical-align: top; } </code></pre> <p>As for the pseudo element, set a content value and absolutely position it relative to the <code>.image</code> element. A width/height of <code>100%</code> will ensure that this works with varying <code>img</code> dimensions. If you want to transition the element, set an opacity of <code>0</code> and add the transition properties/values.</p> <pre><code>.image:after { content: '\A'; position: absolute; width: 100%; height:100%; top:0; left:0; background:rgba(0,0,0,0.6); opacity: 0; transition: all 1s; -webkit-transition: all 1s; } </code></pre> <p>Use an opacity of <code>1</code> when hovering over the pseudo element in order to facilitate the transition:</p> <pre><code>.image:hover:after { opacity: 1; } </code></pre> <p><a href="http://jsfiddle.net/9z26ky19/"><strong>END RESULT HERE</strong></a></p> <hr> <h2>If you want to add text on hover:</h2> <p>For the simplest approach, just add the text as the pseudo element's <code>content</code> value:</p> <p><a href="http://jsfiddle.net/jdL7q7z1/"><strong>EXAMPLE HERE</strong></a></p> <pre><code>.image:after { content: 'Here is some text..'; color: #fff; /* Other styling.. */ } </code></pre> <p>That <em>should</em> work in most instances; however, if you have more than one <code>img</code> element, you might not want the same text to appear on hover. You could therefore set the text in a <code>data-*</code> attribute and therefore have unique text for every <code>img</code> element.</p> <p><a href="http://jsfiddle.net/avz0hgjs/"><strong>EXAMPLE HERE</strong></a></p> <pre><code>.image:after { content: attr(data-content); color: #fff; } </code></pre> <p>With a <code>content</code> value of <code>attr(data-content)</code>, the pseudo element adds the text from the <code>.image</code> element's <code>data-content</code> attribute:</p> <pre><code>&lt;div data-content="Text added on hover" class="image"&gt; &lt;img src="http://i.stack.imgur.com/Sjsbh.jpg" alt="" /&gt; &lt;/div&gt; </code></pre> <p>You can add some styling and do something like this:</p> <p><a href="http://jsfiddle.net/51csqs0b/"><strong>EXAMPLE HERE</strong></a></p> <p>In the above example, the <code>:after</code> pseudo element serves as the black overlay, while the <code>:before</code> pseudo element is the caption/text. Since the elements are independent of each other, you can use separate styling for more optimal positioning.</p> <pre><code>.image:after, .image:before { position: absolute; opacity: 0; transition: all 0.5s; -webkit-transition: all 0.5s; } .image:after { content: '\A'; width: 100%; height:100%; top: 0; left:0; background:rgba(0,0,0,0.6); } .image:before { content: attr(data-content); width: 100%; color: #fff; z-index: 1; bottom: 0; padding: 4px 10px; text-align: center; background: #f00; box-sizing: border-box; -moz-box-sizing:border-box; } .image:hover:after, .image:hover:before { opacity: 1; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload